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