Mac – How to use rsync to backup the Mac to a NAS

backupmacrsync

I need to backup a bunch of directories to my NAS (it basically shows up as a network share in Finder). I don't want to make it bootable, just need to backup a few directories. I'd also like to have it run nightly. I've tried looking at documentation for rsync, but haven't been able to figure it out yet.

I also took a look at rsyncx, but can't get that to install. Don't care about Time Machine support either, just need a simple backup.

Best Answer

Does your NAS have an rsync service set up? Some support it (e.g. ReadyNAS). If so, you can do something like this:

#!/bin/sh

DEST=xx.xx.xx.xx::backup/swilliams/
RSYNC_OPTS='-vaC --exclude ".DS_Store"'

/usr/bin/rsync $RSYNC_OPTS --exclude "build/" --exclude "dist/" --exclude "*.pyc" ~/code $DEST
/usr/bin/rsync $RSYNC_OPTS ~/Music $DEST
/usr/bin/rsync $RSYNC_OPTS ~/Pictures $DEST
/usr/bin/rsync $RSYNC_OPTS --exclude "Virtual Machines*" ~/Documents $DEST

You'll need to change DEST to the IP address of your NAS, and change paths and stuff to suit.

If you save that as, say, ~/bin/backup.sh (and be sure to do chmod a+x ~/bin/backup.sh) then you can run it nighly using cron. Run crontab -e and add this line:

0 0 * * *      $HOME/bin/backup.sh > $HOME/logs/backup.log 2>&1

(0 0 * * * means: run at midnight every day, every month. First column is minutes, so 3am is 0 3 * * *. This will write logs in ~/logs so make sure that directory exists, or put them somewhere else)

If your NAS doesn't support rsync as a service then I think it should work if you change the start to this:

#!/bin/sh

mount_smbfs //user:password@xx.xx.xx.xx/backup /Volumes/backup
DEST=/Volumes/backup

and at the end:

umount /Volumes/backup

(if your share is open, you can leave off user and password)

If you want timestamped backups, you can experiment with the date command. e.g.

DATE=`date +%Y%m%d`

then access $DATE in your script.

If anything here isn't clear to you, just ask.