Skip to content

Git backup with Dropbox

August 18, 2010

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

git backup

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:

#!/bin/bash

date
cd /path/to/gitrepo/on/server/
pwd
git backup
cd /path/to/gitrepo2/on/server/
pwd
git backup
...

Finally you add the job to cron with the command "crontab -e"

@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! 🙂

Advertisements
4 Comments leave one →
  1. December 9, 2011 7:34 am

    How about doing the backup push in a commit hook? There’s no need to run the backup unless something is committed and there’s no overhead of running it often since it only pushes the deltas. Comments on this?

  2. December 9, 2011 8:21 am

    I agree that that could work for some, but wouldn’t that require that you’re always pushing from the same computer that runs the backup? In my case I push from several machines and they don’t always have access to the machine at work that runs the backup. So it basically depends on how your workflow is I think, but it is clearly a good suggestion.

    • December 9, 2011 8:35 am

      Yeah, I guess it depends on your setup. I was thinking of pushing from workstations to a central repo that has commit access to the backup repo. For private backups I would just keep the project files in my Dropbox or let the hourly rsync / time machine handle it.

Trackbacks

  1. Weekend links | the dreadful flying blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: