Solved

Error Check before deleting folder

Posted on 2013-01-31
2
201 Views
Last Modified: 2013-02-01
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?

#!/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 

Open in new window


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

Open in new window

0
Comment
Question by:jwmcf1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
ThomasMcA2 earned 500 total points
ID: 38840549
Check the return code from the copy, and only delete when there are no errors:

cp --parents --preserve -a "$1" /mnt/dr/export/$(hostname)/$todaysdate

if [ "$?" = "0" ]; then
        rm "$1"
fi
0
 

Author Comment

by:jwmcf1
ID: 38844604
well that was easy! thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange html entity encoding by tidy 14 100
Unix/bash: scripted arithmetic 13 102
How to run git alias with shell other than sh 17 135
Powershell to pull all Service accounts 1 78
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question