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

I run an OpenSuse server that uploads compressed source backups to a Microsoft FTP server every night. I wrote a Bash script that does this via a cron job.

I want to delete backed up files older than a certain date. How can i do this?

+1


source to share


4 answers


You can delete files on the FTP server using the delete or mdelete FTP commands. I don't know how to select old files as a server operation, so one option would be to do FTP ls to get a list of files on the server and then parse the output to get those files older than your specified date. Then remove each one using the 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 and then delete them one by one from the server.



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

+1


source


Next, all files under the directory tree, rooted in dir, whose last modification was before November 1, are deleted:

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

      



Date / time format must be in accordance with ISO 8601; I don't know if other formats are accepted.

+1


source


Unfortunately, deleting old files from an FTP server is not as easy as running find. -mtime +30 -delete because normally you don't get shell access to your FTP space. Everything has to be done via FTP.

There is a simple perl script here that does the trick. This requires a module Net::FTP

.

+1


source


/*******************************************************************************************
* 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 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()); }
    }
}

      

+1


source







All Articles