Script: receive files via SFTP

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.

# 20180801
# Receive files via SFTP
LOGFILE=/var/tmp/receivefiles.$(date -I).log
echo "START: $(date)" >>$LOGFILE
# Connection data
# 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
# Connection via SFTP and download files
for FILE in "${LIST[@]:1}"
    # 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
echo "END:   $(date)" >>$LOGFILE
exit 0