jwmcf1
asked on
Error Check before deleting folder
I have a simple bash script that copies files from the local machine to a remote machine on a monthly basis for the purpose of backing them up. The host is SLES 11 and the remote backup machine is a Windows 2008 Server.
The script is straight forward: mount the remote directory on the Windows Server then copy the contents of a local folder to the remote server. This works great but I then have to manually delete the local files after I have been verified they were copied.
So to remove the need to "continually" manually check; I would like to implement some form of error checking into the script to insure the files have been copied and then after verifying, delete the local files.
What would be the easiest way to verify that the copy has run successfully and if the copy did not run at all or did not copy everything then DO NOT delete the local files?
My thoughts are to compare the local and mounted remote directory for size and number of files and if they do not match then do not delete. I new to bash scripting - I was think maybe the diff command and du could be used to accomplish this?
Is there an easier way to go about this?
export-copy script
The script is straight forward: mount the remote directory on the Windows Server then copy the contents of a local folder to the remote server. This works great but I then have to manually delete the local files after I have been verified they were copied.
So to remove the need to "continually" manually check; I would like to implement some form of error checking into the script to insure the files have been copied and then after verifying, delete the local files.
What would be the easiest way to verify that the copy has run successfully and if the copy did not run at all or did not copy everything then DO NOT delete the local files?
My thoughts are to compare the local and mounted remote directory for size and number of files and if they do not match then do not delete. I new to bash scripting - I was think maybe the diff command and du could be used to accomplish this?
Is there an easier way to go about this?
#!/bin/bash
todaysdate=$(date +"%Y-%m")
echo $todaydate
umount /mnt/dr
mount -t cifs -o username=xxx,password=xxxx //backupsrv/E /mnt/dr
mkdir /mnt/dr/export/$(hostname)/$todaysdate
echo "Starting copy..."
cd /data/localexport
find . -mtime +30 -exec /usr/local/bin/export-copy {} \;
echo "Finished copying."
echo
cd /
umount /mnt/dr
export-copy script
#!/bin/bash
todaysdate=$(date +"%Y-%m")
echo "$1"
if [ -d "$1" ]
then
echo "This is a directory and will not be processed"
else
echo "Copying file..."
cp --parents --preserve -a "$1" /mnt/dr/export/$(hostname)/$todaysdate
#rm "$1"
fi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER