#!/bin/sh thisdir=`pwd` usrname='username' passwrd='password' # There are several areas to fix, the username/password above, and the # server/share name and dns name specifics below, the file to copy, and what # that file should be called on this box once the copying is successful # Correct If a file doesn't exist, or the old file is deleted first, # Behavior then the new file is copied to the desired location, during # the time the file is being copied, we get an error, the # destination file isn't created or modified, and things work # like they should. # # Problem: When doing an SMB copy from a windows share at the same time a # new file is being copied over top of an old one, the resulting # copy we get is damaged (the file is zero'ed out from the point # where the new copy started being written over the old). # # Solution: Attempt two copies of the file. If a new file is being copied # over the top of the old one, the zero'ed out area(s) will make # the resulting files different, once the two files are identical, # and larger than zero bytes, then we can be assured that the # copy was successful. get_valid_smb_copy() { echo "Attempting to retrieve wlan.ini" if [ -f temp.1 ]; then rm temp.1 fi if [ -f temp.2 ]; then rm temp.2 fi #copy the wlan.ini file from the gcam server share (windows smb copy) /usr/bin/smbclient //server/sharename -I servers.dns.entry \ -U $usrname%$passwrd -W workgrpORdomain -N -c "get file-to-copy temp.1" fn=1 smbcnt=0 while [ 0 = 0 ]; do # Nice little function to have $fn occilate between 1 and 2 fn=`expr $fn % 2 + 1` # Remove old attempt before attempting to copy it again. This # prevents problems when the file might disappear for a short # time. The copy attempt fails, but the old file is then still # left. This way, what ever is in this file after the copy # attempt is guaranteed to be the results of the most recent # copy attempt. if [ -f temp.$fn ]; then rm temp.$fn fi /usr/bin/smbclient //server/sharename -I servers.dns.entry \ -U $usrname%$passwrd -W workgrpORdomain -N -c "get file-to-copy temp.$fn" # If we're able to copy the file twice, and the destination files are identical # and they are larger than 0 bytes, then I believe we can say with certainty, # there have been no problems or errors while copying the files if [ -f temp.1 ] && [ -f temp.2 ] && [ ! -z temp.1 ] && [ ! -z temp.2 ]; then echo -n " Got 2 copies..." if ( cmp -s temp.1 temp.2 ); then echo " Copies identical" break else echo " Copies not identical" fi fi smbcnt=`expr $smbcnt + 1` echo " SMB Copy loop #$smbcnt" if (( $smbcnt == 50 )); then echo "Tried to get a valid SMB copy $smbcnt times, but failed; script exiting." exit fi # Allow some small amount of time to pass before allowing the next # attempt. Otherwise 50 failures could pile up very quickly if the # target file is deleted for a relatively short time. sleep 2 done rm temp.2 mv temp.1 resulting-file-name } get_valid_smb_copy echo "File successfully copied to resulting-file-name" exit