Backup your data to any remote server

Reference only! Archived older discussions about backup and restore of the DiskStation to any client.

Moderators: Synology Inc, Honorary Moderator

Backup your data to any remote server

Postby pomprocker » Sat Jun 13, 2009 9:38 pm

I am going to attempt to write a guide on the wiki, but it will be developed here with your help.

When setting this up, I referenced this wonderful engadget article:
http://www.engadget.com/2007/03/21/how- ... -computer/

This involves SSH, RSYNC, DSA Keys, and Cron jobs.

Please enable SSH on your Synology device before getting started.

First requirements are to sign up with some remote free storage site. The site must have SSH enabled and RSYNC enabled.
The site I signed up with is netdojo.com. They provide me with seemingly unlimited storage.

The hardware and software involved in my trials is as follows:
My Laptop: MacBook Pro running Mac OS X 10.4.x Tiger
My NAS: Synology DS209j w/ SSH enabled
My Remote Server: Intel based Solaris 8 4/01

From my macbook I am able to SSH to this remote host using this command:
Code: Select all
ssh <username>@<fully qualified domain name>
ex:
ssh foobar@foobar.netdojo.com


Once logged in I can verify that SSH and RSYNC are installed, if you haven't asked your host provider already.
Code: Select all
> which ssh
/usr/local/bin/ssh
> which rsync
/usr/local/bin/rsync


Now doing a directory listing once logged in using the 'ls' command, it reveals that there is a 'public_html' folder. That folder is where apache looks for your web pages and makes them public. We DO NOT want to put our backups in there. Our backups with go on the same level as that folder directly in your home directory on the remote server.

Create a directory:
Code: Select all
> mkdir syno_backups


The next step involves creating keys so that when this is automated no manual password entry is necessary.

SSH into your Synology device as root. The root password is the same as the admin password.
Code: Select all
ssh root@192.168.0.xxx


Once logged in you should be in root's home directory "/root"
You can verify this by typing the command 'pwd'
If you are not, you can simply type 'cd' as root and it will place you in the home directory

Now you have to create a directory. First verify it is not already created with the unix command 'ls -la'. You should see a few files and directories starting with a dot such as '.profile'. We are looking for a directory called '.ssh' if it is not there then create it:
Code: Select all
mkdir .ssh

'cd' into .ssh and now we will create the keys.
Code: Select all
ssh-keygen -t dsa -b 2048


It will ask you for a passphrase, hit enter as no passphrase is wanted. It will then create your private and public key.
Make sure no outside users get your private key. Place your private and public key in your .ssh directory. They might be called known_hosts, and known_hosts.pub
You need to tighten up the permissions on these files to -r-x------ and make sure the ownership is root:root. You can do this using chmod and chown.

Now you have to copy your public key known_hosts.pub to your remote server. Since there is no 'scp' on the Synology linux, you could maybe copy it to one of your shared folders that is in turn mounted on your pc/laptop and then copy it to your local machine. Then you could upload it to your remote host? This could be done any number of ways, bottom line is you need to get a copy of that known_hosts.pub onto your remote host.

On your remote host you need to do the same thing above by creating a .ssh directory in the main user's home directory.
Copy the known_hosts.pub file into the .ssh directory.
Now you have to get the contents of your public key into a folder called 'authorized_keys'
Code: Select all
cat known_hosts.pub >> authorized_keys


Again make sure the permissions on these files is pretty closed down to only allow the owner (you) of the files to read and write to it.


Now the keys should be set up and we need to test this manually, we should first try rsyncing a SMALL directory
Code: Select all
rsync -avz -e "ssh -i /root/.ssh/known_hosts" /volume1/home/someuser/somefolder foobar@foobar.netdojo.com:syno_backups/


On your DiskStation this should spit out some output listing the files. If you look in your syno_backups folder on your remote server you should now see the small folder you sync'd up.

If that works we need to create a small shell script in a directory that is in roots PATH.

If you don't know where to put it make sure you understand the Linux directory layout and what all the directories are for:
http://www.google.com/search?hl=en&q=ex ... oq=&aqi=g1

You can place your script in /usr/local or /usr/local/bin or something like that. I placed mine is /usr/local/sbin.
Remember this script is going to be run by the user cron, and cron has no environment setup so you need full paths.
Code: Select all
DiskStation> vi /usr/local/sbin/backup.sh

#!/bin/sh

## Written by Me - June 2009

cd /usr/local/sbin
/usr/syno/bin/rsync -avz -e "ssh -i /root/.ssh/known_hosts" /volume1/homes foobar@foobar.netdojo.com:syno_backups/


This script will do all the home directories, to do more or other directories, you could create multiple scripts and multiple crontab entries.
Make sure the owership and permissions on your script are correct:
Code: Select all
DiskStation> ls -la /usr/local/sbin/backup.sh
-rwxr-xr-x    1 root     root          191 Jun  8 20:54 /usr/local/sbin/backup.sh



Now we need to do the crontab piece.
You need to edit the crontab with 'vi' as there is no 'crontab -e' command here.
Try to make your entry look like this (there may be other entries, but I have omitted those in my pasting):
Code: Select all
DiskStation> vi /etc/crontab
#minute hour    mday    month   wday    who     command
0       3       *       *       *       root    /usr/local/sbin/backup.sh 2>&1 >> /var/log/rsync_backup.log


If you want to know what all that means, please google for 'crontab', It will basically output everything to a log. If you don't want a log just send the output to /dev/null, or if you want it emailed to you, then you will have to install sendmail on your DiskStation.

The above command will run the script at 3am and will append the output to a log. The syntax '2>&1' just redirects stdout and stderr to the same output.

Now you need to restart your cron service:
Code: Select all
/usr/syno/etc.defaults/rc.d/S04crond.sh stop
/usr/syno/etc.defaults/rc.d/S04crond.sh start


This is a call for help:
That should be about it. Please help me improve this procedure, and my basic script. From there we can put this article on the Synology wiki.
DS209
DSM 2.3-1141

http://pomprocker.blogspot.com
Nerd Stuff - Programming, Electronics, Mechanics
User avatar
pomprocker
Knowledgeable
Knowledgeable
 
Posts: 357
Joined: Tue Jun 02, 2009 5:28 pm
Location: Mission Viejo, CA

Re: Backup your data to any remote server

Postby cruiseback » Sun Jun 14, 2009 1:03 am

Very good guide if you ask me. I managed to set a backup routine up, which has never happened before :mrgreen:

I guess you could add in some more detail, like the chmod and chown bits for example. Security really is important.

Thank you for writing it.

.cb
cruiseback
I'm New!
I'm New!
 
Posts: 8
Joined: Sat Jun 13, 2009 4:28 pm

Re: Backup your data to any remote server

Postby pomprocker » Sun Jun 14, 2009 10:14 pm

I just wanted to point out this thread as well:

viewtopic.php?f=36&t=6431
DS209
DSM 2.3-1141

http://pomprocker.blogspot.com
Nerd Stuff - Programming, Electronics, Mechanics
User avatar
pomprocker
Knowledgeable
Knowledgeable
 
Posts: 357
Joined: Tue Jun 02, 2009 5:28 pm
Location: Mission Viejo, CA

Re: Backup your data to any remote server

Postby pomprocker » Mon Jun 22, 2009 7:02 pm

To do:

Improve key-gen section (change name of keyfile from default known_hosts for security purpose)
Improve chmod/chown section
Custom SSH connection Info (may help, but makes tutorial more complex)
Only Allow rsync to use keys on remote server
Improve logging with ssh verbose and rsync -stats
Exclude file to exclude files/directories such as #recycle and @eaDir
Maybe install mail/mailx/sendmail in order to email logs. (how does the syno DSM email?)
Show how to use rsync --delete if user doesn't want incremental backups.
DS209
DSM 2.3-1141

http://pomprocker.blogspot.com
Nerd Stuff - Programming, Electronics, Mechanics
User avatar
pomprocker
Knowledgeable
Knowledgeable
 
Posts: 357
Joined: Tue Jun 02, 2009 5:28 pm
Location: Mission Viejo, CA


Return to Backup/Restore (Archived)

Who is online

Users browsing this forum: Google [Bot] and 1 guest