nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Require shell script using rsync

This is a discussion on Require shell script using rsync within the Shell scripting forums, part of the Development/Scripting category; Dear All. I require a shell script using rsync. I have two Linux servers both are identical. I want to ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 04-22-2006, 12:40 AM
Member
User
 
Join Date: Mar 2006
Posts: 63
Rep Power: 0
puppen
Default Require shell script using rsync

Dear All.

I require a shell script using rsync. I have two Linux servers both are identical. I want to copy data from one server to another server in every 1 hour using rsync and I want to put in Cron Script.

I tried this but when I do rsync it asks for password even I give --password-file option but no luck.

Example :- rsync -avz server1:/mail server2:/mail
I wan to put this in cron scheduler , so it copy all data every day in a gap of one hour and this script should start every day after 6: PM and stop at 2 AM .

Regards.
Puppen
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-22-2006, 01:41 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Replace your rsync command (assuming that you want to sync server1 with local server2)

Code:
rsync -avz server1:/mail server2:/mail
with (type command on server2)
Code:
rsync -avrR --links --rsh=/usr/bin/ssh server1:/mail :/mail
To avoid password prompt you need to setup ssh-keys, so on server2 type following commands:
Code:
ssh-keygen -t rsa
ssh you@server1 "mkdir .ssh"
scp .ssh/id_rsa.pub you@server1:.ssh/authorized_keys2
Now you can login to server1 from server2 without password. Now setup cron job using following script (save script on server2):
Code:
#!/bin/bash
MASTER="server1"
DIR="/mail"
LDIR="/mail"
SSH="/usr/bin/ssh"
rsync -avrR --links --rsh=$SSH $MASTER:$DIR $LDIR
Cron job
Setup cronjob using following syntax:
Code:
1 18,19,20,21,22,23,0,1,2 * * * /path/to/script.sh
Script.sh will run at 6:01pm, 7:01pm, 8:01pm ... 2:01am

Read following docs for more info:

How do I add jobs to cron under Linux or UNIX oses
SSH Public key based authentication - How-to?
How do I sync data between two Load balanced Linux/UNIX servers?

if you have more question just reply back and we will be glad to assist you
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 04-22-2006, 03:38 AM
Member
User
 
Join Date: Mar 2006
Posts: 63
Rep Power: 0
puppen
Default

Dear

Thanks a bunch for reply.
But I am confused between 2 servers and which command runs on which server.
Currently what I am doing is,
I am running this command on server2 where I am taking back up i.e on server2
rsync -avz server1:/mail /mail
the above command copy files from server1 to server2

One more thing I have so many dir in mail folder but I do not want to take backup of all dir except certain dir. in this case what should I do/

Regards.
Puppen
__________________
someone somewhere is made for you.
LOVE is Journey not a destination.
ALL I want is EVERYTHING.
Reply With Quote
  #4 (permalink)  
Old 04-22-2006, 12:28 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Quote:
One more thing I have so many dir in mail folder but I do not want to take backup of all dir except certain dir. in this case what should I do/
Use --exclude option

Code:
rsync -avz --exclude '/mail/rocky' server1:/mail /mail
Above command will not include /mail/rocky directory. If you have lots of dir then you can store them in file. For example:

file.txt
Code:
/mail/rocky
/mail/xyz
/mail/dddd
And your command should be as follows:
Code:
rsync -avz --exclude-from file.txt server1:/mail /mail
Hope this helps
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #5 (permalink)  
Old 04-23-2006, 01:52 AM
Member
User
 
Join Date: Mar 2006
Posts: 63
Rep Power: 0
puppen
Default

Dear All
Thanks a bunch, Ya I have got it. This script works properly.

regards.
Puppen
__________________
someone somewhere is made for you.
LOVE is Journey not a destination.
ALL I want is EVERYTHING.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
rsync pls help linuxmanjusha Web servers 1 10-23-2007 12:12 PM
writing a shell script to find out my shell name jaymob123 Shell scripting 1 10-08-2007 01:36 AM
rsync folder zafar466 Shell scripting 1 04-28-2007 04:28 AM
Require Shell Script Which sends all server info on mail puppen Linux software 3 10-25-2006 09:07 PM
require shell script puppen Shell scripting 4 04-12-2006 10:42 PM


All times are GMT +5.5. The time now is 08:59 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36