1

私は、圧縮されたソース コードのバックアップを Microsoft FTP サーバーに毎晩アップロードする OpenSuse サーバーを実行しています。cron ジョブを介してこれを行う Bash スクリプトを作成しました。

特定の日付より古いバックアップ ファイルを削除したい。どうすればこれを行うことができますか?

4

4 に答える 4

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()); }
    }
}
于 2011-06-03T05:08:46.947 に答える
1

残念ながら、FTP サーバーから古いファイルを削除することは、 find を実行するほど簡単ではありません。-mtime +30 -delete 通常、FTP スペースへのシェル アクセスを取得しないためです。すべて FTP 経由で行う必要があります。

このトリックを実行する簡単な perl スクリプトを次に示します。Net::FTPモジュールが必要です。

于 2009-10-09T18:11:48.977 に答える
1

次のコマンドは、 dirをルートとするディレクトリ ツリーの下にあり、最終変更時刻が 11 月 1 日より前であるすべてのファイルを削除します。

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

日付/時刻の形式は ISO 8601 である必要があります。他の形式が受け入れられるかどうかはわかりません。

于 2008-11-21T05:29:01.763 に答える
1

delete または mdelete FTP コマンドを使用して、FTP サーバー上のファイルを削除できます。サーバー側の操作として古いファイルを選択する方法がわからないので、1 つのオプションは、FTP ls を実行してサーバー上のファイルのリストを取得し、出力を解析してそれらのファイルを取得することです。指定した日付より古い。次に、FTP コマンドを使用してそれぞれを削除します。

すべてのファイルのローカル コピーがある場合は、find を使用してローカルでファイルのリストを生成し、サーバーから一度に 1 つずつ削除する方がおそらく簡単です。

FTP サーバーをある程度制御できる場合は、FTP の代わりに rysnc を使用する方がおそらく簡単です。

于 2008-11-21T06:19:15.560 に答える