automated gitolite backups

Since I run a gitolite server for my private projects, I needed a way to regularly backup these repositories. The following script is run via crontab as the gitolite user, and creates bundled versions of all hosted repositories. Those are then pushed via SMB to a network attached storage.

The workflow is as follows:

  • iterate over all existing repositories: all repositories are subfolders in the repositories folder
  • for each repository, create a bare clone, and of that create a git bundle
  • pack all those bundles into an archive
  • push the new archive to a defined directory on a network share
#!/usr/bin/env bash

workdir="/tmp/gitbackup"

mkdir -p "$workdir"
for repo in /home/git/repositories/*;
do
	reponame=$(basename $repo)
	cd "$workdir"
	git clone --bare "$repo"
	cd "$reponame"
	git bundle create "$workdir/${reponame}.bundle" --all
done;

cd "$workdir"

archive="$(date +%F).tar.gz"
tar -zcf "$archive" *.bundle


smbclient //my-share/backup <password> -U=<user>  <<EOC
cd git
put $archive
EOC

rm -rf "$workdir"
comments powered by Disqus