diff options
author | Andrew Cady <d@jerkface.net> | 2023-05-22 15:46:59 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2023-05-22 15:46:59 -0400 |
commit | 39722e736c57bd54c0878fffc68d082f43b987ac (patch) | |
tree | 2f33f9ed2ad4bfa2d58ca101279eea50bc9baaf5 | |
parent | 1e91b2fac8135a37509d9e8129494a639674e7c6 (diff) |
retention.bash
-rwxr-xr-x | retention.bash | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/retention.bash b/retention.bash new file mode 100755 index 0000000..149bc7a --- /dev/null +++ b/retention.bash | |||
@@ -0,0 +1,71 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | datetime_regexp='[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]:[0-9][0-9]' | ||
4 | |||
5 | main() | ||
6 | { | ||
7 | [ $# = 1 ] || exit | ||
8 | case "$1" in | ||
9 | /*) ;; | ||
10 | *) exit 1 ;; | ||
11 | esac | ||
12 | |||
13 | find "$1" -maxdepth 1 -type d -name '.snapshot~'"$datetime_regexp" | sort -n | retain | ||
14 | } | ||
15 | |||
16 | is_readonly_subvolume() | ||
17 | { | ||
18 | return 0 | ||
19 | btrfs subvolume show -- "$1" | egrep -q '^ Flags:.*\breadonly\b' | ||
20 | } | ||
21 | |||
22 | retain() | ||
23 | { | ||
24 | _year= _month= _day= _hour= _minute= _second= _nanosecond= | ||
25 | now=$(date +%s) | ||
26 | while read line | ||
27 | do | ||
28 | dateline=${line#*/.snapshot~} | ||
29 | before=$(date -d "$dateline" +%s) | ||
30 | age=$((now - before)) | ||
31 | |||
32 | IFS='~-T:,' read year month day hour minute second nanosecond <<< "$dateline" | ||
33 | |||
34 | keep= | ||
35 | if [ "$year" != "$_year" ] | ||
36 | then | ||
37 | keep="$year -> $line" | ||
38 | elif [ "$month" != "$_month" ] && [ "$age" -lt $((365 * 24 * 60 * 60)) ] | ||
39 | then | ||
40 | keep="$year-$month -> $line" | ||
41 | elif [ "$day" != "$_day" ] && [ "$age" -lt $((7 * 24 * 60 * 60)) ] | ||
42 | then | ||
43 | keep="$year-$month-$day -> $line" | ||
44 | elif [ "$hour" != "$_hour" ] && [ "$age" -lt $((24 * 60 * 60)) ] | ||
45 | then | ||
46 | keep="$year-$month-${day}T$hour -> $line" | ||
47 | elif [ "$minute" != "$_minute" ] && [ "$age" -lt $((60 * 60)) ] | ||
48 | then | ||
49 | keep="$year-$month-${day}T$hour:$minute -> $line" | ||
50 | elif [ "$age" -lt 60 ] | ||
51 | then | ||
52 | keep="recent -> $line" | ||
53 | fi | ||
54 | |||
55 | if [ "$keep" ] | ||
56 | then | ||
57 | echo "$keep" >&2 | ||
58 | else | ||
59 | btrfs subvolume delete "$line" | ||
60 | fi | ||
61 | |||
62 | _year=$year | ||
63 | _month=$month | ||
64 | _day=$day | ||
65 | _hour=$hour | ||
66 | _minute=$minute | ||
67 | _second=$second | ||
68 | done | ||
69 | } | ||
70 | |||
71 | main "$@" | ||