2018-06-09

How I backup my Synology NAS using rsync

I might be a bit oldfashioned, but I prefer to use my onw backup solution on my NAS rather than relying on a tool by the supplier. I use two external Harddrives, which I exchange in intervals. To backup my content on it, I use good old rsync. Note that for this approach to work, you should turn on the "User-Home-Service", otherwise, there is no home directory for admin. To install the script, ssh to the NAS with the admin user: ssh admin@yoursyno.domain Then change to root using sudo -i. The backupscript is simple. Note that it just creates an archive on the disk. Since my target disk currently is on NTFS, I use raw rsync. At a later stage, I will change to rsnapshot. (Which requires you to install perl on the NAS first.) Without further ado, here is my script:

#!/bin/bash
# Simple Backup Script (inspried by https://zarino.co.uk/post/synology-rsync-backup/)
rsync -a -v --log-file="/var/services/homes/admin/log/backup.$(date +%Y-%m-%d-%H%M).log" \
--exclude '*@SynoResource' \
--exclude '@eaDir' \
--exclude '*.vsmeta' \
--exclude '.DS_Store' \
/volume1/data/ /volumeUSB2/usbshare/data
view raw backup.sh hosted with ❤ by GitHub
I added this in /etc/crontab to start the backup daily. Note how I use the flock command to ensure that only a single instance of the backup is running. It is important to put the lockfile in a location other than /tmp.

0 22 * * * root flock -n /var/services/homes/admin/lock/backup.lock /var/services/homes/admin/bin/backup.sh
view raw crontab hosted with ❤ by GitHub
Finally, I need to make sure that the logs do not overflow. To that end, I created this file in /etc/logrotate.d

#/etc/logrotate.d/backup_sh
/var/services/homes/admin/log/backup*.log {
missingok
}
view raw backup_sh hosted with ❤ by GitHub