Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix script to move data

Status
Not open for further replies.
Mar 25, 2009
2
US
Hi All,
I am new to unix shell scripting. I want to write a script that can move my log files daily from my servers about 5 of them to a storage server. The script should move only today's log files from a log dir to the storage server. I want the script to run as a cron job from one main server. This script should be able to connect to all the other servers using ssh and then scp the files to my storage server.

On the storage server the log files should be moved to different directories that I have created on the storage server. Before moving the files to these directories the script should first delete all the log files already in the dir which are older than 30 days.


thanks
 
This is the short script I have so far

#!/usr/bin/sh

for i in 01 02 03 04 05

do

ssh test@hostname-$i "cd /var/log; find /var/log -mtime +1 -exec scp storage_server"

done
 
Good start, but it won't result in a valid scp command. Try this:

Code:
ssh test@hostname-$i "cd /var/log; find /var/log -type f -mtime -1 -exec scp {} storage_server:/destination/directory \;"

The filenames that are found are inserted in the position of the {} marker before executing the command, and the command needs to be terminated by an (escaped) semi-colon. I also added a -type f so you don't end up inadvertently copying directories, and changed the mtime from +1 to -1 (i.e. files less than a day old; +1 means more than a day old).

Have you cnosidered setting up a central logging host instead (see man syslog)?

Annihilannic.
 
You should also use full path to scp or any other executables when using cron.

A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top