15日を過ぎたファイルを削除する(月間および年間バージョンを除く)

15日を過ぎたファイルを削除する(月間および年間バージョンを除く)

簡単な検索スクリプトを使用して、30日以上経過したバックアップtarファイルを削除していますが、履歴を保存してより詳細に表示できるように、過去15日間に作成されたすべてのバックアップを維持するように切り替えたいと思います。毎月1日(12ヶ月)と毎年1月1日(12年を振り返る日)です。これを行う最もエレガントな方法は何ですか?

答え1

ファイルに正しいファイルシステムレベルのタイムスタンプがある場合そして日付はファイル名(たとえば、backup-20230713.tar.gz周囲)に含まれ、次のようになります。

# delete files older than 30 days, except ones from the first of each month
find . -name 'backup-*.tar.gz' -mtime +30 ! -name '*-??????01.tar.gz' -delete

# delete files older than a year, except ones from January 1st
find . -name 'backup-*.tar.gz' -mtime +366 ! -name '*-????0101.tar.gz' -delete

# delete everything older than 12 years:
find . -name 'backup-*.tar.gz' -mtime +$(( 12 * 366 )) -delete

あるいは、ファイルが作成されたら、「グループ」に基づいて別々のディレクトリにファイルを配置するだけです。たとえば、毎年最初の日は、yearly/残りmonthly/も同じですdaily/。次に、次のようにします。

find daily/   -mtime +30 -delete
find monthly/ -mtime +366 -delete
find yearly/  -mtime +$((12 * 366)) -delete

おそらく、このようなものを使用する前に広範囲にテストする必要があります。 (etcを使用してテストファイルを作成し、etcなしでコマンドをtouch -d試してください)find-delete

答え2

免責事項:私はこの本の現在の著者です。生皮右回転)この回答に使用されたプログラム(場所:https://github.com/raforg/rawhide)。

右回転(そして探す)を使用すると、修正時間を正確に指定できる参照ファイルと修正時間を比較できますtouch -t YYYYMMDDhhmm

以下は、バックアップディレクトリにchdirを指定し、過去12ヶ月間毎月1日と過去12年間で毎年1月1日のタイムスタンプを持つ参照ファイルを生成するシェルスクリプトです。

その後呼び出されます。右回転ファイルリスト(使用右回転オプション-l)は仕様に従って削除する必要があります。

-Dファイルを削除するオプションがあります(使用右回転オプション-UUU)。

検索基準は、指定された日付(つまり、指定された時間と1日後)にファイルが最後に変更されたときにtrueを返すヘルパー関数を定義することから始まります。

on(givenday) { mtime >= givenday && mtime < givenday + day }

その後、検索基準はftarファイル("*.tar*")で、少なくとも15日(old(15*days))が経過し、最後の変更日が過去12ヶ月のいずれかの月の最初の日ではないか、最初の日付ではない通常のファイル()と一致します。月は、過去12年以内の任意の年の1月です(例!on("1st00".mtime) && ...:)。

その後、タイムスタンプは指定された参照ファイルを削除します。

#!/bin/sh

# Delete backup tarfiles that are older than 15 days except those created
# on the 1st of the month for the past 12 months, and those created on the
# first of January for the past 12 years.

# usage: $0 [-D]
# The -D option deletes files. Without it, they are listed.

# Set this to the path to the backups
path=/path/to/backups

opts=-l
case "$1" in
    -D)
        opts="-UUU"
        ;;
    ?*)
        echo usage: $0 [-D]
        echo "The -D option deletes files (with exceptions)."
        echo "Without it, they will be listed instead."
        echo Files in $path will be listed or deleted.
        exit 1
        ;;
esac

cd "$path" || exit 1

# Create timstamped reference files for the 1st Jan (past 12 years)

year=`date +%Y`
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12
do
    y=`expr $year - $i`
    touch -t ${y}01010000 1stJan`printf %02d $i`
done

# Create timstamped reference files for the 1st of the month (past 12 months)

month=`date +%m`
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12
do
    touch -t $year${month}010000 1st`printf %02d $i`
    if [ $month = 01 ]
    then
        month=12
        year=`expr $year - 1`
    else
        month=`expr $month - 1`
        month=`printf '%02d' $month`
    fi
done

# Delete the backup tarfiles

rh $opts '

    # Define a helper function to make the rest easier to read.
    # True when mtime is on the given day.
    on(givenday) { mtime >= givenday && mtime < givenday + day }

    # Tar files at least 15 days old
    f && "*.tar*" && old(15*days) &&

    # Not last modified on the 1st of the past 12 months
    !on("1st00".mtime) &&
    !on("1st01".mtime) &&
    !on("1st02".mtime) &&
    !on("1st03".mtime) &&
    !on("1st04".mtime) &&
    !on("1st05".mtime) &&
    !on("1st06".mtime) &&
    !on("1st07".mtime) &&
    !on("1st08".mtime) &&
    !on("1st09".mtime) &&
    !on("1st10".mtime) &&
    !on("1st11".mtime) &&
    !on("1st12".mtime) &&

    # Not last modified on the 1st of January for the past 12 years
    !on("1stJan00".mtime) &&
    !on("1stJan01".mtime) &&
    !on("1stJan02".mtime) &&
    !on("1stJan03".mtime) &&
    !on("1stJan04".mtime) &&
    !on("1stJan05".mtime) &&
    !on("1stJan06".mtime) &&
    !on("1stJan07".mtime) &&
    !on("1stJan08".mtime) &&
    !on("1stJan09".mtime) &&
    !on("1stJan10".mtime) &&
    !on("1stJan11".mtime) &&
    !on("1stJan12".mtime)

'

# Clean up the timestamped reference files

rm 1st[01][0-9] 1stJan[01][0-9]

単一のバックアップディレクトリにハードコードされていますが、コマンドラインで使いやすいです。しかし、そうすることは危険です。外出するときは気をつけてください。

同じことができます探すほぼ同じ方法で構文が異なり、構文が多く、タイムスタンプを持つ参照ファイルが2倍以上あります。

関連情報