Skip to content

Commit 8cfeb12

Browse files
committed
Implement --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 d063d3b commit 8cfeb12

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/zfs-auto-snapshot.8

+3
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

+50-5
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=''
@@ -66,6 +67,7 @@ print_usage ()
6667
{
6768
echo "Usage: $0 [options] [-l label] <'//' | name [name...]>
6869
--default-exclude Exclude datasets if com.sun:auto-snapshot is unset.
70+
-D, --date=FORMAT Date format. Default '%F-%H%M'.
6971
-d, --debug Print debugging messages.
7072
-e, --event=EVENT Set the com.sun:auto-snapshot-desc property to EVENT.
7173
--fast Use a faster zfs list invocation.
@@ -160,6 +162,45 @@ do_run () # [argv]
160162
}
161163

162164

165+
setup_snap_glob ()
166+
{
167+
local base="$1"
168+
local format="$2"
169+
170+
# The dash to mimic the separator between prefix/label
171+
# and DATE in SNAPNAME.
172+
echo -n "$base"-
173+
echo $format | \
174+
awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
175+
while read char; do
176+
[ "$char" = "%" ] && continue
177+
178+
if [ "$char" = "." ]; then
179+
echo -n "."
180+
elif [ "$char" = "-" ]; then
181+
echo -n "-"
182+
else
183+
# Given the format char, create
184+
# a string with only that.
185+
# So if we said '%Y', the percentage
186+
# sign would have been filtered out
187+
# at the top of the loop, leaving the
188+
# 'Y' here. So str=2014.
189+
str=$(date +"%$char")
190+
191+
# Print the lenght of that with
192+
# question marks (4 with the '%Y'
193+
# example above).
194+
i=0
195+
while [ $i -lt ${#str} ]; do
196+
echo -n "?"
197+
i=$((i + 1))
198+
done
199+
fi
200+
done
201+
}
202+
203+
163204
do_snapshots () # properties, flags, snapname, oldglob, [targets...]
164205
{
165206
local PROPS="$1"
@@ -335,12 +376,12 @@ $jj"
335376
GETOPT=$(getopt \
336377
--longoptions=default-exclude,dry-run,fast,skip-scrub,recursive \
337378
--longoptions=event:,keep:,label:,prefix:,sep: \
338-
--longoptions=debug,help,quiet,syslog,verbose \
379+
--longoptions=date:,debug,help,quiet,syslog,verbose \
339380
--longoptions=pre-snapshot:,post-snapshot:,destroy-only \
340381
--longoptions=send-full:,send-incr:,send-opts:,recv-opts: \
341382
--longoptions=send-ssh-opts:,send-mbuf-opts:,pre-send:,post-send: \
342383
--longoptions=send-fallback \
343-
--options=dnshe:l:k:p:rs:qgv \
384+
--options=D:dnshe:l:k:p:rs:qgv \
344385
-- "$@" ) \
345386
|| exit 128
346387

@@ -349,6 +390,10 @@ eval set -- "$GETOPT"
349390
while [ "$#" -gt '0' ]
350391
do
351392
case "$1" in
393+
(-D|--date)
394+
opt_date_format="$2"
395+
shift 2
396+
;;
352397
(-d|--debug)
353398
opt_debug='1'
354399
opt_quiet=''
@@ -702,13 +747,13 @@ SNAPPROP="-o com.sun:auto-snapshot-desc='$opt_event'"
702747

703748
# ISO style date; fifteen characters: YYYY-MM-DD-HHMM
704749
# On Solaris %H%M expands to 12h34.
705-
DATE=$(date --utc +%F-%H%M)
750+
DATE=$(date --utc +"$opt_date_format")
706751

707752
# The snapshot name after the @ symbol.
708753
SNAPNAME="$opt_prefix${opt_label:+$opt_sep$opt_label}-$DATE"
709754

710-
# The expression for matching old snapshots. -YYYY-MM-DD-HHMM
711-
SNAPGLOB="$opt_prefix${opt_label:+?$opt_label}????????????????"
755+
# The expression for matching old snapshots.
756+
SNAPGLOB="$(setup_snap_glob $opt_prefix${opt_label:+?$opt_label} $opt_date_format)"
712757

713758
if [ -n "$opt_do_snapshots" ]
714759
then

0 commit comments

Comments
 (0)