Subversion Repositories zfs_utils

Rev

Rev 54 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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>
54 rodolico 30
# Usage: cleanSnaps [-f] [-v] [-t <shift>] [-h]
31
#   -f  actually destroy snapshots (use with care)
47 rodolico 32
#   -v  verbose
48 rodolico 33
#   -t  time shift, simulate running at a different time. Examples:
34
#       -t 2m   (run as if 2 months ago)
35
#       -t -4w  (run as if 4 weeks earlier)
36
#       -t +3d  (run as if 3 days in the future)
37
#   -h  show this help and exit
47 rodolico 38
 
39
use strict;
40
use warnings;
54 rodolico 41
use Getopt::Long qw(GetOptions);
47 rodolico 42
use Time::Piece;
43
 
54 rodolico 44
# Allow short option bundling (e.g. -fv) and accept long names
45
Getopt::Long::Configure('bundling');
46
 
47 rodolico 47
my %opts;
54 rodolico 48
GetOptions(\%opts,
49
    'force|f',        # force (actually perform destroys)
50
    'verbose|v',      # verbose
51
    'help|h',         # help
52
    'timeshift|t=s',  # time shift string (alias -t)
53
) or die "Error parsing command line options\n";
47 rodolico 54
 
54 rodolico 55
# Normalize options: default behaviour is dry-run unless --force is specified.
56
my $FORCE   = (defined $opts{'force'} || defined $opts{'f'}) ? 1 : 0;
57
my $VERBOSE = (defined $opts{'verbose'} || defined $opts{'v'}) ? 1 : 0;
58
my $timeshift = defined $opts{'timeshift'} ? $opts{'timeshift'} : $opts{'t'};
47 rodolico 59
 
60
my $ZFS_CMD = $ENV{ZFS_CMD} // 'zfs';
61
 
62
sub logmsg { print @_, "\n" if $VERBOSE }
63
 
48 rodolico 64
# show help and exit
54 rodolico 65
if ($opts{'help'} || $opts{'h'}) {
66
    print "Usage: cleanSnaps [--force|-f] [--verbose|-v] [--timeshift|-t <shift>] [--help|-h] [pool]\n";
67
    print "  --force, -f         actually destroy snapshots (default: dry-run)\n";
68
    print "  --verbose, -v       verbose logging\n";
55 rodolico 69
    print "  --timeshift, -t     time shift, simulate running at a previous time (examples: 2m, -4w, +3d)\n";
54 rodolico 70
    print "  --help, -h          show this help and exit\n";
48 rodolico 71
    exit 0;
72
}
73
 
74
# compute simulated "now" if the user requested a time shift
50 rodolico 75
# allows us to keep or remove snapshots as if we were running at a different time
76
# giving retention decisions less stringent on, say, backup systems
47 rodolico 77
my $now = time();
54 rodolico 78
if (defined $timeshift && $timeshift =~ /^(?:([+-]?)(\d+)([smhdwy]))$/i) {
48 rodolico 79
    my ($sign, $num, $unit) = ($1, $2, lc $3);
80
    # default behavior: no leading sign => treat as negative (simulate running in the past)
81
    $sign = '-' unless defined $sign && $sign ne '';
82
    my %unit_secs = (
83
        s => 1,
84
        m => 30 * 86400,   # months approximated as 30 days to match snapshot 'm' semantics
85
        h => 3600,
86
        d => 86400,
87
        w => 7 * 86400,
88
        y => 365 * 86400,
89
    );
90
    if (!exists $unit_secs{$unit}) {
91
        die "Invalid time unit '$unit' in -t option; use s,m,h,d,w,y\n";
92
    }
93
    my $shift_seconds = $num * $unit_secs{$unit};
94
    $shift_seconds = -$shift_seconds if $sign eq '-';
95
    $now += $shift_seconds;
54 rodolico 96
    my $ts_display = defined $timeshift ? $timeshift : $opts{t};
97
    logmsg("Simulating current time with shift $ts_display; adjusted now -> " . scalar(localtime($now)) );
48 rodolico 98
}
47 rodolico 99
 
50 rodolico 100
my @candidates; # this will hold the candidates for removal
101
 
102
# Optional command-line parameter: pool name to process (only snapshots from this pool will be considered)
103
my $pool = shift @ARGV;
104
if ($pool) {
105
    logmsg("Filtering snapshots to pool: $pool");
106
}
107
 
108
# if a pool name was given, limit zfs list to that pool and do a recursive list
109
# recursion is done by default if no pool given
110
my $command = $ZFS_CMD . ' list -H -o name -t snapshot' . ($pool ? " -r $pool" : '');
111
 
