1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gerhard Wessels
  • 681
  • 1
  • 9
  • 18
  • I would strongly suggest to add the date of the backup to the backup file name, especially if the FTP server is hosted by third party. If something messes up the file times, you might accidentally delete the wrong files. – Franci Penov Nov 21 '08 at 05:59
  • I do! This is a typical file name - factory-hotcopy-Fri-14-Nov-2008-Rev574.zip. I also calculate a md5 on the zip file that gets stored with it in an accompanying text file. – Gerhard Wessels Nov 21 '08 at 07:04
  • If you can use Python, please see [this](http://stackoverflow.com/questions/2867217/how-to-delete-files-with-a-python-script-from-a-ftp-server-which-are-older-than-7/3114477#3114477) answer in a related question. – tzot Jun 24 '10 at 22:59

4 Answers4

1

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.

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
Luca Gibelli
  • 961
  • 12
  • 19
1

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.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • The server I'm using doesn't seem to support the "Find" command - from the greeting, it looks like it's NcFTPd. Is this just a command the admin hasn't enabled, or is there something else I can use? – SqlRyan Dec 11 '08 at 22:00
1

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.

David Dibben
  • 18,460
  • 6
  • 41
  • 41
1
/*******************************************************************************************
* 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()); }
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514