Skip to content

Instantly share code, notes, and snippets.

@mjgp2
Created September 6, 2017 11:52
Show Gist options
  • Save mjgp2/b4e25ef4583226e7d1060f374489825a to your computer and use it in GitHub Desktop.
Save mjgp2/b4e25ef4583226e7d1060f374489825a to your computer and use it in GitHub Desktop.
Backup github repositories to S3
#!/bin/bash -ex
# Script to backup git repo to S3
# Set bucket, dir, password and account to use for the backup. I keep mine in local env vars
# These are set by localrc which lives on an encrypted home directory and is executed by my bashrc
bucket=$S3_BACKUP_BUCKET
dir="tmp-backups"
account=$GITHUB_ACCOUNT
date=`date '+%Y%m%d%H%M%S'`
# Create the backup directory
mkdir -p $dir
function backup {
repository=$1
echo "Backing up $repository"
git clone --mirror [email protected]:$account/$repository.git $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error cloning $repository"
return 1
fi
tar cpzf $dir/$repository.$date.git.tar.gz $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error compressing $repository"
return 1
fi
if [ -f $dir/$repository.$date.git.tar.gz ]; then
aws s3 cp $dir/$repository.$date.git.tar.gz s3://$bucket/$repository/$repository.$date.git.tgz || return false
fi
if [ $? -ne 0 ]; then
echo "Error uploading $repository to S3"
return 1
fi
#delete tar file and checked out folder
/bin/rm $dir/$repository.$date.git.tar.gz
/bin/rm -rf $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error removing $repository"
return 1
fi
echo "Succesfully backed up $repository"
return 0
}
# Setup repository to $1
for input in $@
do
backup $input || exit 1
backup "$input.wiki" || true
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment