Brief:
I have a directory where files are dropped arbitrarily in server A. I want to dump these files into server B and delete them from server A.
Background:
I would like write an Bash script where the code iterates through each file, put
s the file across to server B, and deletes it straight after. This will be ammended to the cron
and called hourly to scrape the files across to server B
Code:
The code I have attempted is:
#!/bin/bash
MY_SCRIPT_NAME=`basename "$0"`
PATH_TO_METRICS='/home/some/directory'
if pidof -o %PPID -x $MY_SCRIPT_NAME > /dev/null; then
echo "$MY_SCRIPT_NAME already running; exiting"
exit 1
fi
sftp -i my_priv_key -oPort=12345 [email protected] <<EndOfTransfer
cd $PATH_TO_METRICS
for filename in $PATH_TO_METRICS; do
PUT "$filename"
rm "$filename"
done
EndOfTransfer
Relevant Research:
- Execute command in sftp connection through script
- FTP file transfer, loop through a directory and copy old files
Question:
How does one iterate through files in a given a directory and use sftp to put each of the files across to another server, and delete the file if a successful transfer has happened?