Wednesday, February 16, 2011

Automatic Linux Server Backup using SSH

Example is given to backup files from hostserver to remoteserver.


---------Logging from hostserver to remoteserver without giving passwords--------

First you should create a user in both servers, in my case the username is backup_user.
I created them in both machine.

Then in hostserver, I created SSH private and Public key pairs using ssh-keygen (as given below) so that backup can be automated without inputing passwords. I did this as backup_user

#ssh-keygen -t rsa -b 1024
When you execute above command it will ask for several things, leave all them as default.
Then at one place it will ask for passphrase. At that point, without enter any password press enter key. (In other words, leave them blank)

Then you may see two files, id_rsa and id_rsa.pub in /home/backup_user/.ssh/

Now you should copy the pub key to the remote server. To do this,
execute following command :

ssh-copy-id -i /home/backup_user/id_rsa.pub backup_user@remoteserver

Now you should be able to login from hostserver to remoteserver without giving any password.



--------Making backups from hostserver to remoteserver------------------------------


**We need to create a script to make the nessary backups to backup_user's home directory with the date label.
eg: Following script will create a folder with date label and server name (eg. 20101223-hostserver).
Then it makes an archive of required folder in the newly created folder. (eg. in our case we get backup of mysql dir)

#backup script .This is saved in /home/backup_user/bak_script
mkdir /home/backup_user/`date +%Y%m%d`-hostserver
tar cfz /home/backup_user/`date +%Y%m%d`-hostserver/something.tar.gz /var/lib/mysql/xxxxx
chown backup_user:backup_user /home/backup_user/ * -R

****Then we should run this script as a ROOT level cronjob
Therefore first become as super user and execute following commands
crontab -e
and then add following

30 18 * * * /home/backup_user/bak_script
Above command says that bak_scipt has to be executed at 6.30 pm everyday.



****Following script is to copy the files to remoteserver

#script to archieve and then copy that to remoteserver. This is saved in /home/backup_user/copy_script

tar cfz /home/backup_user/`date +%Y%m%d`-hostserver.tar.gz /home/backup_user/`date +%Y%m%d`-hostserver
scp `date +%Y%m%d`-hostserver.tar.gz backup_user@192.248.8.17:/home/backup_user/backups

Then we should add a backup_user level cron to the above scipt and run that with some time after the above script
(USER level cron job)
crontab -e

and add folling things

30 19 * * * /home/backup_user/copy_script

According to this, all the files that we specified first backedup to hostserver it self at 18.30.
Then it will be transfered to remoteserver at 19.30. It is recommended to give sufficient time between copying files to server it self and copying to remoteserver.

Good luck

1 comment:

Sports Jerseys said...

Thanks a lot for the article. I really love to read such articles for you share different body of knowledge that people should know. I admire writers like you on providing great post that you dedicate your time in doing so. Thanks again and keep up the great work!