Git backup with Dropbox
Today I wanted to write how I have set up my git repositories for backup. At work I have quite limited backed up space, but scratch space which is not backed up I get plenty of. I keep my different projects in git repositories, and I also mirror the svn repositories for the programs I use at CERN (they just recently moved from CVS to SVN).
Just to point out this to users that might get confused: The command && just means that I combine two lines, to save linespace in this blog.. It is basically for me to point out where you have to use a certain command.
First I create the repositories on the server space I have. If you do not know how to do that (
git init && git add <file> && git commit), then have a look at a brief tutorial to git before you continue.
Next in each repository I create a remote repository with the name "dropboxbackup", like this:
git remote add dropboxbackup file:///home/myusername/Dropbox/git/nameofgitrepo/
Next I need to initialize the new repository in Dropbox. It is purely backup, so I choose a bare repository:
cd ~/Dropbox/git/nameofgitrepo/ && git init --bare
Now, I can backup my repository with the following command (inside the repository on my server)
git push --mirror dropboxbackup
In order to simplify things, you can add a new alias in git:
cd /path/to/gitrepo/on/server/ && git config alias.backup "push --mirror dropboxbackup"
This way you can run the backup with
This is getting quite easy, but one thing which is easy to forget is to actually back up. This is where cron come into play. First make sure the cron daemon is running on your system. Next, you write a small script "backupGit.sh" with the following content:
Finally you add the job to cron with the command "
@hourly ID=backupgitrepos nice -n 19 /path/to/script/backupGit.sh >> /path/to/script/backupLog.txt 2>&1
Notice that I used “>>”, which means the file backupLog.txt must exist before the job is ran. You can do this with “
touch /path/to/script/backupLog.txt“. This will make sure that every hour your git repositories will be backed up to Dropbox. Have a look in the backupLog.txt file where you can see if everything is going as expected. You can of course still backup manually with "
git backup" in addition to the automatic backup whenever you want. For me this means that I will have a copy of the repositories on the server, on all my computers that have installed the dropbox client and on the dropbox server itself. That is actually significantly better than the daily backup they offer at work!
If something was confusing, or you have suggestions for a better setup, let the comments flow!