Linux – Script to delete FTP data older than 7 days

bashftplinuxscriptingshell-scripting

Currently I am using following simple line script to take backup of server directory to remote FTP location

# Take backup to remote location
ncftpput -z -R -uusername -ppassword ftp.example.org /destination/directory /source/directory

Now, I just need to delete the backup from remote FTP which is older than 7 days. On normal directory location within the server I simply do it with following simple script

# Delete backups older than 7 days
find /backup -name '*' -type d -mtime +6 -exec rm -rfv "{}" \;

But, running the same is not working on FTP, is there any way I can delete FTP remote location data older than 7 days? may be via lftp or any other command

Best Answer

There is a FUSE filesystem called curlftpfs which will allow you to mount the remote FTP site as if it were a normal filesystem, thus allowing you to run your find command to delete files older than 7 days. First install the package by running apt-get install curlftpfs, yum install curlftpfs, or the equivalent for your distribution.

Once installed, you can mount the remote ftp site like this:

mkdir /tmp/ftp_mount
curlftpfs -o user=username:password ftp.example.org /tmp/ftp_mount

where username and password get replaced by your actual username and password. Now just cd into /tmp/ftp_mount and run your find command to delete the older files. When you're done, cd out of the filesystem and unmount it with:

fusermount -u /tmp/ftp_mount

Since curlftpfs is a FUSE filesystem, you can do this as any user (you don't need root for it to work).