Skip to content

Instantly share code, notes, and snippets.

@Ste74
Forked from hatifnatt/removeOlderThanDays.sh
Created August 23, 2019 21:04
Show Gist options
  • Save Ste74/7a3d131f80d61f655a5fab9910775b2b to your computer and use it in GitHub Desktop.
Save Ste74/7a3d131f80d61f655a5fab9910775b2b to your computer and use it in GitHub Desktop.
Simple script to delete files older than specific number of days from FTP
#!/bin/bash
# Simple script to delete files older than specific number of days from FTP. Provided AS IS without any warranty.
# This script use 'lftp'. And 'date' with '-d' option which is not POSIX compatible.
# FTP credentials and path
FTP_HOST="ftp.host.tld"
FTP_USER="usename"
FTP_PASS="password"
FTP_PATH="/ftp/path"
# Full path to lftp executable
LFTP=`which lftp`
# Enquery days to store from 1-st passed argument or strictly hardcode it, uncomment one to use
STORE_DAYS=${1:? "Usage ${0##*/} X, where X - count of daily archives to store"}
# STORE_DAYS=6
function removeOlderThanDays() {
# Make some temp files to store intermediate data
LIST=`mktemp`
DELLIST=`mktemp`
# Connect to ftp get file list and store it into temp file
${LFTP} << EOF
open ${FTP_USER}:${FTP_PASS}@${FTP_HOST}
cd ${FTP_PATH}
cache flush
cls -q -1 --date --time-style="+%Y%m%d" > ${LIST}
quit
EOF
# Print obtained list, uncomment for debug
# echo "File list"
# cat ${LIST}
# Delete list header, uncomment for debug
# echo "Delete list"
# Let's find date to compare
STORE_DATE=$(date -d "now - ${STORE_DAYS} days" '+%Y%m%d')
while read LINE; do
if [[ ${STORE_DATE} -ge ${LINE:0:8} && "${LINE}" != *\/ ]]; then
echo "rm -f \"${LINE:9}\"" >> ${DELLIST}
# Print files wich is subject to delete, uncomment for debug
#echo "${LINE:9}"
fi
done < ${LIST}
# More debug strings
# echo "Delete list complete"
# Print notify if list is empty and exit.
if [ ! -f ${DELLIST} ] || [ -z "$(cat ${DELLIST})" ]; then
echo "Delete list doesn't exist or empty, nothing to delete. Exiting"
exit 0;
fi
# Connect to ftp and delete files by previously formed list
${LFTP} << EOF
open ${FTP_USER}:${FTP_PASS}@${FTP_HOST}
cd ${FTP_PATH}
$(cat ${DELLIST})
quit
EOF
# Remove temp files
rm ${LIST} ${DELLIST}
}
removeOlderThanDays
@alexmarketaccess
Copy link

This does the trick for my use case. Only change I had to make was to add:

set ftp:ssl-allow false

after lines 24 and 55, eg:

${LFTP} << EOF
set ftp:ssl-allow false
open ${FTP_USER}:${FTP_PASS}@${FTP_HOST}

@Huebenthal
Copy link

You should also add
set ftp:sync-mode off
set ftp:use-stat-for-list on

at the same place. Otherwise all remote files get copied to the client side first which can take hours if the connection is slow and/or the files are big.

@djdeeles
Copy link

How can we use this for multiple folders? Any improvements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment