【问题标题】:How to delete files older than N weeks from a Microsoft FTP server如何从 Microsoft FTP 服务器删除超过 N 周的文件
【发布时间】:2008-11-21 05:19:29
【问题描述】:

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

我想删除早于某个日期的备份文件。我怎么能这样做?

【问题讨论】:

  • 我强烈建议将备份日期添加到备份文件名中,尤其是在 FTP 服务器由第三方托管的情况下。如果某些事情弄乱了文件时间,您可能会不小心删除了错误的文件。
  • 我愿意!这是一个典型的文件名 - factory-hotcopy-Fri-14-Nov-2008-Rev574.zip。我还计算了 zip 文件上的 md5,该文件与它一起存储在随附的文本文件中。
  • 如果您可以使用 Python,请参阅this 相关问题中的答案。

标签: bash scripting ftp


【解决方案1】:

您可以使用 delete 或 mdelete FTP 命令删除 FTP 服务器上的文件。我不知道选择旧文件作为服务器端操作的方法,因此一种选择是执行 FTP ls 以获取服务器上的文件列表,然后解析输出以获取那些文件早于您指定的日期。然后使用 FTP 命令删除每一个。

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

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

【讨论】:

    【解决方案2】:

    以下删除以dir为根的目录树下最后修改时间在11月1日之前的所有文件:

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

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

    【讨论】:

    • 我使用的服务器似乎不支持“查找”命令 - 从问候语来看,它看起来像是 NcFTPd。这只是管理员未启用的命令,还是我可以使用其他命令?
    【解决方案3】:

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

    Here 提供了一个简单的 perl 脚本来解决问题。它需要Net::FTP 模块。

    【讨论】:

    • 非常感谢 - 这正是我想要的。
    【解决方案4】:
    /*******************************************************************************************
    * 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()); }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 2016-12-01
      • 1970-01-01
      • 2011-10-11
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多