I have written two scripts to transfer files to another server outside the company.
One is a batch script , and the other script calls the batch script, send the files and archive the file sent.
The problem is, that I want to get the list of files which have been uploaded the the other server, in order to be sure that all files have been transferred.
Below is the batch script, and the other script.
Please, would you take a look and give any suggestion to improve the performance of these scripts?
Thank you in advance!!
batch-script:
UploadFile=$1
ServerSftp=$2
echo put $UploadFile > /tmp/batch-sftp$$.log
# Send the file throught SFTP as Batch
sftp -b /tmp/batch-sftp$$.log $ServerSftp
# Remove log file
rm -f /tmp/batch-sftp$$.log
push script:
$data = `date +%c`;
chomp($data);
# get the list of files.
@files = </usr/local/xxx/yyy/zzz/*>;
foreach $file (@files) {
# Send each file with SFTP
$sftp_put = `/usr/local/scripts/batch-sftp.sh "$file" username\@192.168.xx:xx/upload 1> /dev/null; echo \$?`;
# clean out-put from newline
chomp($sftp_put);
if ($sftp_put eq "0") {
$move_archive = `mv "$file" /usr/local/archive/ > /dev/null; echo \$?`;
chomp($leviz_archive);
if ($move_archive == "0") {
$write_logs = `echo \"$data - OK - "$file" was sucessfully uploaded\" >> /usr/local/logs/outgoing.log`;
$write_logs = `echo \"$data - INFO - "$file" was sucessfully moved to /usr/local/archive/\" >> /usr/local/logs/outgoing.log`;
} else {
$write_logs = `echo \"$data - ERROR - "$file" cannot be moved to /usr/local/archive/ : "$move_archive"\" >> /usr/local/logs/outgoing.log`;
}
} elsif ($sftp_put ne "0") {
$write_logs = `echo \"$data - ERROR - "$file" cannot be uploaded : "$sftp_put"\" >> /usr/local/logs/outgoing.log`;
}
}
Why not use sftp's capabilities like "abort on error" or "raised logging level" ( man sftp )
to make sure all files have been transmitted correctly instead of that cumbersome later analysis? Create the batchfile carefully, and then check the logfile for anomalies.