Backup remote linux machines to Synology

The Purpose

I use a number of Virtual Private Servers (VPS) and wanted to make a backup of the data and applications running on them.

The first step is to make a local copy of your data to a folder on the remote machine, then you can pull these files to the Synology NAS via a scheduled task. For my applications I simply used tar to backup all the directories I can about to a single file and a mysql dump to dump all the databases in the mysql server to a single file.

Setup Authentication

generate your keys


#ssh-keygen

  • do not configure with a password

Verify sshd is configured to use key files


vi /etc/ssh/sshd_config


AuthorizedKeysFile      %h/.ssh/authorized_keys

Add the public key to ssh authorized_keys


cat key.pub >> ~/.ssh/authorized_keys

Copy the private key to the Synology

Use any method you like for this. I personally simply copied the contents of the private key, then pasted it into a file on my local machine and moved it to an existing share on the NAS.

Connect to source machine from Synology and trust the source machine


chmod 400 $AbsolutePathToPrivateKey
ssh -p 22 -i $PRIVATEKEY your-user-name@server.your-domain.com

The scheduled task

Create the scheduled task

save the script below local to the Synology and make it executable

notes:

  1. You may need to enable SSH terminal access on your NAS.
  2. If you edit the script locally on a windows machine with Notepad ++ make sure you change the EOL (End of Line) to Unix


#!/bin/bash
USER="your-user-name"
SERVER="server.your-domain.com"
PORT="22"
SSHID="/volume1/backups/scripts/certificates/server.your-domain.com.privkey.pem"
SOURCE="/backups/synology/"
TARGET="/volume1/backups/server.your-domain.com/"
LOG="/volume1/backups/server.your-domain.com/backup.log"
/usr/bin/rsync -avz --progress -e "ssh -p $PORT -i $SSHID" $USER@$SERVER:$SOURCE $TARGET >> $LOG 2>&1

Run the script and verify your data is copied

References

http://raphael.kallensee.name/journal/how-to-backup-an-external-server-with-a-synology-nas-via-rsync/

Leave a Reply