Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SFTP Script 1

Status
Not open for further replies.

fmuquartet

Programmer
Sep 21, 2006
60
US
Greetings!
I have revised a simple script for connecting to a remote server and zipping files and then pulling them to a local server, I now need help implement some logic to send notification if files on the remove machine aren't present.

***INFO***

ERROR when there are no files:
Archiving Metrics files

No match.

Pulling Metrics Archive

Connecting to remote-server...
Couldn't stat remote file: No such file or directory
File "/var/tmp/Activity.zip" not found.

Script:
#!/bin/bash

USERID=foobar
REM_MACHINE=remote-server
REM_FILE=/var/tmp/Eactivity.zip
LOC_FILE=/home/user/upload
ZIP=/usr/ecc/exec/utils/zip
FILE=/var/tmp/Eactivity

#######################################
#Package remote File #
#######################################
echo -e "\nArchiving Metrics files\n"

ssh $REM_MACHINE $ZIP -9r $FILE /tmp/reporting/activity*.open

#######################################
#Pulling Remote file
#in user upload directory #
#######################################



echo -e "\nPulling Metrics Archive\n"

sftp $USERID@$REM_MACHINE:$REM_FILE $LOC_FILE

echo -n


Thanks!
 
A starting point:
...
ssh $REM_MACHINE $ZIP -9r $FILE /tmp/reporting/activity*.open 2>&1 >/tmp/Eactivity.$$
grep 'No match' /tmp/Eactivity.$$ && {
#your notification here
exit 1
}
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, this seems to have helped, I guess I can added to this later.
 
However, I do have another issue as follows:

I am wanting to run remote commands with this same script as follows, but the results are not the same as running in the shell.

ssh remote-server 'find /usr/sap/reporting -mtime +0 -name "*.txt" -exec zip /var/tmp/file {} \;'

For some reason -exec is not being matched when I run via the script, I suspect its a problem with single-quotes, but I need the internal double-quotes --can someone help me here?
 
I can't see any problem with the quotes and so-on... but are you sure you want to zip all of the found files into the same destination file? Does that work? I presume it just overwrites it each time, rather than adding them to the zip.

Annihilannic.
 
Annihilannic--
Thanks for the follow-up, but when I run the command from the shell script, for some reason the zip is being omitted, as for the same zip file, I can change this later, I am just try to get the command to run successfully at this point.
 
No error messages, nothing? Weird. Try the full path to your zip binary perhaps?

Does this echo the commands you would expect it to run?

Code:
ssh remote-server 'find  /usr/sap/reporting -mtime +0 -name "*.txt" -exec echo zip /var/tmp/file {} \;'




Annihilannic.
 
Now, that the script has revolved, in terms of check if a file exist in the following format 'FILE-`date +%Y%m%d`.zip' and runs 5 minutes after mid-night, I need a way of check if the file exist before pulling a new file.

I have tried:

if [[ -f /home/user/FILE-`date +%Y%m%d`.zip ]];
then
rm -f /home/user/FILE-`date +%Y%m%d`.zip
fi

The problem here is that when the script runs 5 minutes after minutes MIDNIGHT (new day) the format 'FILE-`date +%Y%m%d`.zip' wouldn't get matched, thus NOT removing the file.

I need to remove the file after midnight from the previous day is my problem, in the given format.

Any suggestions on this?
 
Have a look here:
faq822-4802

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
have you thought of using rsync (
It uses ssh (if you want it to) and has on the fly compression as well.

it matters if you want to "clone" the directories or just store the files in zips ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top