Created
June 5, 2018 15:15
-
-
Save johnstanfield/b18329f0dcee4eda787c2a65ffe5c1e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# backs up gitolite to s3 | |
# SSH's into the gitolite host to list all repositories | |
# then clones each repo | |
# - on subsequent runs, it updates the repo rather than reclone (this is faster) | |
# - todo: test what happens after a force push; perhaps rm -rf and reclone | |
# then does a git bundle to turn the repo into a single-file backup | |
# - todo: handle empty repos (or delete empty repos from gitolite) | |
# then pushes the file to an S3 bucket | |
# - the bucket should have a lifecycle policy to delete old backups | |
# | |
DATE=`date +%Y%m%d` | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
LOGFILE="$DIR/git-backup.log" | |
BUCKET_PATH="my-bucket/git" | |
GITOLITE_HOST="[email protected]" | |
log(){ | |
indent=" " | |
if [ "$2" = "noindent" ]; then | |
indent="" | |
fi | |
msg="`date '+%Y-%m-%d %H:%M:%S'` ${indent}${1}" | |
printf '%s\n' "$msg" | tee -a "$LOGFILE" | |
} | |
mkdir -p bundles | |
mkdir -p repos | |
fail=false | |
for repo in `ssh $GITOLITE_HOST info | cut -f 2 | tail -n +3`; do | |
log "backing up $repo..." noindent | |
subdir="$(dirname $repo)" | |
cd "$DIR/repos" | |
if [ -e $repo.git ]; then | |
log "$repo exists, updating" | |
cd $repo.git | |
log "`git remote update 2>&1`" | |
else | |
log "$repo does not exist, cloning" | |
mkdir -p "$subdir" | |
cd "$subdir" | |
log "`git clone --mirror $GITOLITE_HOST:$repo 2>&1`" | |
fi | |
cd "$DIR/repos/$repo.git" | |
bundle="$DIR/bundles/$repo" | |
mkdir -p "$(dirname $bundle)" | |
log "making $bundle" | |
bundle_message="`git bundle create "$bundle" --all`" | |
if [ "$bundle_message" != "" ]; then | |
fail=true | |
log "$bundle_message" | |
else | |
log "made $bundle" | |
fi | |
log "`aws s3 cp --no-progress "$bundle" "s3://$BUCKET_PATH/$DATE/$repo" 2>&1`" | |
done | |
if [ fail ]; then | |
log "a failure happened" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment