Posts Tagged ‘Rsync backup’

How to backup linux Server remotely

Monday, September 7th, 2009

Bellow article will show how to backup a Linux server remotely by using RSYNC with public key base authentication.

To make this backup process automatic we need a password less authentication system so that we don’t have to insert username and password before backup starts.

How to create Key base authentication:

Here are the steps you need to do on the computer that acts as the SSH client:

1) Generate your SSH encryption key pair for the filecopy account. Press the Enter key each time you are prompted for a password to be associated with the keys. (Do not enter a password.)

[filecopy@bigboy filecopy]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key
(/filecopy/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in
/filecopy/.ssh/id_dsa.
Your public key has been saved in
/filecopy/.ssh/id_dsa.pub.
The key fingerprint is:
1e:73:59:96:25:93:3f:8b:50:39:81:9e:e3:4a:a8:aa
filecopy@bigboy
[filecopy@bigboy filecopy]#

2) These keyfiles are stored in the.ssh subdirectory of your home directory. View the contents of that directory. The file named id_dsa is your private key, and id_dsa.pub is the public key that you will be sharing with your target server. Versions other than RedHat/Fedora may use different filenames, use the SSH man pages to verify this.

[filecopy@bigboy filecopy]# cd ~/.ssh
[filecopy@bigboy filecopy]# ls
id_dsa  id_dsa.pub  known_hosts
[filecopy@bigboy .ssh]#

3) Copy only the public key to the home directory of the account to which you will be sending the file.

[filecopy@bigboy .ssh]# scp id_dsa.pub filecopy@smallfry:public-key.tmp

Now, on to the server side of the operation.

Configuration – Server Side

Here are the steps you need to do on the computer that will act as the SSH server.

1) Log into smallfry as user filecopy. Create an .ssh subdirectory in your home directory and then go to it with cd.

[filecopy@smallfry filecopy]# ls
public-key.tmp
[filecopy@smallfry filecopy]# mkdir .ssh
[filecopy@smallfry filecopy]# chmod 700 .ssh
[filecopy@smallfry filecopy]# cd .ssh

2) Append the public-key.tmp file to the end of the authorized_keys file using the >> append redirector with the cat command. The authorized_keys file contains a listing of all the public keys from machines that are allowed to connect to your Smallfry account without a password. Versions other than RedHat/Fedora may use different filenames, use the SSH man pages to verify this.

[filecopy@smallfry .ssh]# cat ~/public-key.tmp >> authorized_keys
[filecopy@smallfry .ssh]# rm ~/public-key.tmp

From now on you can use ssh and scp as user filecopy from server bigboy to smallfry without being prompted for a password.

2. Taking Backup by Rsync

Write a Backup script Example : backup.sh in /root directory

cd /root

vi backp.sh

Press I to Insert

then Write like this :

#!/bin/bash

DESTROOT=”/backups”
TODAY=`date ‘+%A’`

rsync -e ssh -avz –delete filecopy@smallfry:/var/www $DESTROOT/backup

#Archive todays files
tar czvf $DESTROOT/archived/${TODAY}-backup.tar.gz $DESTROOT/backup > $DESTROOT/archived/${TODAY}-backup.log

Press :wq [ to Save the file and exit]

then : chmod 700 backup.sh [ so it will make this file executable for root]

Explanation : The Script will connect to the server smallfry via Ssh, then will download all the files from /var/www directory to local pc under /backups Directory.

3. Automate the system by Crontab

crontab -e

Press I to insert
00 3 * * 1-5 /root/backup.sh >/dev/null 2>&1

Press :wq [ to save the file]

Explanation : Now crontab will execute this file First minute 3 am Every day Every month Monday to Friday .