nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 21-04-2006, 11:40 PM
Member
User
 
Join Date: Mar 2006
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 22-04-2006, 12:41 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 244 Times in 183 Posts
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 22-04-2006, 02:38 AM
Member
User
 
Join Date: Mar 2006
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
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 22-04-2006, 11:28 AM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 708
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
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.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
  #5 (permalink)  
Old 23-04-2006, 12:52 AM
Member
User
 
Join Date: Mar 2006
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
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


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 06:53 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38