112
open my $fh, '-|', $command or die "Failed to run $ZFS_CMD: $!";
47 rodolico 113
while (my $snap = <$fh>) {
114
    chomp $snap;
115
    next unless defined $snap && $snap =~ /\S/;
116
 
117
    unless ($snap =~ /@/) {
118
        logmsg("skipping invalid snapshot name: $snap");
119
        next;
120
    }
121
    my ($dataset, $snapname) = split /@/, $snap, 2;
122
 
50 rodolico 123
    # match an optional prefix, a date or date-time stamp, optional text, then TTL
124
    # Examples matched:
125
    #   pool@snapname-2025-12-01-3d
126
    #   pool@prefix_2025-12-01T12:30:00_foo_3h
127
    #   pool@2025-12-01_foo_2w
128
    # allow time separators of ':' or '.' and optional underscore or space before time
129
    unless ($snapname =~ m/.*?(\d{4}-\d{2}-\d{2}(?:[T _]\d{2}[:\.]\d{2}(?:[:\.]\d{2})?)?)(?:.*?)(\d+)([smhdwy])$/) {
130
        logmsg("snapshot does not match ...<date/time>...<N><s|m|h|d|w|y>: $snap");
47 rodolico 131
        next;
132
    }
133
    my ($snap_date, $num, $unit) = ($1, $2, $3);
134
 
50 rodolico 135
    # parse snapshot date/time using Time::Piece; accept several common formats
47 rodolico 136
    my $snap_epoch;
50 rodolico 137
    my @fmts = (
138
        '%Y-%m-%d %H:%M:%S',
139
        '%Y-%m-%dT%H:%M:%S',
140
        '%Y-%m-%d %H:%M',
141
        '%Y-%m-%d_%H.%M.%S',
142
        '%Y-%m-%d_%H.%M',
143
        '%Y-%m-%d',
144
    );
145
    # try to parse using each format until one works
146
    my $parsed = 0;
147
    for my $fmt (@fmts) {
148
        eval {
149
            my $tp = Time::Piece->strptime($snap_date, $fmt);
150
            $snap_epoch = $tp->epoch;
151
            $parsed = 1;
152
            1;
153
        };
154
        last if $parsed;
155
    }
156
    # didn't parse successfully, show message and skip
157
    unless ($parsed) {
158
        logmsg("failed to parse date/time '$snap_date' for $snap");
47 rodolico 159
        next;
50 rodolico 160
    }
47 rodolico 161
 
50 rodolico 162
    # compute retention in seconds according to unit (s,m,h,d,w,m,y)
163
    my $retention_seconds;
164
    if ($unit eq 's') { $retention_seconds = $num }
165
    elsif ($unit eq 'm') { $retention_seconds = $num * 30 * 86400 }   # months ~= 30 days
166
    elsif ($unit eq 'h') { $retention_seconds = $num * 3600 }
167
    elsif ($unit eq 'd') { $retention_seconds = $num * 86400 }
168
    elsif ($unit eq 'w') { $retention_seconds = $num * 7 * 86400 }
169
    elsif ($unit eq 'y') { $retention_seconds = $num * 365 * 86400 }
47 rodolico 170
    else { logmsg("unknown unit $unit for $snap"); next }
171
 
50 rodolico 172
   # determine if snapshot is older than retention period
47 rodolico 173
    my $age = $now - $snap_epoch;
50 rodolico 174
    if ($age > $retention_seconds) { # ready for removal
47 rodolico 175
        push @candidates, $snap;
50 rodolico 176
    } else { # keep
177
        my $age_days = $age / 86400;
178
        my $ret_days = $retention_seconds / 86400;
179
        logmsg("keep: $snap (age " . sprintf("%.2f", $age_days) . "d <= " . sprintf("%.2f", $ret_days) . "d)");
47 rodolico 180
    }
181
}
182
close $fh;
183
 
184
if (!@candidates) {
185
    print "No snapshots to remove.\n";
186
    exit 0;
187
}
188
 
189
printf "Snapshots to remove (%d):\n", scalar @candidates;
190
for my $s (@candidates) { printf "  %s\n", $s }
191
 
54 rodolico 192
if (!$FORCE) {
47 rodolico 193
    print "\nDry-run: no snapshots were destroyed. Use -f to actually remove them.\n";
194
    exit 0;
195
}
196
 
50 rodolico 197
# actual removal. If any fail, report error but continue to remove rest
47 rodolico 198
my $failed = 0;
199
for my $s (@candidates) {
200
    printf "Destroying: %s\n", $s;
201
    my $rc = system($ZFS_CMD, 'destroy', $s);
202
    if ($rc != 0) {
203
        printf STDERR "Failed to destroy %s\n", $s;
204
        $failed = 1;
205
    }
206
}
207
 
208
if ($failed) {
209
    print STDERR "One or more destroys failed.\n";
210
    exit 1;
211
}
212
 
213
print "Done.\n";