-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-zfs-snapshots.sh
More file actions
executable file
·355 lines (311 loc) · 9.85 KB
/
create-zfs-snapshots.sh
File metadata and controls
executable file
·355 lines (311 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env bash
# ZFS Snapshot Management Script
#
# This script automates ZFS snapshot creation and retention management across
# multiple datasets based on configurable intervals (hourly, daily, weekly).
# It processes configuration files and automatically prunes old snapshots
# according to predefined retention policies.
#
# Features:
# - Interval-based snapshot creation (hourly, daily, weekly)
# - Automatic pruning with configurable retention policies:
# * Hourly: Keep current day only (prune older than yesterday)
# * Daily: Keep 7 days (prune older than 7 days)
# * Weekly: Keep configurable weeks (default: 10 weeks)
# - Recursive snapshot support for dataset hierarchies
# - ZFS scrub detection and waiting (prevents conflicts during pool scrubs)
# - Configuration-driven dataset management
# - Comprehensive error handling and reporting
#
# Prerequisites:
# - ZFS utilities must be installed
# - Must be run with sudo/root privileges
# - Configuration files located in /usr/local/etc/create-zfs-snapshots.d/
# - Each configuration file (.conf) must define:
# * DATASET: Full ZFS dataset path to snapshot
# * INTERVALS: Comma-separated list of intervals for this dataset
# * RECURSIVE: Optional boolean for recursive snapshots (default: false)
#
# Configuration Example:
# DATASET="tank/data"
# INTERVALS="hourly,daily,weekly"
# RECURSIVE=true
#
# Usage:
# ./create-zfs-snapshots.sh INTERVAL [OPTIONS]
#
# Examples:
# ./create-zfs-snapshots.sh daily
# ./create-zfs-snapshots.sh hourly --verbose
#
# Exit Codes:
# - 0: Success
# - 1: Invalid arguments or configuration directory missing
# - 2: Not running as root
# - 100: One or more snapshot operations failed
#
# Snapshot Naming Convention:
# - Hourly: auto-hourly-YYMMDDHH (e.g., auto-hourly-24052115)
# - Daily/Weekly: auto-[interval]-YYMMDD (e.g., auto-daily-240521)
# - Custom intervals: auto-[interval]-YYMMDD
CONFIG_DIR="${CONFIG_DIR:-/usr/local/etc/create-zfs-snapshots.d}"
SNAPSHOT_NAME=""
KEEP_WEEKLY=${KEEP_WEEKLY:-10}
INTERVAL=""
# Function to check if a ZFS scrub is in progress
is_scrub_running() {
zpool status | grep -q "scrub in progress"
}
print_usage() {
cat << EOF
Usage: $0 INTERVAL [OPTIONS]
OPTIONS:
-v, --verbose Enable verbose output
-h, --help Show this help message
ENVIRONMENT VARIABLES:
CONFIG_DIR Directory containing snapshot configuration
files (default: /usr/local/etc/create-zfs-snapshots.d)
EOF
}
prune_daily() {
local dataset="$1"
local recursive="$2"
local result=0
# don't keep daily snapshots older than 7 days
local seven_days_ago=$(date -d "7 days ago" +"%y%m%d")
local seven_days_ago_int=$((seven_days_ago))
while read -r snapshot; do
snapshot_name=$(cut -d'@' -f2 <<< "$snapshot")
snapshot_time=${snapshot_name#auto-daily-}
snapshot_time_int=$((snapshot_time))
if [ "$snapshot_time_int" -le "$seven_days_ago_int" ]; then
echo "Pruning old daily snapshot: $snapshot"
if ! zfs destroy "$snapshot"; then
echo "Error: Failed to prune $snapshot"
result=1
fi
else
break
fi
done < <(zfs list -Ho name -t snapshot "${dataset}" |
grep -P "@auto-daily-\d{6}$" |
sort)
if [ "$recursive" = true ]; then
while read -r child_dataset; do
if ! prune_daily "$child_dataset" false; then
result=1
fi
done < <(zfs list -Ho name |
grep "^$dataset/" |
sort)
fi
return $result
}
prune_hourly() {
local dataset="$1"
local recursive="$2"
local result=0
# don't keep hourly snapshots older than yesterday
local yesterday=$(date -d "yesterday" +"%y%m%d%H")
local yesterday_int=$((yesterday))
# zfs list -Ho name -t snapshot "${dataset}" |
# grep "@auto-hourly-" |
# sort |
# while read -r snapshot; do
# snapshot_name=$(cut -d'@' -f2 <<< "$snapshot")
# snapshot_time=${snapshot_name#auto-hourly-}
# snapshot_time_int=$((snapshot_time))
# if [ "$snapshot_time_int" -le "$yesterday_int" ]; then
# echo "Pruning old hourly snapshot: $snapshot"
# if ! zfs destroy "$snapshot"; then
# echo "Error: Failed to prune $snapshot"
# result=1
# fi
# else
# break
# fi
# done
while read -r snapshot; do
snapshot_name=$(cut -d'@' -f2 <<< "$snapshot")
snapshot_time=${snapshot_name#auto-hourly-}
snapshot_time_int=$((snapshot_time))
if [ "$snapshot_time_int" -le "$yesterday_int" ]; then
echo "Pruning old hourly snapshot: $snapshot"
if ! zfs destroy "$snapshot"; then
echo "Error: Failed to prune $snapshot"
result=1
fi
else
break
fi
done < <(zfs list -Ho name -t snapshot "${dataset}" |
grep -P "@auto-hourly-\d{8}$" |
sort)
if [ "$recursive" = true ]; then
# zfs list -Ho name |
# grep "^$dataset/" |
# sort |
# while read -r child_dataset; do
# if ! prune_hourly "$child_dataset" false; then
# result=1
# fi
# done
while read -r child_dataset; do
if ! prune_hourly "$child_dataset" false; then
result=1
fi
done < <(zfs list -Ho name |
grep "^$dataset/" |
sort)
fi
return $result
}
prune_weekly() {
local dataset="$1"
local recursive="$2"
local result=0
# don't keep weekly snapshots older than configured number of weeks
local weeks_ago=$(date -d "$KEEP_WEEKLY weeks ago" +"%y%m%d")
local weeks_ago_int=$((weeks_ago))
while read -r snapshot; do
snapshot_name=$(cut -d'@' -f2 <<< "$snapshot")
snapshot_time=${snapshot_name#auto-weekly-}
snapshot_time_int=$((snapshot_time))
if [ "$snapshot_time_int" -le "$weeks_ago_int" ]; then
echo "Pruning old weekly snapshot: $snapshot"
if ! zfs destroy "$snapshot"; then
echo "Error: Failed to prune $snapshot"
result=1
fi
else
break
fi
done < <(zfs list -Ho name -t snapshot "${dataset}" |
grep -P "@(auto-weekly-\d{6}|\d+)" |
sort)
if [ "$recursive" = true ]; then
while read -r child_dataset; do
if ! prune_weekly "$child_dataset" false; then
result=1
fi
done < <(zfs list -Ho name |
grep "^$dataset/" |
sort)
fi
return $result
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Please use sudo."
exit 2
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-v|--verbose)
VERBOSE=true
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
if [ -z "$INTERVAL" ]; then
INTERVAL="$1"
shift
else
echo "Unknown option: $1"
print_usage
exit 1
fi
;;
esac
done
if [ -z "$INTERVAL" ]; then
echo "Error: INTERVAL argument is required."
print_usage
exit 1
fi
if [ ! -e "$CONFIG_DIR" ]; then
echo "Error: Configuration directory $CONFIG_DIR does not exist"
exit 1
fi
# Wait for any ongoing scrub to complete
while is_scrub_running; do
echo "ZFS scrub in progress. Waiting..."
sleep 120 # Wait for 2 minute between checks
done
SNAPSHOT_NAME="auto-$INTERVAL"
if [ "$INTERVAL" = "hourly" ]; then
SNAPSHOT_NAME="$SNAPSHOT_NAME-$(date +"%y%m%d%H")"
else
SNAPSHOT_NAME="$SNAPSHOT_NAME-$(date +"%y%m%d")"
fi
HAS_FAILURE=false
for config_file in "$CONFIG_DIR"/*.conf; do
# Skip if not a file
[ -f "$config_file" ] || continue
DATASET=""
RECURSIVE=false
INTERVALS=""
# Get the base filename (service name)
source "$config_file"
# Validate required variables
if [ -z "$DATASET" ]; then
echo "Error: DATASET not set in $config_file"
HAS_FAILURE=true
continue
fi
if ! zfs list -H -o name "$DATASET" &>/dev/null; then
echo "Error: Dataset $DATASET does not exist"
HAS_FAILURE=true
continue
fi
if [ -z "$INTERVALS" ]; then
echo "Error: INTERVALS not set in $config_file"
HAS_FAILURE=true
continue
fi
if [[ ! " $INTERVALS " =~ $INTERVAL ]]; then
echo "Skipping $DATASET: INTERVAL $INTERVAL not in $INTERVALS"
continue
fi
RECURSE_ARG=""
if [ "$RECURSIVE" = true ]; then
RECURSE_ARG="-r"
fi
# Take a snapshot of the dataset
echo "Creating snapshot for dataset $DATASET..."
zfs snapshot $RECURSE_ARG "${DATASET}@${SNAPSHOT_NAME}"
if [ $? -ne 0 ]; then
echo "Error: Failed to create snapshot for $DATASET"
HAS_FAILURE=true
continue
fi
echo "Snapshot ${DATASET}@${SNAPSHOT_NAME} created successfully."
echo "Pruning old snapshots for $DATASET..."
case "$INTERVAL" in
hourly)
if ! prune_hourly "${DATASET}" "${RECURSIVE}"; then
HAS_FAILURE=true
fi
;;
daily)
if ! prune_daily "${DATASET}" "${RECURSIVE}"; then
HAS_FAILURE=true
fi
;;
weekly)
if ! prune_weekly "${DATASET}" "${RECURSIVE}"; then
HAS_FAILURE=true
fi
;;
*)
echo "Snapshots created with custom interval '$INTERVAL': ${SNAPSHOT_NAME}"
;;
esac
done
if [ "$HAS_FAILURE" = true ]; then
echo "One or more snapshot operations failed."
exit 100
fi
echo "Done."