-
-
Save odenijs/23b7cd1bcf90caf6f23335ba7361203e to your computer and use it in GitHub Desktop.
Simple script to delete files older than specific number of days from FTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment