Skip to content

Commit d66e785

Browse files
committed
Add option --date=FORMAT to specify the date part in the snapshot name.
Create a setup_snap_glob() func to create the SNAPGLOB variable correctly.
1 parent d5cb31a commit d66e785

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/zfs-auto-snapshot.8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ for those in which the user-property \fBcom.sun:auto-snapshot\fR is
1616
set to \fBfalse\fR. This option reverses the behavior and requires
1717
\fBcom.sun:auto-snapshot\fR to be set to \fBtrue\fR.
1818
.TP
19+
\fB\-D\fR, \fB\-\-date=\fIFORMAT\fR
20+
Use \fIformat\fR for snapshot name. See \fBdate\fR(1) for more information.
21+
.TP
1922
\fB\-d\fR, \fB\-\-debug\fR
2023
Print debugging messages.
2124
.TP

src/zfs-auto-snapshot.sh

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ IFS="
2424
"
2525

2626
# Set default program options.
27+
opt_date_format='%F-%H%M'
2728
opt_backup_full=''
2829
opt_backup_incremental=''
2930
opt_default_exclude=''
@@ -56,6 +57,7 @@ print_usage ()
5657
{
5758
echo "Usage: $0 [options] [-l label] <'//' | name [name...]>
5859
--default-exclude Exclude datasets if com.sun:auto-snapshot is unset.
60+
-D, --date=FORMAT Date format. Default '%F-%H%M'.
5961
-d, --debug Print debugging messages.
6062
-e, --event=EVENT Set the com.sun:auto-snapshot-desc property to EVENT.
6163
--fast Use a faster zfs list invocation.
@@ -144,6 +146,45 @@ do_run () # [argv]
144146
}
145147

146148

149+
setup_snap_glob ()
150+
{
151+
local base="$1"
152+
local format="$2"
153+
154+
# The dash to mimic the separator between prefix/label
155+
# and DATE in SNAPNAME.
156+
echo -n "$base"-
157+
echo $format | \
158+
awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
159+
while read char; do
160+
[ "$char" = "%" ] && continue
161+
162+
if [ "$char" = "." ]; then
163+
echo -n "."
164+
elif [ "$char" = "-" ]; then
165+
echo -n "-"
166+
else
167+
# Given the format char, create
168+
# a string with only that.
169+
# So if we said '%Y', the percentage
170+
# sign would have been filtered out
171+
# at the top of the loop, leaving the
172+
# 'Y' here. So str=2014.
173+
str=$(date +"%$char")
174+
175+
# Print the lenght of that with
176+
# question marks (4 with the '%Y'
177+
# example above).
178+
i=0
179+
while [ $i -lt ${#str} ]; do
180+
echo -n "?"
181+
i=$((i + 1))
182+
done
183+
fi
184+
done
185+
}
186+
187+
147188
do_snapshots () # properties, flags, snapname, oldglob, [targets...]
148189
{
149190
local PROPS="$1"
@@ -210,9 +251,9 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
210251
GETOPT=$(getopt \
211252
--longoptions=default-exclude,dry-run,fast,skip-scrub,recursive \
212253
--longoptions=event:,keep:,label:,prefix:,sep: \
213-
--longoptions=debug,help,quiet,syslog,verbose \
254+
--longoptions=date:,debug,help,quiet,syslog,verbose \
214255
--longoptions=pre-snapshot:,post-snapshot:,destroy-only \
215-
--options=dnshe:l:k:p:rs:qgv \
256+
--options=D:dnshe:l:k:p:rs:qgv \
216257
-- "$@" ) \
217258
|| exit 128
218259

@@ -221,6 +262,10 @@ eval set -- "$GETOPT"
221262
while [ "$#" -gt '0' ]
222263
do
223264
case "$1" in
265+
(-D|--date)
266+
opt_date_format="$2"
267+
shift 2
268+
;;
224269
(-d|--debug)
225270
opt_debug='1'
226271
opt_quiet=''
@@ -530,13 +575,13 @@ SNAPPROP="-o com.sun:auto-snapshot-desc='$opt_event'"
530575

531576
# ISO style date; fifteen characters: YYYY-MM-DD-HHMM
532577
# On Solaris %H%M expands to 12h34.
533-
DATE=$(date --utc +%F-%H%M)
578+
DATE=$(date --utc +"$opt_date_format")
534579

535580
# The snapshot name after the @ symbol.
536581
SNAPNAME="$opt_prefix${opt_label:+$opt_sep$opt_label}-$DATE"
537582

538-
# The expression for matching old snapshots. -YYYY-MM-DD-HHMM
539-
SNAPGLOB="$opt_prefix${opt_label:+?$opt_label}????????????????"
583+
# The expression for matching old snapshots.
584+
SNAPGLOB="$(setup_snap_glob $opt_prefix${opt_label:+?$opt_label} $opt_date_format)"
540585

541586
if [ -n "$opt_do_snapshots" ]
542587
then

0 commit comments

Comments
 (0)