Visit Jeremy's Blog.
Go Back > Blogs > Kashif Aziz Awan
User Name


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
November 13, 2009


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

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

1- Password less Authentication with ssh

check ssh service at both systems, server and client.

[root@ ~]# 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@ ~]# ssh

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

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

[root@ ~]# cd /root/.ssh/
[root@ ~]# 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/

The key fingerprint is:

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

[root@ ~]#

just hit (enter key) til back at the prompt

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

do, On Server

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

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

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


Now try to ssh from client to server:

[root@ ~]# ssh

It work password less.

2- rsync configuration

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

[root@ ~]# yum install rsync

[root@ ~]# rsync -azvv -e ssh /home/folders root@
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

[root@ ~]# service crond status

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

[root@ ~]# yum install vixie-cron

Default file:-

[root@ ~]# vi /etc/crontab




# 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@

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


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


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@ && 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
Posted in Uncategorized
Views 10077 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 02:42 PM.

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