As mentioned in yesterday’s post, I’ve taken to backing up the important guts of my server via Dropbox. It turned out to be very easy, and gives me the added benefit of not having to do any sort of versioning: I get that for free with Dropbox. Plus, it seamlessly integrates with my existing backup routines.
So, how do I do it? It’s honestly very simple.
First, I generate some backups. I run these out of cron every morning.
mysqldump -u root wordpress | gzip -c > /path/to/backup/blogdb.sql.gz tar czf /path/to/backup/apache2.tgz /etc/apache2/*
I do this for my web databases, any important config files, crontabs, etc. (The actual sites are already backed up, since I develop them locally.)
Once they’re dropped off in the backup location, it’s just a matter of having the script come along and copy them to Dropbox. I chose to write it in ruby. Honestly, my code is 90% of what you find in Dropbox’s example. Here it is, in all of its glory:
require 'dropbox_sdk' # Dropbox app key, secret, token APP_KEY = 'get your own' APP_SECRET = 'get your own' ACCESS_TOKEN = 'get your own' BACKUP_DIR = '/path/to/backup/' # Open our Dropbox Client client = DropboxClient.new(ACCESS_TOKEN) # Our hardcoded list of files files = ['list of files to backup'] # For each file, let's upload it files.each do |filename| file = File.new(BACKUP_DIR + filename) # Send file to dropbox -- last param tells us to overwrite the previous one response = client.put_file('/' + filename, file, true) end
That’s it. I don’t really do any error checking (I should, and probably will some day, but I don’t today). I should probably store the key/secret/token in another place, but since my app was created and only has access to one Dropbox folder, and I can revoke access at any time, it’s not too much of a risk. Eventually, when I get really ambitious, I’ll have the files list be dynamic, not static. But for now, it’s less than 20 lines of code to backup important files.
That’s good enough for me.