How to delete files older than N weeks from a Microsoft FTP server

I run an OpenSuse server that uploads zipped source code backups to a Microsoft FTP server every night. I have written a Bash script that does this through a cron job.

I want to delete backed up files that are older than a certain date. How could I do this?

Answers


You can delete files on the FTP server using the delete or mdelete FTP commands. I don't know of a way to select old files as a server-side operation, so one option would be to do an FTP ls to get a list of the files on the server, then parse the output to pick up those files which are older than your specified date. Then delete each one using an FTP command.

If you have a local copy of all the files then it is probably easier to generate the list of files locally using find then delete them one at a time from the server.

If you have some control over the FTP server then using rysnc instead of FTP would probably be easier.


The following deletes all files under the directory tree rooted at dir whose last modification time was before November 1:

find dir -type f \! -newermt 2008-11-01 -exec rm '{}' \+

The date/time format should be ISO 8601; I don't know if other formats are accepted.


Unfortunately deleting old files from an FTP server is not as simple as running find . -mtime +30 -delete because usually you don’t get shell access to your FTP space. Everything must be done via FTP.

Here comes a simple perl script that does the trick. It requires the Net::FTP module.


/*******************************************************************************************
* Author: Kevin Osborne
* This java app aims to delete non-empty directories from an FTP server that are older than 
* 45 days, the 45 can be changed to whatever.  I believe it's recursive, but I've only tried
* with 3 levels deep, so I can't guarantee anything beyond that, but it worked for my needs 
* and hopefully it will for yours, too.
*
* It uses ftp4j, which I found to be incredibly simple to use, though limited in some ways.
* feel free to use it, I hope it helps. ftp4j can be downloaded as a jar file here:
* http://www.sauronsoftware.it/projects/ftp4j/ just include it in your IDE.
*******************************************************************************************/


package siabackupmanager;

import java.util.Calendar.*;
import java.util.*;
import it.sauronsoftware.ftp4j.*;

public class SIABackupManager {

   // @SuppressWarnings("static-access")
    public static void main(String[] args) {
    if (args.length != 3) {
        System.out.println("Usage: java -jar SIABackupManager.jar HOST USERNAME PASSWORD");
        System.exit(0);
    }
    try {
        FTPClient client = new FTPClient();
        String hostname = args[0];
        String username = args[1];
        String password = args[2];

        client.connect(hostname);
        client.login(username, password);

        FTPFile[] fileArray = client.list();

        for (int i = 0; i < fileArray.length; i++) {


            FTPFile file = fileArray[i];
            if (file.getType() == FTPFile.TYPE_DIRECTORY) {

                java.util.Date modifiedDate = file.getModifiedDate();
                Date purgeDate = new Date();
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -45);
                purgeDate = cal.getTime();

                if (modifiedDate.before(purgeDate)) {

                        String dirName = file.getName();
                        deleteDir(client, dirName);
                        client.changeDirectoryUp();
                        client.deleteDirectory(dirName);
                }
            }
        }
     } catch(Exception ex) { System.out.println("FTP error: " + ex.getMessage()); }
  }

  public static void deleteDir(FTPClient client, String dir) {
        try {
            client.changeDirectory(dir);
            FTPFile[] fileArray = client.list();
            for (int i = 0; i < fileArray.length; i++) {
                FTPFile file = fileArray[i];
                if (file.getType() == FTPFile.TYPE_FILE) {
                    String fileName = file.getName();
                    client.deleteFile(fileName);
                }
            }
            for (int i = 0; i < fileArray.length; i++) {
                FTPFile file = fileArray[i];
                if (file.getType() == FTPFile.TYPE_DIRECTORY) {
                    String dirName = file.getName();
                    deleteDir(client, dirName);
                    client.changeDirectoryUp();
                    String currentDir = client.currentDirectory();
                    client.deleteDirectory(dirName);
                }
            }
         } catch (Exception ex) { System.out.println("deleteDir error: " + ex.getMessage()); }
    }
}

Need Your Help

PHP JAVA Bridge

linux centos php-java-bridge

I am very new to Linux but I am trying best to learn. We have dedicated hosting server of CentOS 6. It is configure with Apache Server as our public website. Now we need to install PHP JAVA Bridge ...

MPMediaItem generating unique filename

ios ios5 mpmediaitem

I am importing items from the iPod library and want to generate a unique filename for each MPMediaItem imported. I want the filename to uniquely represent one MPMediaItem so that if the user import...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.