How do I use rsync to backup my Mac to a NAS?

13,086

Solution 1

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:[email protected]/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.

Solution 2

When using rsync for backup the most import options to have a look at are --backup and --backup-dir, without those you are doing a copy, not backup. As with backup you want to keep track of changes, instead of just overwrite them. What I use is this:

#!/bin/sh

RSYNCOPTS="-e ssh --delete --one-file-system --relative --archive -P --verbose --compress --recursive"
BACKUPDIR="/backup/"
BACKUPINCR="${BACKUPDIR}/incr/$(date -I)"
BACKUPROOT="${BACKUPDIR}/root/"
EXCLUDES="--exclude-from /home/juser/.backup-excludes"

# Make sure we use a new incr backup directory on each run
NUM=0
while [ -e "${BACKUPINCR}-${NUM}" ]; do
  NUM=$(($NUM  + 1))
done
BACKUPINCR="${BACKUPINCR}-${NUM}"

for i in /home/ /etc/; do
  rsync $RSYNCOPTS $EXCLUDES--backup --backup-dir="$BACKUPINCR" "$i" "$BACKUPROOT"
done

This gives you a copy in /backup/root/ and a backup of all changed or deleted files in /backup/incr/$DATE-$NUM.

This script uses --one-file-system, so it doesn't cross file system borders and it uses --relative to get the absolute path names preserved.

If you want it extra pedantic, you can add --checksum, that will checksum all files instead of just using the timestamp for skipping. Advantage is that your backup will have a better chance of being free of bit-flips, but it will run for a good while longer.

Another general advise, always use --dry-run when you play around with new option where you are not sure what they do, especially when it comes to --delete and friends, as you don't won't to wipe out your already existing backup by accident.

Share:
13,086

Related videos on Youtube

swilliams
Author by

swilliams

I enjoy Programming, Photography, Writing, Cooking, Running, and capitalizing the wrong letters in sentences.

Updated on September 17, 2022

Comments

  • swilliams
    swilliams over 1 year

    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.

  • Chealion
    Chealion almost 15 years
    Some applications still use Resource Forks. For example Quicken for Mac's backups and main data files become completely useless without the resource fork.
  • swilliams
    swilliams almost 15 years
    Ok, I ran that, but it took FOREVER. About 10 hours to sync up 300GB of data. Will subsequent runs be quicker after the first one? If not, I'll need to look at another solution.
  • John Fouhy
    John Fouhy almost 15 years
    The first time you run it, it will just be a matter of copying all your data. The whole point of rsync is that subsequent runs will be faster because it only needs to transmit changes. Try running it again now -- nothing much should have changed so it should be very quick. If it's not then you may have network issues.
  • swilliams
    swilliams almost 15 years
    Right, I was just worried because I had already copied all of the data over before (when it was on my prior PC workstation). I just ran it again and it completed quickly. Thank you.
  • Jonathan Garber
    Jonathan Garber about 11 years
    Greetings, Michael, welcome to SuperUser. Generally speaking, it's best to add at least a short summary of content you link to, just in case the links ever go down. Could you edit your answer to include a little of the information you link to?