Deleting files from Remote FTP-Server older than x days

Solution 1:

You could send a series of commands to lftp and wrap it in a shell script which calculates the filename of the too old files (see the script below).

TODAY=$(date --iso)                   # Today's date like YYYY-MM-DD
RMDATE=$(date --iso -d '6 days ago')  # TODAY minus X days - too old files
FTPUSER=username
FTPPW=password                        # Better load this from an encrypted file
FTPSERVER=FQDN_or_IP
LFTP=/usr/bin/lftp                    # Path to binary
TMPDIR=$(mktemp -d)                   # Your archives are here

# CAUTION: mput -E deletes local files after upload
echo -n "Uploading files via FTP... "
$LFTP << EOF
open ${FTPUSER}:${FTPPW}@${FTPSERVER}
cd backups/${HOSTNAME}
mkdir ${TODAY}
cd ${TODAY}
mput -E ${TMPDIR}/*
cd ..
rm -rf ${RMDATE}
bye
EOF
echo "Done."

Make sure nobody execpt root can read this script or put the credentials somewhere else. TMPDIR is the directory where the backups reside locally. Of course you need to edit the obivous parts.

HTH,
PEra

Solution 2:

At the high risk of being voted down for posting a late comment on this thread, here's an idea.

If you don't care about having dates on your filenames, and if you keep a small manageable number of backups, you can use ordinal filenames: ie backup.1.tgz, backup.2.tgz, backup.3.tgz, and so on.

Given these names, you can simply rename the previous backups before a new one is uploaded.

  • rename backup.2.tgz backup.3.tgz
  • rename backup.1.tgz backup.2.tgz
  • put backup.1.tgz