Backup to your Synology NAS with rsync


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:
sudo su

Go to the home directory of the rsync user that we created earlier:
cd /volume1/homes/rsync

Create a folder that will be used to store SSH key generated information:
mkdir .ssh

Give it the correct ownership:
chown rsync:users .ssh

And the correct permissions:
chmod 711 .ssh

Enter the .ssh folder:
cd .ssh

Create an empty file called authorized_keys:
vim 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:
ssh-keygen

Follow the instructions to have what I like to call a key and keyhole generated.
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:
cd .ssh
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!

Have any Question or Comment?

7 comments on “Backup to your Synology NAS with rsync

pyrho

I also had to modify some sshd configuration and change the permission on the home directory of the newly created user.

Reply
GnaXi

Hi Pyrho,
Thank you for your comment!

Could you elaborate on what changes you did to the sshd configuration and what level of permission change you did to the home directory of the user, to help out others that might experience issues following my guide?

Reply
PuffyThePirateBoy

Had some issues too. To be honest, I am not sure what exactly was the final factor. But I updated /etc/ssh/sshd_config and uncommented “#PubkeyAuthentication yes”. Then I restarted the SSH service by just turning it off then on again.

Additionally changed the permissions where the final row eventually got things going:
chmod 700 /volume1/homes/rsync/.ssh
chmod 600 /volume1/homes/rsync/.ssh/authorized_keys
chmod 755 /volume1/homes/rsync

Reply
GnaXi

Thanks for sharing! Might help others that are stuck too! 🙂

Reply
Dylan

I’d like to know this aswell tho..

Reply
Big Eric

Is it possible to backup a Linux device to Synology NAS where the Linux machine is passive? Linux server in a DMZ and Want to backup and restore without the need to provide Linux box with authority to punch through firewall or coding Synology credentials on Linux box.

Reply
GnaXi

You could schedule running a custom script from DSM on your Synology.
An example would be:
rsync -avz --rsh='ssh -i/volume1/SHARENAME/SSHKEYFILE -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' [email protected]:/ /volume1/SHARENAME/BACKUPFOLDERNAME

(Credit for this answer goes to: anders.io)

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *