I've been trying to solve relatively small problem with moving some files across FTP servers but no luck so far.
In a nutshell this is what I'm doing, I have three servers:
SourceSFTP TargetSFTP Target_2_SFTP
The script is supposed to do the following
- Connect to SourceFTP
- Grab all files
- Loop through files
- Call function that takes file as parameter and does stuff to it, let's call it postfunc()
- Drop the files to TargetSFTP
The problem occurs when inside postfunc I put another call to lftp to transfer file to Target_2SFTP. The command is executed properly (I can see the file moved) but then the number 5 never happens.
This is the script I have:
function postfunc() {
the_file=$1
lftp<<END_SCRIPT2
open sftp://$Target2SFTP
user $USERNAME $PASSWORD
cd /root
put $the_file
bye
END_SCRIPT2
}
echo "Downloading files from $SOURCE_SFTP"
lftp -e "echo 'testing connection';exit" -u $SOURCE_USERNAME,$SOURCE_PASSWORD $SOURCE_SFTP
lftp -e "set xfer:clobber true;mget $SOURCE_DIR*.csv;exit" -u $SOURCE_USERNAME,$SOURCE_PASSWORD $SOURCE_SFTP || exit 0
files=(*.csv)
batch=10
for ((i=0; i < ${#files[@]}; i+=batch)); do
commands=""
# Do some stuff
for((j=0; j < batch; j+=1)); do
commands=$commands"mv source_dir/${files[i+j} archivedir/${files[i+j]};"
postfunc ${files[i]}
done
echo "Archiving batch..."
lftp -e "$commands;exit" -u $SOURCE_USERNAME,$SOURCE_PASSWORD $SOURCE_SFTP
lftp<<END_SCRIPT
open sftp://$TARGET_SFTP
user $TARGET_USERNAME $TARGET_PASSWORD
cd $TARGET_DIR
mput dirr/*
bye
END_SCRIPT
done
Hopefully I'm missing something obvious... At the moment even if I move one file "Archiving batch" never shows up, if I remove contents of postfunc() everything executes correctly