Use SSH Keys to Automatically Backup a Linux PC to a Synology NAS!

  • Post author:WunderTech
  • Post published:July 23, 2020
  • Post last modified:June 16, 2024
  • Post category:Linux / Synology
  • Reading time:6 mins read

Today we are going to look at how you can use SSH keys and Cron to automatically back up a Linux PC to a Synology NAS.

The process will allow you to automatically authenticate from a Linux PC to a Synology NAS and schedule the job to run at the frequency of your choosing.

If you aren’t sure how to back up a Linux PC to a Synology NAS using Rsync, check out our tutorial!

Use SSH Keys to Automatically Backup a Linux PC to a Synology NAS – Instructions

1. First, we need to set up our Synology NAS so that it can use SSH keys. Make sure SSH is enabled.

enabling ssh

2. On your Linux PC, open terminal and create an SSH key by entering the command below. Use the default parameters by pressing “Enter” until the key is created.

ssh-keygen -t rsa
ssh keygen creation

3. Copy the public key to our NAS.

ssh-copy-id [USERNAME]@[IP ADDRESS]
copying ssh key

4. After the key has been copied, SSH into your Synology NAS using the terminal from your Linux PC and enter your SSH password. To SSH into your Synology NAS, the user must be part of the Administrators group. If you don’t have SSH enabled, click here to view our tutorial on how to set it up.

ssh [USERNAME]@[IP ADDRESS]
ssh into device

5. We now need to make three changes to our NAS so that our user can access the SSH key we created. If you’d like to see what permission changes we are making, you can find them here: 711, 600

chmod 600 ~/.ssh/authorized_keys
chmod 711 ~/.ssh
chmod 711 ~
changing permissions on ssh folders

6. After the permission changes have been made, “exit” the SSH session and try and authenticate again. You should not need a password.

exit
ssh [USERNAME]@[IP ADDRESS]
sshing into synology nas

Automate the Rsync Backup Command with Cron

7. The final step is to create a cron job so that the Rsync command will execute automatically. We created our Rsync command in our last tutorial and will add the same command to automate the process. Edit the cron file.

crontab -e
editing cron rules

8. Cron is VERY powerful. There are many different ways to set up this command, but I have simplified it as best as I can. In my example, I backup my home folder to my Synology NAS at 2:00 AM every night. If you’re interested in customizing this command to run at different intervals, you can see a bunch of examples here.

0 2 * * * rsync -ax ~ [USERNAME]@[IP ADDRESS]:/[VOLUME]/Backups/[PC NAME]
cron rsync command to run automatically

9. The process will now run automatically at 2 AM every night.

Conclusion

Using SSH keys and Cron to automatically backup a Linux PC to a Synology NAS is the best and easiest way that you can ensure your files are being backed up frequently. Rsync commands are very easy to write and execute, but automating them takes the process one step further and puts simplicity at the forefront. Thanks for reading! Leave any questions you might have in the comments!

WunderTech

WunderTech is a website that provides tutorials and guides on various NAS, server, networking, and infrastructure-related topics. WunderTech is maintained by Frank Joseph, an IT professional with 14+ years of experience in corporate IT and application management. He focuses on sharing his experience with others on his WunderTech website and YouTube page. Frank holds a Bachelor of Science in Computer Information Systems and a master of Business Administration (MBA).