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.
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
3. Copy the public key to our NAS.
ssh-copy-id [USERNAME]@[IP ADDRESS]
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]
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 ~
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]
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
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]
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!