diff --git a/src/zfs-auto-snapshot.8 b/src/zfs-auto-snapshot.8 index 87fc8b3..08ac64c 100644 --- a/src/zfs-auto-snapshot.8 +++ b/src/zfs-auto-snapshot.8 @@ -85,6 +85,10 @@ snapshot all datasets, then run post-snapshot command(s) and clean up with zfs-auto-snapshot \fB\-\-destroy-only\fR. .TP +\fB\-L\fR, \fB\-\-localtime\fR +Use local time instead of UTC for snapshot names. In this case +the timezone abbreviation is added to the name. +.TP name Filesystem and volume names, or '//' for all ZFS datasets. .SH SEE ALSO diff --git a/src/zfs-auto-snapshot.sh b/src/zfs-auto-snapshot.sh old mode 100644 new mode 100755 index 8bbf19d..9167ba7 --- a/src/zfs-auto-snapshot.sh +++ b/src/zfs-auto-snapshot.sh @@ -43,6 +43,8 @@ opt_pre_snapshot='' opt_post_snapshot='' opt_do_snapshots=1 opt_min_size=0 +opt_date='--utc' +opt_timezone='' # Global summary statistics. DESTRUCTION_COUNT='0' @@ -74,6 +76,8 @@ print_usage () -r, --recursive Snapshot named filesystem and all descendants. -v, --verbose Print info messages. --destroy-only Only destroy older snapshots, do not create new ones. + -L, --localtime Use local time instead of UTC for snapshot names. In this case + the timezone abbreviation is added to the name. name Filesystem and volume names, or '//' for all ZFS datasets. " } @@ -150,7 +154,7 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...] local PROPS="$1" local FLAGS="$2" local NAME="$3" - local GLOB="$4" + local NAMETRUNC="$4" local TARGETS="$5" local KEEP='' local RUNSNAP=1 @@ -203,19 +207,21 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...] for jj in $SNAPSHOTS_OLD do # Check whether this is an old snapshot of the filesystem. - if [ -z "${jj#$ii@$GLOB}" ] - then - KEEP=$(( $KEEP - 1 )) - if [ "$KEEP" -le '0' ] - then - if do_run "zfs destroy -d $FLAGS '$jj'" + trunc="$ii@$NAMETRUNC" + case "${jj}" in + (${trunc}*) + KEEP=$(( $KEEP - 1 )) + if [ "$KEEP" -le '0' ] then - DESTRUCTION_COUNT=$(( $DESTRUCTION_COUNT + 1 )) - else - WARNING_COUNT=$(( $WARNING_COUNT + 1 )) + if do_run "zfs destroy -d $FLAGS '$jj'" + then + DESTRUCTION_COUNT=$(( $DESTRUCTION_COUNT + 1 )) + else + WARNING_COUNT=$(( $WARNING_COUNT + 1 )) + fi fi - fi - fi + ;; + esac done done } @@ -228,9 +234,9 @@ GETOPT=$(getopt \ --longoptions=default-exclude,dry-run,fast,skip-scrub,recursive \ --longoptions=event:,keep:,label:,prefix:,sep: \ --longoptions=debug,help,quiet,syslog,verbose \ - --longoptions=pre-snapshot:,post-snapshot:,destroy-only \ + --longoptions=pre-snapshot:,post-snapshot:,destroy-only,localtime \ --longoptions=min-size: \ - --options=dnshe:l:k:p:rs:qgvm: \ + --options=dnshe:l:k:p:rs:qgvm:L \ -- "$@" ) \ || exit 128 @@ -356,6 +362,11 @@ do opt_do_snapshots='' shift 1 ;; + (-L|--localtime) + opt_date='' + opt_timezone='-%Z' + shift 1 + ;; (--) shift 1 break @@ -564,15 +575,15 @@ done # because the SUNW program does. The dash character is the default. SNAPPROP="-o com.sun:auto-snapshot-desc='$opt_event'" -# ISO style date; fifteen characters: YYYY-MM-DD-HHMM +# ISO style date; 15-21 characters depending on the timezone: YYYY-MM-DD-HHMM (UTC) or YYYY-MM-DD-HHMM-XXXXX (local time) # On Solaris %H%M expands to 12h34. -DATE=$(date --utc +%F-%H%M) +DATE=$(date $opt_date +%F-%H%M$opt_timezone) # The snapshot name after the @ symbol. SNAPNAME="${opt_prefix:+$opt_prefix$opt_sep}${opt_label:+$opt_label}-$DATE" -# The expression for matching old snapshots. -YYYY-MM-DD-HHMM -SNAPGLOB="${opt_prefix:+$opt_prefix$opt_sep}${opt_label:+$opt_label}-???????????????" +# The snapshot name truncated of the date for matching old snapshots. +SNAPNAMETRUNC="${opt_prefix:+$opt_prefix$opt_sep}${opt_label:+$opt_label}" if [ -n "$opt_do_snapshots" ] then @@ -600,8 +611,8 @@ fi test -n "$opt_dry_run" \ && print_log info "Doing a dry run. Not running these commands..." -do_snapshots "$SNAPPROP" "" "$SNAPNAME" "$SNAPGLOB" "$TARGETS_REGULAR" -do_snapshots "$SNAPPROP" "-r" "$SNAPNAME" "$SNAPGLOB" "$TARGETS_RECURSIVE" +do_snapshots "$SNAPPROP" "" "$SNAPNAME" "$SNAPNAMETRUNC" "$TARGETS_REGULAR" +do_snapshots "$SNAPPROP" "-r" "$SNAPNAME" "$SNAPNAMETRUNC" "$TARGETS_RECURSIVE" print_log notice "@$SNAPNAME," \ "$SNAPSHOT_COUNT created," \