| 54 |
rodolico |
1 |
## cleanSnaps
|
|
|
2 |
|
|
|
3 |
Clean old ZFS snapshots whose names include a TTL suffix. This document describes usage, accepted snapshot name patterns, options, examples and notes.
|
|
|
4 |
|
|
|
5 |
### Purpose
|
|
|
6 |
|
|
|
7 |
`cleanSnaps` is a small Perl utility that scans a list of ZFS snapshots and removes or reports snapshots that have exceeded their configured TTL. TTLs are encoded in snapshot names as a numeric suffix plus a unit (s, m, h, d, w, y). The script supports date/time-stamped snapshot names and an optional time-shift for testing.
|
|
|
8 |
|
|
|
9 |
### Location
|
|
|
10 |
|
|
|
11 |
Script: `cleanSnaps` (same directory as this file)
|
|
|
12 |
|
|
|
13 |
### Synopsis
|
|
|
14 |
|
|
|
15 |
```
|
|
|
16 |
cleanSnaps [--force|-f] [--verbose|-v] [--timeshift|-t <shift>] [--help|-h] [pool]
|
|
|
17 |
```
|
|
|
18 |
|
|
|
19 |
- `--timeshift, -t <shift>` — Apply an artificial time-shift. The script accepts a signed or unsigned quantity with a unit (examples: `30d`, `-4w`, `+3d`, `3600s`). Units: s, m, h, d, w, y. Useful for testing TTL expiry without changing the system clock. A positive shift moves “now” forward (so more snapshots appear expired); a negative shift moves “now” backward.
|
|
|
20 |
- `--force, -f` — Actually destroy snapshots. By default the script runs in safe (non-destructive) mode and only reports candidate snapshots.
|
|
|
21 |
- `--verbose, -v` — Verbose logging; prints keep/skip messages and extra information.
|
|
|
22 |
- `--help, -h` — Show help/usage and exit.
|
|
|
23 |
- `pool` (optional) — Restrict processing to snapshots whose pool name matches this string.
|
|
|
24 |
|
|
|
25 |
When called without a positional `pool` argument, the script will process snapshots from standard input or from a `zfs list` invocation performed by the script. (See the script headers and tests for details.)
|
|
|
26 |
|
|
|
27 |
### Snapshot name format
|
|
|
28 |
|
|
|
29 |
The script recognizes snapshots whose names contain a date stamp followed (optionally after arbitrary text) by a TTL token. The accepted date/time formats include:
|
|
|
30 |
|
|
|
31 |
- `YYYY-MM-DD` (e.g. `2025-12-15`)
|
|
|
32 |
- `YYYY-MM-DD_HH:MM:SS` or `YYYY-MM-DD_HH.MM.SS` (time portion may use `:` or `.` separators; the separator between date and time is an underscore)
|
|
|
33 |
|
|
|
34 |
The TTL suffix is a number followed by a unit letter:
|
|
|
35 |
|
|
|
36 |
- `s` — seconds
|
|
|
37 |
- `m` — minutes
|
|
|
38 |
- `h` — hours
|
|
|
39 |
- `d` — days
|
|
|
40 |
- `w` — weeks
|
|
|
41 |
- `y` — years
|
|
|
42 |
|
|
|
43 |
Examples of matching snapshot name fragments:
|
|
|
44 |
|
|
|
45 |
- `backup_2025-12-15_16.09.00_7d` — date/time + TTL 7 days
|
|
|
46 |
- `autosnap_2025-12-15_7h` — date + TTL 7 hours
|
|
|
47 |
- `daily_2025-12-15_30d_old` — optional trailing text after TTL is allowed; the TTL token (e.g. `30d`) is used to compute expiry.
|
|
|
48 |
|
|
|
49 |
The script tolerates arbitrary text between the date/time stamp and the TTL token.
|
|
|
50 |
|
|
|
51 |
### Matching regex
|
|
|
52 |
|
|
|
53 |
The script uses a single regular expression to identify date/time stamped snapshot names and their TTL token. The exact regex is:
|
|
|
54 |
|
|
|
55 |
```
|
|
|
56 |
/.*?(\d{4}-\d{2}-\d{2}(?:[T _]\d{2}[:\.]\d{2}(?:[:\.]\d{2})?)?)(?:.*?)(\d+)([smhdwy])$/
|
|
|
57 |
```
|
|
|
58 |
|
|
|
59 |
Explanation:
|
|
|
60 |
- The regex is applied to the snapshot name portion (the part after the `@` in a `dataset@snapshot` string).
|
|
|
61 |
- Capture group 1: the date or date-time stamp. Accepts `YYYY-MM-DD` or `YYYY-MM-DD_HH:MM`/`YYYY-MM-DD_HH.MM.SS` styles. The separator between date and time can be `T`, space, or underscore; time separators may be `:` or `.`.
|
|
|
62 |
- Capture group 2: the numeric TTL quantity (one or more digits).
|
|
|
63 |
- Capture group 3: the TTL unit, one of `s m h d w y` (seconds, minutes, hours, days, weeks, years).
|
|
|
64 |
- The regex allows arbitrary text between the date/time stamp and the TTL token, and requires the TTL token to be at the end of the snapshot name.
|
|
|
65 |
|
|
|
66 |
If a snapshot name does not match this pattern it will be ignored by `cleanSnaps`.
|
|
|
67 |
|
|
|
68 |
### Behavior
|
|
|
69 |
|
|
|
70 |
- By default, the script calculates the expiry time as the date/time parsed from the snapshot name plus the TTL interval. If the resulting expiry timestamp is earlier than the current time (adjusted by any `--timeshift`), the snapshot is considered expired and will be listed. Use `--force` to perform destruction via `zfs destroy`.
|
|
|
71 |
- The script prints actions it would take; in its default (safe) mode it only reports candidate snapshots. Use `--verbose` for additional keep/skip messages and context.
|
|
|
72 |
|
|
|
73 |
### Examples
|
|
|
74 |
|
|
|
75 |
- Run with a 30-day forward time-shift (simulate time 30 days later):
|
|
|
76 |
|
|
|
77 |
```
|
|
|
78 |
cleanSnaps -t 30d
|
|
|
79 |
cleanSnaps -t 2592000 # equivalent (30 days in seconds)
|
|
|
80 |
```
|
|
|
81 |
|
|
|
82 |
- Actually remove all snapshots found from pool tank, displaying each step while doing so:
|
|
|
83 |
|
|
|
84 |
```
|
|
|
85 |
cleanSnaps -f -v tank
|
|
|
86 |
```
|
|
|
87 |
|
|
|
88 |
- Restrict processing to pool `tank` (only snapshots whose pool prefix matches `tank` will be considered):
|
|
|
89 |
|
|
|
90 |
```
|
|
|
91 |
cleanSnaps tank
|
|
|
92 |
```
|
|
|
93 |
|
|
|
94 |
- Feed the script a list of snapshots on stdin (for testing):
|
|
|
95 |
|
|
|
96 |
```
|
|
|
97 |
cat sample-snapshots.txt | cleanSnaps -t 3600
|
|
|
98 |
```
|
|
|
99 |
|
|
|
100 |
### Safety & Notes
|
|
|
101 |
|
|
|
102 |
- The script includes a help flag and a Simplified BSD license header. It is safe by default (it reports candidates without destroying them). Nevertheless, exercise caution when running on production systems — prefer testing with `--timeshift` or sample input first.
|
|
|
103 |
- The script parses a variety of date/time formats but will ignore snapshot entries that do not match the expected patterns; those snapshots are left untouched.
|
|
|
104 |
- The script depends on standard Perl modules available in most Perl 5 installations. See the script header for exact module imports.
|
|
|
105 |
|
|
|
106 |
### Tests
|
|
|
107 |
|
|
|
108 |
There is a test harness in the repository (see `cleanSnaps/test_cleanSnaps.pl`) which includes sample snapshot names covering date/time formats, dot/colon time separators, and the `h` (hour) TTL unit. Use that test to validate behavior before running against real ZFS pools.
|
|
|
109 |
|
|
|
110 |
### Contributing / Changes
|
|
|
111 |
|
|
|
112 |
If you update the script, update this documentation to reflect new options, changes in the snapshot matching regex, or different behaviors for dry-run vs. delete modes.
|
|
|
113 |
|
|
|
114 |
### License
|
|
|
115 |
|
|
|
116 |
The script and this documentation are released under the Simplified BSD License (see header in `cleanSnaps`).
|