LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   automate copying files to multiple directories with log file for admin (https://www.linuxquestions.org/questions/linux-newbie-8/automate-copying-files-to-multiple-directories-with-log-file-for-admin-758831/)

simransab 09-30-2009 11:47 PM

automate copying files to multiple directories with log file for admin
 
Description: im a newly appointed system engineer taking care of linux servers. we have a new set of data coming in which need below configuration:

how to do a script with function?:
for files with ".txt" in sm
copy each of the files to folder : sm1 and sm2 (log every copy)
if succesful:
remove original
log into the log file
if not successful: (not successful copying 1 particular file to all the folders)
retain and retry
log into the log file
mail out the admin with that particular file name

I have already do try a bit:
cd /export/home/
for dir in sm1 sm2; do
cp -p sm/*.txt $dir/
done

is my starting right? how to do the rest parts?

Thank you,
Confused Linux Newbie Sabrina

paulsm4 09-30-2009 11:59 PM

Hi -

Welcome to LQ.

As with everything in Linux, you have lots of choices.

You can easily write a script - and lots of folks here would be happy to help you.

You might also consider using existing tools, such as a backup system (like "amanda") ... or rsync:

http://www.cyberciti.biz/tips/linux-...rectories.html

http://www.mikerubel.org/computers/rsync_snapshots/

http://www.scrounge.org/linux/rsync.html


Good luck .. PSM

simransab 10-01-2009 12:17 AM

hi Paulsm4,

Thanks for warm welcome.
I had tried amanda and flexbackup for other system backups. What i need on above topic is a small script which need to be inserted in one of our process application as post-script command.

Thanks,
sabrina

anotherlinuxuser 10-01-2009 01:08 AM

Here's a script to do most of what you asked:

#!/bin/bash

# Define a function to cycle log files, remember vars used in a function
# only survive until the function finishes and returns, unless the var
# name was set before calling the function.
rotate_logs ()
{
# These are one line 'if' statements:
[ -f /logdir/mycopy.log.old ] && rm /logdir/mycopy.log.old
[ -f /logdir/mycopy.log ] && mv /logdir/mycopy.log /logdir/mycopy.log.old
[ -f /logdir/mycopy.err.old ] && rm /logdir/mycopy.err.old
[ -f /logdir/mycopy.err ] && mv /logdir/mycopy.err /logdir/mycopy.err.old
return 0 # Return value is readable as "$?" after the function finishes
} # End of function

# main script starts here.
# Call the rotate_logs function. Functions must be defined before being called.
rotate_logs

cd /export/home
dir=/destination/directory

# Do a file by file copy because it easier to catch errors.
# the '`' grave accents surround a command. The results of
# the 'ls' command are used as input for the 'for' loop.
for file in `ls sm1/*.txt sm2/*.txt`; do
copyerr="" # Clear our err flag for each iteration of the loop.
# '-v' shows file names while copying, '>>' appends to log file.
# '2>&1' sends errors to the same log file as the file names.
cp -vp $file $dir >> /logdir/mycopy.log 2>&1
if [ $? -ne 0 ]; then # if '$? is not zero, an error occurred.
# Log the error
echo "Copy of $file failed, retrying..." >> /logdir/mycopy.err
# Set a flag, so we know there was an error later.
copyerr="y"
# if there was an error, you may want to attempt to find out why.
# Just wait a second for now before trying again.
sleep 1
# Try again
cp -vp $file $dir >> /logdir/mycopy.log 2>&1
if [ $? -eq 0 ]; then # if '$? is zero, copy succeeded.
# File copy succeeded on second try, so clear copyerr flag, and log it.
copyerr=""
echo "Copy of $file succeeded on second attempt, continuing..." >> /logdir/mycopy.err
else # there was an error on the second attempt.
# Just log the second error, as copyerr will still be set to "y".
echo "Copy of $file failed on second attempt, skipping..." >> /logdir/mycopy.err
fi # end of second copy attempt
fi # end of first copy error check
# If copyerr is empty, the file copy succeeded, so delete source
[ "$copyerr" = "" ] && rm $file
done # End of loop

# Now email admin
if [ -s /logdir/mycopy.err ]; then # Errors occured if file size > 0
# mail is only one of many mailers. Assumes sendmail is configured to
# send mail to the Internet. The '\' says the command continues on the next line
# The '|' pipes the output of the echo command to the body of the email.
echo -e "Errors Occured during mycopy!\nSee `hostname`:/logdir/mycopy.err for details." \
| mail -s "Email Notice" admin@somedomin.com
fi

# End of script

simransab 10-01-2009 01:48 AM

Hi anotherlinuxuser,

Thank you for the great script.
just that you got the destination and source mixed up.
i want to copy 1 file to multiple dir , not multiple dir files to one dir.

or, to be exact: copy any .txt file from 1 dir to other multiple dir

i'm trying to modify the script , can get some help?

simransab 10-01-2009 02:20 AM

I have made a simple modification on
dir=/destination/directory => file=sm/*txt
for file in `ls sm1/*.txt sm2/*.txt`; do => for dir in sm1 sm2; do

but having error:
it only copies into 1 folder, due to command rm file is in the do loop.
how to remove the files after it confirm copied to all the directories?

thanks

simransab 10-01-2009 07:09 PM

Hi anotherlinux user;

Thank you, your script helped me a lot. i managed to modify and able to run perfectly.

Thank you.

Best Regards,


All times are GMT -5. The time now is 03:04 AM.