If you, like me, have a Synology NAS at home, you might want to offload some files from your other servers as a backup.
This might come in handy, if your server hosting provider takes extra for backup, then why not just backup to your own home and save yourself a couple of bucks!
In this article I will be explaining step by step, how you can perform a backup task using rsync from your external Linux server (Debian Stretch in this case) to your Synology NAS automatically and on a schedule.
This will require you to fiddle with your Synology NAS via SSH. If that makes you uncomfortable, this guide is not going to be for you.
You will also need to know how to setup port forwarding to allow access to your NAS.
First step will be to enable rsync on your Synology NAS:
- Go to the Web interface and continue to the Control Panel
- Go to File Services and choose the rsync tab at the top
- Put a check mark in Enable rsync service (I would highly recommend generating a random port number for rsync activity to increase security)
- Press the Apply button
- A new Share will be created automatically called NetBackup (This is where your backups will be stored)
Second step will be to create a new user on your Synology NAS to use specifically for rsync:
- Once again go to the Web interface and continue to the Control Panel
- Go to User and press the Create button to create a new user
- Call the user rsync (or whatever you would prefer to call the user) and press Next
- The user does not need to be part of any special group, so just put it in regular users group and press Next
- Now find the newly created share (NetBackup) on the list and give the user Read/Write access to it and press Next
- User quota settings only matters, if you would make sure that it can only take up a certain amount of space. If that doesn’t matter to you, press Next
- When assigning application permissions, find rsync (Shared Folder Sync, LUN Backup on supported models), allow it and press Next
- User Speed Limit Settings shouldn’t matter either and you can press Next and lastly Apply to create the user
If you have not set up home folders for your Synology users, we will have to enable it by going to The Control Panel -> User -> Advanced. Then Scroll to the bottom and put a check mark in Enable user home service. This is going to be important because of what we’re going to do next.
Now, enable SSH access to your Synology NAS. If it isn’t already active go to: Control Panel -> Terminal & SNMP -> Enable SSH service (As with rsync, I would recommend using another port than the default 22)
Connect to your Synology NAS using your favorite SSH client (I prefer PuTTY myself) and the administrator credentials.
Type the below command to start working as root. While using this account, please be careful with what you type:
Go to the home directory of the rsync user that we created earlier:
Create a folder that will be used to store SSH key generated information:
Give it the correct ownership:
chown rsync:users .ssh
And the correct permissions:
chmod 711 .ssh
Enter the .ssh folder:
Create an empty file called authorized_keys:
To exit and save the file enter:
:wq and press Enter
Change the owner of the file:
chown rsync:users authorized_keys
Change the permissions:
chmod 711 authorized_keys
Let’s get started on the external server!
SSH to the external server with an appropriate user that has access to the content that you would like to back up and use the following command:
Follow the instructions to have what I like to call a key and keyhole generated. Do not set a passphrase, as the passphrase will then be needed in the rsync command.
The key is named id_rsa and the keyhole is named id_rsa.pub. They are put into your currently logged on users .ssh folder, which you can access by entering:
The content of the id_rsa.pub file has to be copied to your authorized_keys file on the Synology NAS, to allow the external server to rsync its content.
When this has been done, we are ready to try and perform an rsync from the external server:
rsync -av --delete -e "ssh -i /home/[USER]/.ssh/id_rsa -p [RSYNC PORT ON YOUR SYNOLOGY NAS]" [/home/user/important/stuff] [email protected][DOMAIN.TLD]::NetBackup/
Replace [USER] with the user of your external server
Replace [RSYNC PORT ON YOUR SYNOLOGY NAS] with the port that you went with for rsync on your Synology NAS
Replace [/home/user/important/stuff] with the directory that you would like to back up
Replace [DOMAIN.TLD] with the address to your Synology NAS
If you do not wish for the rsync to delete backed up files on the Synology NAS, when they are deleted on the external server, just remove –delete from the command
If you would like to have it run automatically, you can setup a cronjob on your external server, for example:
0 0 * * * rsync -av –delete -e “ssh -i /home/[USER]/.ssh/id_rsa -p [RSYNC PORT ON YOUR SYNOLOGY NAS]” [/home/user/important/stuff] [email protected][DOMAIN.TLD]::NetBackup/
I’d love to get your feedback if this did or didn’t work for you!