August 28th, 2006
I don’t trust computers to not suddenly explode and destroy everything I’ve ever created, and I’ve discovered what I believe to be the easiest and most foolproof way to backup my code, or other things for that matter. Email it to yourself automatically.
I use Subversion to manage my personal code library on a remote server, and since my goal is to always have everything important exist in two separate places, I have a compressed dump of the entire repository emailed to myself weekly. I delete the older versions, but since my email account has plenty of free space it’s not very urgent. This all seems straightforward, but I had to do a lot of research to find the right program to use, which I am now sharing with you. All you need to do is set up a cron job like this…
0 6 * * 0 (svnadmin dump ~/svn/repo/ | gzip --best > ~/svn/repo-backup.svndump.gz) 2>&1 | mutt -s 'repo-backup.svndump.gz '$(date +%m/%d/%Y) -a ~/svn/repo-backup.svndump.gz email@example.com
Obviously, replace the path to the repository and the email address to correct values, and put the backup file somewhere too. This particular job is scheduled to run at 6am on Sunday morning. The repository (~/svn/repo) is dumped and compressed (using gzip, at the highest level of compression) to the file "~/svn/repo-backup.svndump.gz". I use
mutt to send the email with the file attached. The subject is the name of the file and the date the email was sent (to avoid grouping backup emails together). I also want messages that
svnadmin outputs (which contain information on what was dumped, and possible error messages) to be the body of the email, but
svnadmin sends the messages through stderr while
muttÂ receives the body of the email through stdout. To fix this stderr is redirected as stdout after stdout has been used to compress the dumpfile. Isn’t Unix great?
Anyway, if you use Gmail, besides a lot of space to manage these backups, you can also make the process easier using plus-addressing. Let’s say your email is "firstname.lastname@example.org", then set the email address in the example to "email@example.com". Set up a label called "Backups" in Gmail and a filter which automatically labels emails as "Backups" when they have been sent to "firstname.lastname@example.org". You can keep them out of your Inbox if you archive them with the filter as well.
Some other important things…
- Keep in mind there are size limits for attachments in emails, but this isn’t much of a problem as long as your repository stays fairly small (and full of text files).
- You don’t need to send them to one address, check the manual page for mutt for more information.
- Using bzip2 rather than gzip would be a better idea, as bzip2 has recovery features if something goes wrong,
but currently Gmail has a major bug when viewing emails with bzip2-compressed files attached. These emails are improperly handled and won’t open through Gmail.
- I prefer to use a
svnadmin dumpsince I won’t be making any commits at exactly that moment, but more active repositories should probably dump a copy of the repository usingÂ
svnadmin hotcopyinstead as a safety precaution.