Bash를 사용하여 오래된 BTRFS 스냅샷 제거

Bash를 사용하여 오래된 BTRFS 스냅샷 제거

나는 나만의 작은 백업 스크립트를 작성하려고 합니다. BTRFS 스냅샷을 사용하고 한동안 보관하고 싶습니다. 그동안 스냅샷 생성은 더 이상 문제가 되지 않지만 이제 5일 후에 다시 삭제하고 싶습니다.

스냅샷 생성 시간을 얻기 위해 다음 명령을 실행했습니다.

/usr/bin/find /run/btrfs-root/sdb/__snapshot/ -mindepth 1 -maxdepth 1 -name "auto-sn-*" -exec /usr/sbin/btrfs subvolume show {} \; | /usr/bin/grep "__snapshot|Creation"

산출:

__snapshot/auto-sn-data-20210803
        Creation time:          2021-08-03 14:59:01 +0200
__snapshot/auto-sn-Log-20210803
        Creation time:          2021-08-03 15:00:42 +0200

안타깝게도 출력 날짜 문자열을 기반으로 삭제 결정을 내리는 방법을 찾을 수 없습니다. 이제 정확한 질문은 이 정보를 기반으로 5일 후에 스냅샷 삭제를 어떻게 시작할 수 있느냐는 것입니다.

감사합니다

작은 아이디어:

/usr/bin/find /run/btrfs-root/sdb/__snapshot/ -mindepth 1 -maxdepth 1 -name "auto-sn-*" ! \( -name "*$(/usr/bin/date -d '1 day ago' +%Y%m%d)" -o -name "*$(/usr/bin/date -d '2 day ago' +%Y%m%d)"  -o -name "*$(/usr/bin/date -d '3 day ago' +%Y%m%d)" \) -exec /usr/sbin/btrfs subvolume delete {} \;

이게 효과가 있을지 모르겠어

답변1

날짜만 얻으려면 다음을 수행하십시오.

$ btrfs subvolume show / | grep -oP 'Creation time:\s*\K\d.*$'
# 2021-03-11 04:22:58 +0100

고맙게도 날짜는 일반적인 bash 문자열 비교 함수를 사용하여 간단하게 비교할 수 있는 ISO 형식이므로 기본적으로 이 시점에서 완료되었습니다.

$ old_date=$(btrfs subvolume show / | grep -oP 'Creation time:\s*\K\d.*$')
(some time passes)
$ new_date=$(btrfs subvolume show / | grep -oP 'Creation time:\s*\K\d.*$')
$ [[ "$old_date" < "$new_date" ]] && echo newer || echo older

관련 정보