pf » Howto Backup your Mac incrementally over SSH
Howto Backup your Mac incrementally over SSH

Do you have access to a shell account on a unix server with some spare space? If so it's pretty easy to incrementally backup your files securely with SSH.
I titled this entry Howto Backup your Mac incrementally over SSH but this technique can also be used to backup any computer that can run rsync and ssh. They are already installed on Mac OS X, and most linux / unix servers.
Step 1 - Create a folder to store your backups on the remote server
mkdir backup
Make sure that your SSH user has permission to write to this directory.
Step 2 - Setup automatic authentication Optional
This step allows the backups to run without prompting you for a password when it runs. You can omit this step but you will have to type in your ssh password each you run backup.
I wrote an article called Setting up public key authentication over SSH that will guide you through this step.
If you own the server you might also want to create a user specifically for this process.
Step 3 - Use rsync to backup files incrementally
rsync -e "ssh" -rca --delete-after ~/test/ pete@myserver.example.com:backup
Now lets break it down a bit:
- rsync - this syncs the local directory to with the server directory.
- -e "ssh" - this tells
rsyncto usesshif your want to pass in other ssh options such as port you can do that in the quotes:-e "ssh -p 12345" - -rca recursive, checksum, and archive
- --delete-after - this will delete files on the server if you delete them locally.
- ~/test/ - I am backing up / syncing the
testdirectory inside my home directory on my mac. - pete@myserver.example.com:backup - my ssh username is
pete, my remote ssh server hostname ismyserver.example.com, and I am backing up into the directory~pete/backup.
Excluding directories
Sometimes you might want to exclude a directory from being backed up, perhaps your Music directory since that is already backed up on your ipod.
rsync -e "ssh" -rca --delete-after --exclude=Music --delete-excluded ~/test/ pete@myserver.example.com:backup
Step 4 - Schedule it with cron Optional
Now lets create a cron job (scheduled task) to run this script every day. First make a new file called backup.sh in your home directory.
#!/bin/sh rsync -e "ssh" -rca --delete-after ~/test/ pete@myserver.example.com:backup
Now sure make the file is executable: chmod ug+x backup.sh
Next type crontab -e this will open up your scheduled tasks list, in most cases it will open an empty file. Add the following to the file:
15 11 * * * yourusername ~yourusername/backup.sh
That's will schedule the script to run at 11:15am. If you don't want to do this step you can simply run the command whenever you want to run a backup.
DISCLAIMER You should test everything before performing the backups on your live data. If you would like to use the scripts above you must acknowledge that I'm not responsible for any loss of data.
I'd love to hear your feedback on this, please comment below. I'm use this technique to backup my two web servers on to each other.
add to del.icio.us
| Tags: backup, ssh, rsync, mac, osx, linux, unix, tips, howto
Related Entries
- The 15 Essential UNIX commands - July 29, 2005
- Anyone used MacFuse? - January 13, 2007
- Top 20 Internet Security Vulnerabilities of 2005 - November 23, 2005
- How to Disable the Safari RSS Feature - August 7, 2005
- Dashboard Widget Tip - Quickly Removing Widgets - May 13, 2005
this is good to backup datafiles (that are single-streamed), but not all files are.
make sure that the copies of the files you have are readable on the backup media...
Change the following variables:
- RSYNCUSER=unix username on remote host - EMAIL=your email address - BACKUPSERVER=your remote hostname - KEY=path to your ssh key (passwordless login)
#!/bin/bash LABEL="BACKUP-`date +%Y%m%d`" SUMMARY="/var/tmp/summary" TIME=`date +%k:%M:%S` BACKUPDATE=`date "+%A %d/%m/%Y"` EMAIL=your@email.address RSYNCUSER=your_rsync_username BACKUPSERVER=backup.server KEY=/root/keys/backup.server.key
check_server() { ssh -i $KEY $RSYNCUSER@$BACKUPSERVER exit >> /dev/null 2>&1 if [ $? -ne 0 ]; then printf "Backup did not run. The backup server was unavailable.\n" >> $SUMMARY cat $SUMMARY | mail -s "Backup summary for $BACKUPDATE" $EMAIL exit 1 fi }
run_backup() { printf "Filesystem backup performed on `date +%m/%d/%Y`.\n\n" >> $SUMMARY printf "\nSession summary:\n" >> $SUMMARY printf "\n\t\t`date +%k:%M:%S`\t- Session started\n" >> $SUMMARY printf "\n\t\t`date +%k:%M:%S`\t- Processing backup\n" >> $SUMMARY rsync -ao --stats -e "ssh -i $KEY" / $RSYNCUSER@$BACKUPSERVER:/ --exclude='/proc*' --exclude='/dev*' > /var/log/backup.log 2>&1 if [ $? -eq 0 ]; then printf "\n\t\t`date +%k:%M:%S`\t- Session completed\n\n" >> $SUMMARY else printf "\n\t\t`date +%k:%M:%S`\t- Session aborted\n\n" >> $SUMMARY fi }
check_server run_backup cat $SUMMARY | mail -s "Backup summary for $BACKUPDATE" $EMAIL exit 0
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
RSS
Pete Freitag is a software engineer, and web developer located in









