Skip to content

Instantly share code, notes, and snippets.

@peterkeen
Created December 4, 2015 14:59
Show Gist options
  • Save peterkeen/d64381ec0415d7ab6f7b to your computer and use it in GitHub Desktop.
Save peterkeen/d64381ec0415d7ab6f7b to your computer and use it in GitHub Desktop.
#!/bin/bash
# This depends on a file named `/root/backup_dirs` containing a list of directories to backup
# one per line. All directories in that file, plus a database dump, will be archived together.
#
# Further, if you have a gitolite installationn, this will backup every repo in an individual archive.
set -e
set -x
set -o pipefail
date=`date +%Y-%m-%d-%H-%M-%S`
hostname=`hostname`
database_dir=/tmp/databases
repo_dir=/home/git/repositories
mkdir -p $database_dir
chmod 777 $database_dir
cd $database_dir
if [[ -e /usr/bin/pg_dumpall ]]; then
su -c "/usr/bin/pg_dumpall -f $database_dir/databases.sql" postgres
fi
backup_dirs="$database_dir"
for backup_dir in `cat /root/backup_dirs`; do
if [[ -d $backup_dir ]]; then
backup_dirs="$backup_dirs $backup_dir"
fi
done
/usr/local/bin/tarsnap -c -f backup-$hostname-$date $backup_dirs
if [[ -d $repo_dir ]]; then
pushd $repo_dir
for repo in `find . -name '*.git' -type d`; do
pushd $repo
if [[ `date +%u` == "7" ]]; then
git gc --aggressive --quiet
else
git gc --auto --quiet
fi
popd
/usr/local/bin/tarsnap -c -f $repo-$date $repo
done
popd
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment