Automatically Backing Up Remote Servers (e.g Web) with Qnap 109 NAS
Contents
Introduction
If you run an important server such as web server you should of course take regular off site backups. Now everyone knows that backups are a total pain to do manually and more often than not get forgotten. However, using linux cron you can automate this task easily and be more confident of your ability to recover should anything go wrong with your server and you need to restore. If you have a decent connection speed at your home or office you can program a QNAP 109 NAS (network attached storage) to take care of this important task for you.
Backing up like this has the following benefits
-It is off-site (assuming your qnap is at your home or office and the server you are backing up is in some data centre / ISP somewhere) -It "pulls" the backups from the webserver. Keeping your qnap device behind a natted firewall means it is harder to be compromised as it will simply connect to the remote server then pull down a backup and then logoff. If you "pushed" the backup from the remote server to a remote host, most likely you would have to store some connection info and credentials on the remote server. This could be given away should your remote server be hacked. This means your server would be hacked and potentially your backups too. Nasty! -Qnaps are very quiet and don't use much power. So rather than using a PC, that could technically do the job just as well, you can leave the qnap on 24/7 without it being too noisy or costly on electricity.Install the QPKG
The QPKG is the qnap package manager that lets you expand your qnap's linux functions and software. We are going to need to replace the default Secure Shell that comes with the QNAP 109 with the OPENSSH package. The following link describes this process: http://wiki.qnap.com/wiki/Install_Optware_IPKG#Automatic_installation_via_QPKG Replace the default qnap SSH The following link describes this process. http://wiki.qnap.com/wiki/How_To_Replace_SSH_Daemon_With_OpenSSHPrepare the Qnap and your remote server for Automated Backups
Create a user on your qnap an on your remote server Choose a username and create that user on both your qnap and your remote server in my example I am going to use the name "dave" Login to the qnap as admin and run this command: Add the user and set his home directory and password:adduser -h /share/HDA_DATA/dave dave
adduser -h /home/dave dave
passwd dave
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Your identification has been saved in /share/HDA_DATA/dave/.ssh/id_rsa. Your public key has been saved in /share/HDA_DATA/dave/.ssh/id_rsa.pub.
scp id_dsa.pub dave@www.myserver.com:/home/dave/.ssh/id_rsa.pub
cat /home/dave/.ssh/id_pub.pub >> authorized_keys
ssh dave@www.myserver.com
drwx------ 2 dave dave 4096 May 14 18:14 .ssh
Example Backup Scripts
Now you can run backup scripts which can backup mysql databases and files as cron jobs. As the qnap can login remotely without any need for a password to be entered cron jobs will succeed (they would fail if the cron scripts expected you to manually put in a password). The following is an example script which you might want to run daily on your qnap that would backup a website directory. Naturally you need to alter some of the paths to suit your own environment.#!/bin/sh BACKUPDATE=`date +%Y-%m-%d` #Optionally change mysitebackup to something which represents your server. TARARCHIVE=mysitebackup.tar #Optionally change the DATA to a path that represent your webserver (or any server you want to backup) DATA=/var/www/html/mysiterootdir/ #==== Backup ==== # Create Tar. This will create a tar archive file which has the backupdate prefixed to the filename. This is the file you will securely copy off site to your Qnap. ssh dave@www.myserver.com "tar -cvf $BACKUPDATE.$TARARCHIVE.tar $DATA" #Zip Tar. Now you have tarred your files zip them up to speed up transfer and use less bandwidth. ssh dave@www.myserver.com "gzip $BACKUPDATE.$TARARCHIVE.tar" #Copy backup file to backup device scp dave@www.myserver.com:/home/dave/$BACKUPDATE.$TARARCHIVE.tar.gz /share/HDA_DATA/dave/backups/files/ # Optionally send a SSH command to delete the backup tar from the remote server. (now you have it on your qnap you don't need it on your server taking up space) ssh dave@www.myserver.com "rm -r -r /home/dave/$BACKUPDATE.$TARARCHIVE.tar.gz"
Waht about rsync?
rsync should do it
Copy public ssh key to targets authorized_keys
useful info
power
Pretty Quiet