infra/codestorage/hg-repo-sync
infra/codestorage/hg-repo-sync/pull.sh
Remove the hgrc file, fix permissions. When pulling backups, make sure we set the permissions to 0770 as well as change the group they belong to. This caused us to not be able to write changes (oops). Also, remove the hgrc file. Which means our annoying "not trusting file /mounted/repos/blah-blah/.hg/hgrc from untrusted user root, group committers" message is back, but that's a minor nuisance. The real issue is that we're overwriting the hgrc for everything _anyways_ to get the push hook, etc. So there seemed to be little point.
| paddy@1 | 1 #!/bin/bash |
| paddy@1 | 2 today=$(date +%F) |
| paddy@1 | 3 |
| paddy@1 | 4 yesterday=$(date --date yesterday +%F) |
| paddy@1 | 5 |
| paddy@1 | 6 DOMAIN=${DOMAIN:-code.secondbit.org} |
| paddy@1 | 7 BACKUP_BUCKET=${BACKUP_BUCKET:-backups.$DOMAIN} |
| paddy@1 | 8 |
| paddy@1 | 9 echo "Fixing permissions..." |
| paddy@1 | 10 chgrp -R 2000 /mounted/repos |
| paddy@1 | 11 |
| paddy@1 | 12 echo "Cleaning up..." |
| paddy@1 | 13 rm -rf /tmp/repos/* |
| paddy@1 | 14 |
| paddy@1 | 15 echo "Downloading bundles from gs://${BACKUP_BUCKET}/${today}" |
| paddy@1 | 16 |
| paddy@1 | 17 output=$(gsutil cp -R gs://$BACKUP_BUCKET/$today/\* /tmp/repos 2>&1) |
| paddy@1 | 18 echo $output |
| paddy@1 | 19 |
| paddy@1 | 20 bundles=$(find /tmp/repos -name '*.bundle') |
| paddy@1 | 21 |
| paddy@1 | 22 # basically, if we get an error downloading the bundles, try for yesterday's |
| paddy@1 | 23 # this could happen if the pod restarts between the last upload of day A and the first upload of day B |
| paddy@1 | 24 if [[ $output == *"No URLs matched:"* ]] |
| paddy@1 | 25 then |
| paddy@1 | 26 echo "Downloading yesterday's bundles from gs://${BACKUP_BUCKET}/${yesterday}" |
| paddy@1 | 27 gsutil cp -R gs://$BACKUP_BUCKET/$yesterday/\* /tmp/repos |
| paddy@1 | 28 mv /tmp/repos/$yesterday /tmp/repos/$today |
| paddy@1 | 29 bundles=$(find /tmp/repos -name '*.bundle') |
| paddy@1 | 30 fi |
| paddy@1 | 31 |
| paddy@1 | 32 for bundle in $bundles |
| paddy@1 | 33 do |
| paddy@1 | 34 dir=$(dirname $bundle) |
| paddy@1 | 35 stripped=${dir#.} |
| paddy@1 | 36 stripped=${stripped#/tmp/repos} |
| paddy@1 | 37 target=${bundle#/tmp/repos} |
| paddy@1 | 38 target=${target%.bundle} |
| paddy@1 | 39 if [ -d "/mounted/repos${target}" ] |
| paddy@1 | 40 then |
| paddy@1 | 41 echo "Pulling changes from $bundle to /mounted/repos$target" |
| paddy@1 | 42 hg --cwd /mounted/repos${target} pull $bundle |
| paddy@1 | 43 else |
| paddy@1 | 44 echo "Creating /mounted/repos$target repo from $bundle" |
| paddy@1 | 45 hg clone $bundle /mounted/repos${target} |
| paddy@1 | 46 fi |
| paddy@3 | 47 chgrp -R 2000 /mounted/repos${target} |
| paddy@3 | 48 chmod -R 0770 /mounted/repos${target} |
| paddy@1 | 49 done |
| paddy@1 | 50 |
| paddy@1 | 51 echo "Cleaning up..." |
| paddy@1 | 52 rm -rf /tmp/repos/* |
| paddy@1 | 53 |
| paddy@1 | 54 echo "Pull complete." |