Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dbuentello/459469bbc1ff0f3dda720d7d0215be80 to your computer and use it in GitHub Desktop.
Complete git repository backup script to Backblaze B2
# Script to backup git repo to Backblaze B2
# 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
# Ensure you have authorized the B2 command line tool with the correct account AND added your SSH
# public key to your github account, if you need to backup private repositories.
# To restore this repo in the future, download it from B2, extract it and then use this command:
# cd old-repository.git
# git push --mirror
# Future details are here:
# Setup repository to $1
date=`date '+%Y%m%d%H%M%S'`
# Create the backup directory
mkdir -p $dir
echo "Backing up $repository"
git clone --mirror$repository $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error cloning $repository"
exit 1
tar cpzf $dir/$repository.$date.git.tar.gz $dir/$repository.$date.git
if [ $? -ne 0 ]; then
echo "Error compressing $repository"
exit 1
#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
if [ -f $dir/$repository.$date.git.tar.gz ]; then
b2 upload-file $bucket $dir/$repository.$date.git.tar.gz git.$repository.$date.git.tar.gz
if [ $? -ne 0 ]; then
echo "Error uploading $repository to Backblaze B2"
exit 1
#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
echo "Succesfully backed up $repository"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment