如何从Microsoft FTP服务器删除超过N周的文件

时间:2008-11-21 05:19:30

标签: bash scripting ftp

我运行的OpenSuse服务器每晚都会将压缩的源代码备份上传到Microsoft FTP服务器。我写了一个Bash脚本,通过一个cron作业来完成这个。

我想删除超过特定日期的备份文件。我怎么能这样做?

4 个答案:

答案 0 :(得分:1)

以下内容删除以 dir 为目录树的目录树下的所有文件,其最后修改时间是11月1日之前:

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

日期/时间格式应为ISO 8601;我不知道是否接受其他格式。

答案 1 :(得分:1)

您可以使用delete或mdelete FTP命令删除FTP服务器上的文件。我不知道如何选择旧文件作为服务器端操作,所以一个选项是做一个FTP ls来获取服务器上的文件列表,然后解析输出以获取那些文件比您指定的日期早。然后使用FTP命令删除每个。

如果你有所有文件的本地副本,那么使用find在本地生成文件列表可能更容易,然后从服务器一次删除它们。

如果您对FTP服务器有一定的控制权,那么使用rysnc代替FTP可能会更容易。

答案 2 :(得分:1)

不幸的是,从FTP服务器删除旧文件并不像运行find那么简单。 -mtime +30 -delete因为通常你没有shell访问你的FTP空间。一切都必须通过FTP完成。

Here出现了一个简单的perl脚本。它需要Net::FTP模块。

答案 3 :(得分: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()); }
    }
}