LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > Kashif Aziz Awan
User Name
Password

Notices

Rate this Entry

Automatically Backup using rsync & crontab.

Posted 04-28-2011 at 05:53 AM by kashifazizawan

Automatically Backup using rsync & crontab.

By: Kashif Aziz Awan

kashif_khb@yahoo.com
www.kashifaziz.co.cc
November 13, 2009



Purpose:

It is used for backup from one computer to other computer automatically/scheduled.


This document has three parts:

1- Password less Authentication with ssh

2- rsync configuration

3-crontab configuration


Assumptions:-
system 1:- 172.16.0.11 [client] to whom we want to backup it.
system 2:- 172.16.0.12 [server] to where we want to backup.

1- Password less Authentication with ssh
:

check ssh service at both systems, server and client.

[root@172.16.0.11 ~]# service ssh status

If working, well if not then first install and start it.

Now check that ssh working by ssh from client to server.

[root@172.16.0.11 ~]# ssh 172.16.0.12

It prompt for passwd....ok it works.

Now we need password less ssh from client to server:
do, On client 172.16.0.11
generate ssh shared key

[root@172.16.0.11 ~]# cd /root/.ssh/
[root@172.16.0.11 ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

42:8c:96:45:7d:06:38:38:2d:6e:d5:b6:43:28:6e:53 root@172.16.0.11

[root@172.16.0.11 ~]#


just hit (enter key) til back at the prompt

it will create two files at location:
/root/.ssh/ i) id_rsa ii) id_rsa.pub
now copy the file, named id_rsa.pub from client to server at same location:

do, On Server 172.16.0.12

[root@172.16.0.12 ~]# cd /root/.ssh
[root@172.16.0.12 ~]# cat /root/.ssh/id_rsa.pub >> authorized_keys

If authorized_keys file not exist just create and do it again...

[root@172.16.0.11 ~]# chmod -R 700 /root/.ssh/*

Done!

Now try to ssh from client to server:

[root@172.16.0.11 ~]# ssh 172.16.0.12

It work password less.

2- rsync configuration
:

at both systems package “rsync” must be installed..

[root@172.16.0.12 ~]# yum install rsync


[root@172.16.0.11 ~]# rsync -azvv -e ssh /home/folders root@172.16.0.12:/home/
This will copy /home/folders from client 0.11 to server 0.12:/home/folders.

-a = archive

-v= Verbose

-z = compress form

--delete= delete folder other than match with client 0.11

3-crontab configuration


Now we need it scheduled/automatically so we need crond serves for this:-
do, On client 172.16.0.11

[root@172.16.0.11 ~]# service crond status

If working then good if not first install it by:-

[root@172.16.0.11 ~]# yum install vixie-cron

Default file:-

[root@172.16.0.11 ~]# vi /etc/crontab
SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/



# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

~
~
Type :quit<Enter> to exit Vim


1st * represent minutes 0-59

2nd * represent hour 0-23

3rd * represent day 1-31

4th * represent month 1-12

5th * represent week 0-6 o represent sunday and 1 represent monday

and so on
if you want to start rsync every 3 minutes then type these script in this.


*/3 * * * * root rsync -azvv -e ssh /home/folders root@172.16.0.12:/home/

here */3 represent that after 3 minutes this script run.

OPTIONAL:-

We can run a script file for rsync and bunzip using cronjob

for examlple create a file as

vi /etc/cron.hourly/hourly

and give permission to this file as

Chmod 755 -R /etc/cron.hourly/hourly

and add a script in this file as below:



tar -cvjf /path/zipdirectoryname$(date +%y%m%d).tar.bz2 /path/source directory

example

tar -cvjf /home/folders$(date +%y%m%d).tar.bz2 /home/folders

rsync -avzu --delete -e ssh /home/aurangzeb$(date +%y%m%d).tar.bz2 root@172.16.0.12:/home/folders && rm /home/aurangzeb$(date +%y%m%d).tar.bz2



In this file first it create a folder /home/folders in bunzip form with the latest date and then using rsync copy the folder on remote system and remove the zip file from the local pc.


Kind Regards:-
Kashif Azeez Awan
www.kashifaziz.co.cc
http://kashifaziz.4shared.com
www.apnacns.co.cc
Posted in Uncategorized
Views 8485 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration