rsync backup into Synology NAS
I love my Synology. Specially the Download and Audio station apps. SickRage is awesome as well. But when in comes to the main purpose of a NAS, making backups is F.R.U.S.T.R.A.T.I.N.G.
Time Machine backups work pretty well⌠until they fail and you're told to start anew. Which is about once every 3 or 4 months. No, it's not a quota issue. Not a permissions issue either. It stops working without reason and OS X tells you to create a new backup from scratch. This makes any backup system unreliable.
DS Cloud official app is another solution, but as far as I know it only accepts one source folder (ala Dropbox). This lacks any kind of flexibility.
rsync to the rescue! I feel dirty owning a Synology and having to resort to rsync to make proper backups, butâŚ
Setting up Synology for network backups
Basically, we will be opening network connections to a specific user for a specific volume in the NAS.
Enable network backup service
Tick the Enable Network backup service box, found in Main Menu > Backup and Replication > Backup Services > Network Backup Destination. This action creates a NetBackup shared folder.
rsync user
Create a rsync user and configure the following three tabs:
User groups: rsync should be in the System default group.
Permissions: rsync should be granted just for read-write access to the NetBackup shared folder.
Applications: rsync should be allowed only for the NetWork Backup Destination.
Fixed IP
To make things easier, disable DHCP in Control Panel > Network > Network Interface. This IP will be used inside the bash script described below.
Setting up OS X to rsync data to NAS
Now we have the NAS ready we will setup a rsync for each of the source folders we want to backup, and then make the system run that again on a daily basis.
List of folders to be backed up
Create a file that will be your list of folders to be backed up. The file is named after your user name in the system. To get that name, just open a terminal and write id -un. If the username was username the file would be username.txt. Its contents can be:
delete,/Users/username/.atom delete,/Users/username/.bash_profile delete,/Users/username/.config delete,/Users/username/.gitconfig keep,/Users/username/Documents/projects delete,/Users/username/Documents/Dropbox
As you see, each line has two parts, a keyword and a path. The keyword is either delete or keep. This lets me configure if I want to rsync --delete-during option or not for that specific path.
Please note: do not use ~/ in paths since bash will take / as a base and you would end up with something like /Users/username/~/âŚ.
And yes, paths here can have spaces, but please do not use spaces between keyword and path.
Password
In order to make it easier to automate (more on that later), we will be saving the rsync password you previously set up in the NAS into a file. We could have set it in an environment var called RSYNC_PASSWORD in your .bash_profile but that was causing problems when running the script from crontab.
So, we create a file, for instance in /Users/username/.config/.rsyncpwd and write the password there. Then set the correct permissions with chmod 600 /Users/username/.config/.rsyncpwd.
Bash script to rsync to NAS
Create a file with .sh extension. For instance, I have placed this file in /Users/username/.config/rsyncnet_backup.sh.
Now the script magic:
#!/bin/bash # Take user name from system so the script won't overwrite data # at destination for other users using the same script. USERNAME=`id -un` # save script folder to a var FOLDER=/Users/username/.config/ # path to password-file PSWRD=/Users/$USERNAME/.config/.rsyncpwd # save destination to a var [email protected].___::NetBackup/$USERNAME/ # reads a file named after your username # each line of the file represents a flag,path OIFS="$IFS"; IFS=$'\n'; lines=($(> $FOLDER/backup.log zync() { for i in "${lines[@]}" do : # each line represents a deletable flag and path to be backed up # do not use ~/ in paths since bash will take / as a base OIFS="$IFS"; IFS=$','; line=($i); IFS="$OIFS" what=${line[0]} SOURCE=${line[1]} if [ "$what" == "delete" ]; then DEL="--delete-during" else DEL="" fi echo echo echo ---------------------------------------------------- echo Syncing $SOURCE STARTTIME=$(date +%s) caffeinate -s rsync --password-file=$PSWRD -a --stats "$SOURCE" $DEST $DEL ENDTIME=$(date +%s) echo "Elapsed $(($ENDTIME - $STARTTIME)) seconds" echo done } zync ${lines[@]} echo END `date` >> $FOLDER/backup.log
The markdown here converts code very strangely, sorry.
The script is setup to log basic stats and elapsed times for each process and write the start/end date to a log file.
I recommend testing this with small folders and the --dry-run option at the beginning, then setup the correct folders once everything is working as you wish.
Scheduling the backups
Once you have run the backup script at least once, and your folders are copied into the NAS, you can schedule it for easier automation. Remember no backup system is a real backup system if you have to do it manually.
We can use iCal, launchd/plist, or crontab. Supposedly, crontab is deprecated and not recommended in OS X, in favor of launchd or running the script from iCal. I really had serious problems running the script from launchd (maybe permissions or env variables not properly set), and iCal Open file custom alert is completely borked when calendars are shared through iCloud (your iPhone cannot run bash). If there was a way to have local and iCloud calendars, this would be a no brainer though.
crontab
So I chose the crontab way.
First we will create a ~/.crontab file and include something like:
00 03 * * * /Users/username/.config/rsyncnet_backup.sh
Obviously, you need to adjust the time (the first two numbers, minute and hour) and path to your bash script.
Now we will tell cron daemon to get this file:
crontab ~/.crontab crontab -l
The last command should return you the contents of the crontab file you created, otherwise (if there was any problem) it will return you something like crontab: no crontab for username.
Your mac is now ready to run your bash script every day at 03:00⌠or maybe not just yet. Keep reading.
Wake system from sleep
If your system goes to sleep after some time of inactivity, and it is not active at the time the task is programmed, the task will not be run. Thus, we need to schedule the wake up of the system just 2 minutes before that time. Open System Preferences and under Energy Saver options you just schedule
That's it now, it-just-worksâ˘.
You have successfully setup a proper backup system to your Synology NAS.













