Skip to content

Instantly share code, notes, and snippets.

@philippb
Forked from weavenet/gist:1524092
Created March 6, 2012 20:52
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save philippb/1988919 to your computer and use it in GitHub Desktop.
Save philippb/1988919 to your computer and use it in GitHub Desktop.
Complete git repository backup script to AWS S3
#!/bin/bash
# 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=$GITHUB_BACKUP_BUCKET
dir=$GITHUB_BACKUP_DIR
password=$GITHUB_BACKUP_PASSWORD
account=$GITHUB_ACCOUNT
# Setup repository to $1
repository=$1
date=`date '+%Y%m%d%H%M%S'`
# Create the backup directory
mkdir -p $dir
echo "Backing up $repository"
git clone --mirror git@github.com:$account/$repository.git $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error cloning $repository"
exit 1
fi
tar cpzf $dir/$repository.$date.git.tar.gz $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error compressing $repository"
exit 1
fi
#Optional file encryption
#echo $password | gpg -c --passphrase-fd 0 $dir/$repository.$date.git.tar.gz
#if [ $? -ne 0 ]; then
# echo "Error encrypting $repository"
# exit 1
#fi
if [ -f $dir/$repository.$date.git.tar.gz ]; then
s3cmd put $dir/$repository.$date.git.tar.gz s3://$bucket/git.$repository.$date.git.tar.gz
fi
if [ $? -ne 0 ]; then
echo "Error uploading $repository to S3"
exit 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"
exit 1
fi
echo "Succesfully backed up $repository"
@jbdhacf
Copy link

jbdhacf commented Aug 14, 2019

Thanks for the script.

Just want to add if you are a windows user change the S3 upload to:

if [ -f $dir/$repository.$date.git.tar.gz ]; then
#s3cmd put $dir/$repository.$date.git.tar.gz s3://$bucket/git.$repository.$date.git.tar.gz
aws s3 cp $dir/$repository.$date.git.tar.gz s3://$bucket/$dir/git.$repository.$date.git.tar.gz
fi

@kraghav
Copy link

kraghav commented Apr 13, 2020

How do I restore it from S3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment