| 47 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
|
| 48 |
rodolico |
3 |
# Simplified BSD License (FreeBSD License)
|
|
|
4 |
#
|
|
|
5 |
# Copyright (c) 2025, Daily Data Inc.
|
|
|
6 |
# All rights reserved.
|
|
|
7 |
#
|
|
|
8 |
# Redistribution and use in source and binary forms, with or without
|
|
|
9 |
# modification, are permitted provided that the following conditions are met:
|
|
|
10 |
#
|
|
|
11 |
# 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
12 |
# list of conditions and the following disclaimer.
|
|
|
13 |
#
|
|
|
14 |
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
15 |
# this list of conditions and the following disclaimer in the documentation
|
|
|
16 |
# and/or other materials provided with the distribution.
|
|
|
17 |
#
|
|
|
18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
21 |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
22 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
23 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
24 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
25 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
26 |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
27 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
28 |
|
| 47 |
rodolico |
29 |
# cleanSnaps - detect and remove old ZFS snapshots named like YYYY-MM-DD-<N><d|w|m>
|
| 48 |
rodolico |
30 |
# Usage: cleanSnaps [-n] [-f] [-v] [-t <shift>] [-h]
|
| 47 |
rodolico |
31 |
# -n dry-run (default)
|
|
|
32 |
# -f actually destroy snapshots
|
|
|
33 |
# -v verbose
|
| 48 |
rodolico |
34 |
# -t time shift, simulate running at a different time. Examples:
|
|
|
35 |
# -t 2m (run as if 2 months ago)
|
|
|
36 |
# -t -4w (run as if 4 weeks earlier)
|
|
|
37 |
# -t +3d (run as if 3 days in the future)
|
|
|
38 |
# -h show this help and exit
|
| 47 |
rodolico |
39 |
|
|
|
40 |
use strict;
|
|
|
41 |
use warnings;
|
|
|
42 |
use Getopt::Std;
|
|
|
43 |
use Time::Piece;
|
|
|
44 |
|
|
|
45 |
my %opts;
|
| 48 |
rodolico |
46 |
getopts('nfvht:', \%opts);
|
| 47 |
rodolico |
47 |
|
|
|
48 |
my $DRY_RUN = $opts{f} ? 0 : 1; # default dry-run, -f disables dry-run
|
|
|
49 |
my $FORCE = $opts{f} ? 1 : 0;
|
|
|
50 |
my $VERBOSE = $opts{v} ? 1 : 0;
|
|
|
51 |
|
|
|
52 |
my $ZFS_CMD = $ENV{ZFS_CMD} // 'zfs';
|
|
|
53 |
|
|
|
54 |
sub logmsg { print @_, "\n" if $VERBOSE }
|
|
|
55 |
|
| 48 |
rodolico |
56 |
# show help and exit
|
|
|
57 |
if ($opts{h}) {
|
|
|
58 |
print "Usage: cleanSnaps [-n] [-f] [-v] [-t <shift>] [-h]\n";
|
|
|
59 |
print " -n dry-run (default)\n";
|
|
|
60 |
print " -f actually destroy snapshots\n";
|
|
|
61 |
print " -v verbose\n";
|
|
|
62 |
print " -t time shift, simulate running at a different time (examples: 2m, -4w, +3d)\n";
|
|
|
63 |
print " -h show this help and exit\n";
|
|
|
64 |
exit 0;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
# compute simulated "now" if the user requested a time shift
|
| 50 |
rodolico |
68 |
# allows us to keep or remove snapshots as if we were running at a different time
|
|
|
69 |
# giving retention decisions less stringent on, say, backup systems
|
| 47 |
rodolico |
70 |
my $now = time();
|
| 48 |
rodolico |
71 |
if (defined $opts{t} && $opts{t} =~ /^(?:([+-]?)(\d+)([smhdwy]))$/i) {
|
|
|
72 |
my ($sign, $num, $unit) = ($1, $2, lc $3);
|
|
|
73 |
# default behavior: no leading sign => treat as negative (simulate running in the past)
|
|
|
74 |
$sign = '-' unless defined $sign && $sign ne '';
|
|
|
75 |
my %unit_secs = (
|
|
|
76 |
s => 1,
|
|
|
77 |
m => 30 * 86400, # months approximated as 30 days to match snapshot 'm' semantics
|
|
|
78 |
h => 3600,
|
|
|
79 |
d => 86400,
|
|
|
80 |
w => 7 * 86400,
|
|
|
81 |
y => 365 * 86400,
|
|
|
82 |
);
|
|
|
83 |
if (!exists $unit_secs{$unit}) {
|
|
|
84 |
die "Invalid time unit '$unit' in -t option; use s,m,h,d,w,y\n";
|
|
|
85 |
}
|
|
|
86 |
my $shift_seconds = $num * $unit_secs{$unit};
|
|
|
87 |
$shift_seconds = -$shift_seconds if $sign eq '-';
|
|
|
88 |
$now += $shift_seconds;
|
|
|
89 |
logmsg("Simulating current time with shift $opts{t}; adjusted now -> " . scalar(localtime($now)) );
|
|
|
90 |
}
|
| 47 |
rodolico |
91 |
|
| 50 |
rodolico |
92 |
my @candidates; # this will hold the candidates for removal
|
|
|
93 |
|
|
|
94 |
# Optional command-line parameter: pool name to process (only snapshots from this pool will be considered)
|
|
|
95 |
my $pool = shift @ARGV;
|
|
|
96 |
if ($pool) {
|
|
|
97 |
logmsg("Filtering snapshots to pool: $pool");
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
# if a pool name was given, limit zfs list to that pool and do a recursive list
|
|
|
101 |
# recursion is done by default if no pool given
|
|
|
102 |
my $command = $ZFS_CMD . ' list -H -o name -t snapshot' . ($pool ? " -r $pool" : '');
|
|
|
103 |
|
|
|
104 |
open my $fh, '-|', $command or die "Failed to run $ZFS_CMD: $!";
|
| 47 |
rodolico |
105 |
while (my $snap = <$fh>) {
|
|
|
106 |
chomp $snap;
|
|
|
107 |
next unless defined $snap && $snap =~ /\S/;
|
|
|
108 |
|
|
|
109 |
unless ($snap =~ /@/) {
|
|
|
110 |
logmsg("skipping invalid snapshot name: $snap");
|
|
|
111 |
next;
|
|
|
112 |
}
|
|
|
113 |
my ($dataset, $snapname) = split /@/, $snap, 2;
|
|
|
114 |
|
| 50 |
rodolico |
115 |
# match an optional prefix, a date or date-time stamp, optional text, then TTL
|
|
|
116 |
# Examples matched:
|
|
|
117 |
# pool@snapname-2025-12-01-3d
|
|
|
118 |
# pool@prefix_2025-12-01T12:30:00_foo_3h
|
|
|
119 |
# pool@2025-12-01_foo_2w
|
|
|
120 |
# allow time separators of ':' or '.' and optional underscore or space before time
|
|
|
121 |
unless ($snapname =~ m/.*?(\d{4}-\d{2}-\d{2}(?:[T _]\d{2}[:\.]\d{2}(?:[:\.]\d{2})?)?)(?:.*?)(\d+)([smhdwy])$/) {
|
|
|
122 |
logmsg("snapshot does not match ...<date/time>...<N><s|m|h|d|w|y>: $snap");
|
| 47 |
rodolico |
123 |
next;
|
|
|
124 |
}
|
|
|
125 |
my ($snap_date, $num, $unit) = ($1, $2, $3);
|
|
|
126 |
|
| 50 |
rodolico |
127 |
# parse snapshot date/time using Time::Piece; accept several common formats
|
| 47 |
rodolico |
128 |
my $snap_epoch;
|
| 50 |
rodolico |
129 |
my @fmts = (
|
|
|
130 |
'%Y-%m-%d %H:%M:%S',
|
|
|
131 |
'%Y-%m-%dT%H:%M:%S',
|
|
|
132 |
'%Y-%m-%d %H:%M',
|
|
|
133 |
'%Y-%m-%d_%H.%M.%S',
|
|
|
134 |
'%Y-%m-%d_%H.%M',
|
|
|
135 |
'%Y-%m-%d',
|
|
|
136 |
);
|
|
|
137 |
# try to parse using each format until one works
|
|
|
138 |
my $parsed = 0;
|
|
|
139 |
for my $fmt (@fmts) {
|
|
|
140 |
eval {
|
|
|
141 |
my $tp = Time::Piece->strptime($snap_date, $fmt);
|
|
|
142 |
$snap_epoch = $tp->epoch;
|
|
|
143 |
$parsed = 1;
|
|
|
144 |
1;
|
|
|
145 |
};
|
|
|
146 |
last if $parsed;
|
|
|
147 |
}
|
|
|
148 |
# didn't parse successfully, show message and skip
|
|
|
149 |
unless ($parsed) {
|
|
|
150 |
logmsg("failed to parse date/time '$snap_date' for $snap");
|
| 47 |
rodolico |
151 |
next;
|
| 50 |
rodolico |
152 |
}
|
| 47 |
rodolico |
153 |
|
| 50 |
rodolico |
154 |
# compute retention in seconds according to unit (s,m,h,d,w,m,y)
|
|
|
155 |
my $retention_seconds;
|
|
|
156 |
if ($unit eq 's') { $retention_seconds = $num }
|
|
|
157 |
elsif ($unit eq 'm') { $retention_seconds = $num * 30 * 86400 } # months ~= 30 days
|
|
|
158 |
elsif ($unit eq 'h') { $retention_seconds = $num * 3600 }
|
|
|
159 |
elsif ($unit eq 'd') { $retention_seconds = $num * 86400 }
|
|
|
160 |
elsif ($unit eq 'w') { $retention_seconds = $num * 7 * 86400 }
|
|
|
161 |
elsif ($unit eq 'y') { $retention_seconds = $num * 365 * 86400 }
|
| 47 |
rodolico |
162 |
else { logmsg("unknown unit $unit for $snap"); next }
|
|
|
163 |
|
| 50 |
rodolico |
164 |
# determine if snapshot is older than retention period
|
| 47 |
rodolico |
165 |
my $age = $now - $snap_epoch;
|
| 50 |
rodolico |
166 |
if ($age > $retention_seconds) { # ready for removal
|
| 47 |
rodolico |
167 |
push @candidates, $snap;
|
| 50 |
rodolico |
168 |
} else { # keep
|
|
|
169 |
my $age_days = $age / 86400;
|
|
|
170 |
my $ret_days = $retention_seconds / 86400;
|
|
|
171 |
logmsg("keep: $snap (age " . sprintf("%.2f", $age_days) . "d <= " . sprintf("%.2f", $ret_days) . "d)");
|
| 47 |
rodolico |
172 |
}
|
|
|
173 |
}
|
|
|
174 |
close $fh;
|
|
|
175 |
|
|
|
176 |
if (!@candidates) {
|
|
|
177 |
print "No snapshots to remove.\n";
|
|
|
178 |
exit 0;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
printf "Snapshots to remove (%d):\n", scalar @candidates;
|
|
|
182 |
for my $s (@candidates) { printf " %s\n", $s }
|
|
|
183 |
|
|
|
184 |
if ($DRY_RUN) {
|
|
|
185 |
print "\nDry-run: no snapshots were destroyed. Use -f to actually remove them.\n";
|
|
|
186 |
exit 0;
|
|
|
187 |
}
|
|
|
188 |
|
| 50 |
rodolico |
189 |
# actual removal. If any fail, report error but continue to remove rest
|
| 47 |
rodolico |
190 |
my $failed = 0;
|
|
|
191 |
for my $s (@candidates) {
|
|
|
192 |
printf "Destroying: %s\n", $s;
|
|
|
193 |
my $rc = system($ZFS_CMD, 'destroy', $s);
|
|
|
194 |
if ($rc != 0) {
|
|
|
195 |
printf STDERR "Failed to destroy %s\n", $s;
|
|
|
196 |
$failed = 1;
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if ($failed) {
|
|
|
201 |
print STDERR "One or more destroys failed.\n";
|
|
|
202 |
exit 1;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
print "Done.\n";
|