How to back up multiple servers


Just writing down the process so I don't forget. If anyone else gets some use out of it, that's cool too...

Here's how I just set up my Mac to automatically back up 2 servers, as well as my home directory, to an external firewire drive. The process uses stuff that's included with MacOSX, so won't cost a dime. And it's automatable, so I won't forget to run it.

Set up SSH to allow automated connection

Following these instructions, boiled down to bare essentials below. Run this stuff from the "client" machine (in my case, my desktop box in my cube) - where all data will wind up.

% .ssh/authorized_keys2
% scp ~/.ssh/id_dsa.pub :~/.ssh/authorized_keys2

repeat scp step for each server

This will allow your account on that client machine to SSH, rsync and SCP without being prompted for a password every time, making it possible to automate the process.

Create a shell script to automate backups

Using rsync to copy directories from the server(s) to a local volume (preferably an external drive). I created a file at ~/bin/backup_servers.sh, but it can live anywhere. Replace [SERVER] with the ip/domain of the server to be backed up. Replace [DIRECTORY] with the directory on the server to be backed up (could be something like /Library/WebServer). Replace [DEST_DIRECTORY] with the directory that will contain the backup (could be something like /Volumes/BackupDrive/[SERVER]/[DIRECTORY]).

#!/bin/sh

echo "Backing up [SERVER] [DIRECTORY] directory"
rsync -rtlzv --ignore-errors -e ssh [USERNAME]@[SERVER]:[DIRECTORY] [DEST_DIRECTORY] > [DEST_DIRECTORY]/backup.log

Tweak directories as needed, but this should create a backup (without deleting missing files) of the server directory to the external hard drive. If you want to back up more than one server, or more than one directory, just keep repeating the echo/rsync lines as needed (changing values for each server/directory, of course). I have 5 entries in my script, copying a couple of directories from 2 servers, and also backing up my home directory on my Desktop machine.

Automate it via cron

I have my cron tasks defined in a file at ~/mycrontab, so I just added this to the end of that file:

30	4	*	*	*		~/bin/backup_servers.sh > /dev/null

So now, every morning at 4:30AM, the servers and directories that I've specified in ~/bin/backup_server.sh will get backed up to my external firewire drive (assuming it's on and mounted). I'm sure I could be doing fancier things to make this process even smoother, but it seems to be working fine now.

At the moment, I have stuff backed up from the 2 servers, and important stuff from my Powerbook (iPhoto library, iTunes library, Documents, etc...) get copied to the desktop where they get backed up automatically to the external drive.

comments powered by Disqus