Backing up Raspberry Pi to Synology NAS

With what could have happened 2 weeks ago if I wouldn’t have had a good backup plan, I realized that I had to do something about those Raspberry Pi’s that have started invading our house and how I’m backing up the code running on those small Linux computers.

Cause what happens in practice is that you open a telnet/ssh session on the RPi, start coding, debugging & testing until it works. And by the time you’re finished with that and made sure that everything starts automatically after a reboot etcetera, you close the session and you’re done – on to the next adventure; right? Wrong.

This leaves all the code on the RPi, on a 4GB SD card with no backup at all… sounds dangerous! I just had to do something about that, before I would lose something.

The first thing that popped up in my mind was rsync, a tool to sync files and directories to another machine. And since I have 2 Synology NAS devices, I thought it would be smart to backup my RPi’s to one of those instead of making images of the SD cards. Cause the latter option would result in downtime each time I want to do a backup; yuck. So rsync it will be.

All the examples I found that worked with rsync made use of ssh, so that was the first hurdle I had to take – I’ve never done that much with public & private keys, key generators and so on. But with some help I managed to get things going. First you have to generate a key with ‘ssh-keygen -t rsa‘ on the NAS. The resulting file with the public key (default file name id_rsa.pub) has to be added to the authorized_keys file on the RPi. Normally you should be able to use ssh-copy-id command for that, but for some reason that didn’t work on my NAS, so I had to manually copy the file to the RPi and do a ‘cat id_rsa.pub >> authorized_keys’ to make that happen.

After that I could ssh from my NAS to the RPi without the need of entering a password (which is needed for the script to run unattended!).

Ok, next item on the list was making a rsync script.

After some tries I came up with this:

/usr/syno/bin/rsync -avz --delete --exclude-from=/volume1/backup/_scripts/rsync-exclude.txt -e "ssh -p 22" root@raspdev.hekkers.lan:/ /volume1/backup/raspdev/ >> /volume1/backup/raspdev_backup.log 2>&1

And it worked! In about 10 minutes the rsync script completed its work:

RPi backup on Synology

Last but not least, scheduling the backup job. It seems that since DSM 4.2 the Synology Task Scheduler has an option to run user-defined scripts. Just what I need:

Synology Task Scheduler

Cool.. so now my ‘master’ Synology NAS (a DS209) takes care of backing up the RPi and 1 hour later a Network Backup job makes a copy to my other NAS (DS109) ; so now I have 3 versions – the original and 2 copies. Try to beat that, Mr. Murphy! 😉

It’s roughly the same way how I backup my Windows servers – robocopy copies the important directories to the first NAS and Network Backup does the rest.

Now some statistics on how rsync is performing. Here’s a part of the initial rsync backup log:

sent 972675 bytes
received 1363869597 bytes
2347106.23 bytes/sec
total size is 1360002151 speedup is 1.00

It does take some processing power to perform the back up though:

Backup process load

70% CPU for process-id’s 3914 and 3921 (3rd and 4th line), that’s quite a lot. Removing the ‘z’ option (=compression during transfer) from the rsync command resulted in lower CPU usage and I might try to make the backup somewhat ‘nicer’ to reduce the CPU usage even more – backup is not that time critical but other processes running on the RPi might be… I’ll have to do some tests to see what’s best practice.

Tagged . Bookmark the permalink.

