In the past days I had to write a script to download files from a remote server using an SFTP connection; this script will be run by cron.
The goal was to download the files on my server and delete them on the remote machine after being downloaded.
I used an array to get the list of the current files on the remote server and a for-loop to download them.
#!/bin/bash
#
# 20180801
#
# Receive files via SFTP
LOGFILE=/var/tmp/receivefiles.$(date -I).log
echo "START: $(date)" >>$LOGFILE
# Connection data
HOST='XXX.XXX.XXX.XXX'
PORT='22'
USER='mashiny'
# Where to download files
cd /home/mashiny/download
# File list
LIST_RAW="$(echo "ls -1 /out" | sftp -oPort=$PORT $USER@$HOST 2>/dev/null)"
declare -a LIST
readarray -t LIST <<<"${LIST_RAW}"
if [[ "${#LIST[@]}" -lt 1 ]]; then
echo "Unknown error in SFTP connection" >>$LOGFILE
exit 1
elif [[ "${#LIST[@]}" -lt 2 ]]; then
echo "No files to download" >>$LOGFILE
exit 0
fi
# Connection via SFTP and download files
for FILE in "${LIST[@]:1}"
do
# Download
echo "get $FILE" | sftp -oPort=$PORT $USER@$HOST &>/dev/null
[ $? -ne 0 ] && echo "$FILE download failed" >>$LOGFILE && continue
echo "$FILE downloaded" >>$LOGFILE
# Delete on remote dir
echo "rm $FILE" | sftp -oPort=$PORT $USER@$HOST &>/dev/null
[ $? -ne 0 ] && echo "$FILE not removed" >>$LOGFILE && continue
echo "$FILE removed" >>$LOGFILE
done
echo "END: $(date)" >>$LOGFILE
exit 0