Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--localtime: Use local time instead of UTC (include timezone abbrevation in the name) #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/zfs-auto-snapshot.8
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 31 additions & 20 deletions src/zfs-auto-snapshot.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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.
"
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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

Expand Down Expand Up @@ -356,6 +362,11 @@ do
opt_do_snapshots=''
shift 1
;;
(-L|--localtime)
opt_date=''
opt_timezone='-%Z'
shift 1
;;
(--)
shift 1
break
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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," \
Expand Down