LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-30-2009, 11:47 PM   #1
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Rep: Reputation: 15
Question 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

Last edited by simransab; 10-01-2009 at 01:14 AM.
 
Old 09-30-2009, 11:59 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 10-01-2009, 12:17 AM   #3
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 10-01-2009, 01:08 AM   #4
anotherlinuxuser
Member
 
Registered: Jan 2007
Location: Alberta Canada
Distribution: Fedora/Redhat
Posts: 63

Rep: Reputation: 19
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
 
Old 10-01-2009, 01:48 AM   #5
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
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?

Last edited by simransab; 10-01-2009 at 01:55 AM.
 
Old 10-01-2009, 02:20 AM   #6
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 10-01-2009, 07:09 PM   #7
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
Hi anotherlinux user;

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

Thank you.

Best Regards,
 
  


Reply

Tags
confused, linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying files and sub-directories of a directory except the directories named ".abc" sri1025 Linux - General 2 08-24-2010 08:53 AM
Copying a single file to multiple directories quest49 Linux - Software 6 12-04-2006 08:07 AM
copying multiple different files in different directories with identical file names nickleus Linux - Software 4 03-09-2006 01:25 PM
Moving or copying a directory into multiple directories oadvantage Linux - Newbie 3 03-07-2006 03:47 AM
Copying a single file to multiple directories tgolly Linux - Newbie 3 04-26-2004 03:47 PM


All times are GMT -5. The time now is 07:13 AM.

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