2

I have a backup ftp server and I have a CRON bash sending all backups here.

Right now I have to manually delete all old files, but I would like to automate this process with a CRON bash

The remote FTP does'nt have date on files (I don't know why), but backups are named by date (%Y-%m-%d-%H-%M-%S).

I thought of listing all files in the directory, sorting them by name and deleting all files with index > X but I don't know how to do that

My current code is :

ftp -inv $ftphost << EOF
user $ftpuser $ftppass
put $savepath.bz2 mysql/"$dbname"/"$datum".sql.bz2
bye
EOF
  • Which OS is this? I've never heard of a UNIX-like OS which doesn't have file creation or modified meta data. – JayMcTee Oct 30 '15 at 10:56
  • all files are dated in 2021 at the same hour, I don't control this ftp server – Bouki Oct 30 '15 at 13:29
2

This solution certainly isn't waterproof but if your backup job runs daily and you want to keep 1 month worth of backups you could delete the file that is one month old with something like:

old=$(date -d 'now -1 month' +'%Y-%m-%d')

And then in the FTP section

mdelete mysql/"$dbname"/"$old"*.sql.bz2

Depending on the FTP server you mau need to toggle glob first. The mls command should tell you whether or not the file name expansion works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.