16 Responses to Backing up Raspberry Pi to Synology NAS

  1. OWK says:

    Good work, like what you came up with.

    I’m using rpi-clone (https://github.com/billw2/rpi-clone), but it does only back-up to a SD card inserted in a USB card reader connected to the rpi and is manually initiated. That results in a complete image on SD that is bootable.

    Three questions if I may:
    Are already stored back-up files overwritten?
    How do you restore a bootable SD card in case Murphy struck again?
    Any chance this procedure works on Qnap TS209 as well? As far as I know there is no task scheduler, so that is an issue…

    Please keep us posted with progress on this subject!

    Regards
    OWK

    • Thanks and yes, you may.. 😉

      1. Only if they’ve changed.
      2. The idea is to write a fresh image to the SD card and overwrite that one with the backup.
      3. If you can provide me the QNAP TS209, then…?

      To be continued…

  2. Wouter Martens says:

    Thanks for the extremely helpful information ;-).

    I am just wondering though… what did you put in your “rsync-exclude.txt” file?
    Would you mind sharing it?

    Thanks in advance ;-).
    Kind regards.

  3. Robin says:

    Well done written documentation. It was the first script that worked for me.
    It would be nice, if you could write a restore documentation for this. Esspacially for the RPi from the scratch (blank SD-Card). On the internet you see thousands of backups guides but no restore guides.

    • Robin,
      Thanks for the feedback. Yeah I know, I should do that – some day, when I find the time, and have a spare RPi laying around, cause they’re all in use right now keeping the house up and running 😉

  4. Simon says:

    Actually this isn’t working so good for me. It’s been 2 and 1/2 hours and the file is coming close to 10GB and it’s not complete yet. Which I find weird when my SD card is only 8GB. Will try again before going to bed but I find this very strange.

    Thoughts?

    • Strange, never had this issue. I’m backing up 5 Rpi’s to my NAS without any problem.
      Did you use the exclusions? (they’re in the comments IIRC)
      Not excluding those might give some strange results…

      • Simon says:

        Yes, I did include them.

        It turned out my NAS was also backing up itself!

        I just tried it again and now it worked as a charm 🙂 Changed the options a bit though.

        /usr/syno/bin/rsync -aEv --delete --exclude-from=/volume1/backup_hdd1/rsync-
        pi/rsync-exclude.txt -e "ssh -p 22" pi@192.168.0.103:/ /volume1/backup_hdd1/rsyn
        c-pi/backup >> /volume1/backup_hdd1/rsync-pi/rpi_backup.log 2>&1

        So to the people who is having its NAS at 99% CPU load and it’s taking more than 20 minutes (mine took 15), you should probably read the logs to see what is actually happening. I also ran su before my backup, but I don’t think that was the problem the last time though.

        My backup folder is about 2GB. My build is clean except for OwnCloud (and stuff that comes with it such as PHP, MySQL etc.).

        Thank you, Robert!!

  5. Simon says:

    And a few questions on top of that (as I’m fairly new to UNIX):

    – Can I save the script with the file extension .sh?
    – Wont the script require a password each time it is run? (as it does when manually running it)

    Thanks again!

    • 1) Yep, my scripts all have the .sh extension.
      2) Nope.. I always run the script from a telnet session the first time, after that it just runs without any problem. I don’t even know if it’s really required to do it this way, but I like to check the script before I let it run unattended.

  6. Simon says:

    Just wanted to give some more tips about this. At first my Task Scheduler (DSM 5.0-4493) wasn’t running my script, but it was working from terminal (after entering password).

    What I did:
    – In Task Scheduler, added sh</code before the path to the actual script in the User-defined script textare, so it looks something like this sh /filepath/to/backup-script.sh. Weird, I know.
    - Made it possible for my DS to access my RPi via SSH without a password. A great guide from the RPi team right here!.

    Again, thank you Robert. This is truly awesome 😀

  7. Simon says:

    Just wanted to give some more tips about this. At first my Task Scheduler (DSM 5.0-4493) wasn’t running my script, but it was working from terminal (after entering password).

    What I did:
    – In Task Scheduler, added sh before the path to the actual script in the User-defined script textarea, so it looks something like this sh /filepath/to/backup-script.sh. Weird, I know.
    – Made it possible for my DS to access my RPi via SSH without a password. A great guide from the RPi team right here!.

    Again, thank you Robert. This is truly awesome 😀

  8. Pingback: Aufsetzen Fhem und Kodi auf Raspberry Pi | Xiberger's Blob

  9. Amz says:

    Works perfectly… thanks for the article.

Leave a Reply

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