Back up user directory using rsync over ssh as hourly cron job

Alec Jacobson

November 18, 2009

weblog/

First I followed this blogger's instructions to set up my ssh password so that my script wouldn't have to prompt for a password. I tested this by sshing into my destination server (shouldn't ask for password now). There I made a directory to put my backup:
mkdir ~/macbook-pro-backup
I don't want to back up everything in my user directory so I made an exclude file at ~/.cims-exclude-list:
/ajx/Library
/ajx/Music
/ajx/Movies
/ajx/Pictures
/ajx/Documents/Parallels
/ajx/Downloads
/ajx/Desktop
/ajx/.Trash
*.mp3
.DS_Store
Then I constructed and tested my rsync command:
rsync -e ssh -avz --exclude-from ~/.cims-exclude-list ~ jacobson@access.cims.nyu.edu:macbook-pro-backup
Note: I have not used the --delete option because for now I have the space available on my destination server, but eventually I will probably have to start deleting older, no-longer-existent files. After I used the above command to do the initial (big/long) transfer, knowing that it works I constructed this bash script to keep a nice log. I saved this in cims-backup.sh:
#!/bin/bash
touch ~/.cims-backup.log
if rsync -e ssh -az --exclude-from ~/.cims-exclude-list ~ \
  jacobson@access.cims.nyu.edu:macbook-pro-backup
then
  echo "Back up succeeded on $(date)" >> ~/.cims-backup.log
else
  echo "Back up failed on $(date)" >> ~/.cims-backup.log
fi
Issue the following command to make this file executable
chmod -x cims-backup.sh
I ran this (bash cims-backup.sh) a couple times and checked the log file: cat ~/.cims-backup.log should produce something like
Back up succeeded on Wed Nov 18 16:23:49 EST 2009
Back up succeeded on Wed Nov 18 16:24:04 EST 2009
To be sure I sshed into my destination server and checked the macbook-pro-backup directory. All that's left is to set up an hourly cron job. To do this, in the terminal issue:
crontab -e
Then add this line and save:
0 * * * * /path/to/cims-backup.sh
If you are using vim to edit and you are getting this error:
crontab: temp file must be edited in place
then add this line to your .vimrc file:
set backupskip=/tmp/*,/private/tmp/*