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

Remove 'label' (eg frequent/hourly/daily etc) from snapshot name and place in snapshot property #22

Closed
wants to merge 2 commits into from
Closed
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
31 changes: 18 additions & 13 deletions src/zfs-auto-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
local GLOB="$4"
local TARGETS="$5"
local KEEP=''

local LABEL="$6"

# global DESTRUCTION_COUNT
# global SNAPSHOT_COUNT
# global WARNING_COUNT
Expand All @@ -168,25 +169,29 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
# including the one that was just recently created.
test -z "$opt_keep" && continue
KEEP="$opt_keep"

# ASSERT: The old snapshot list is sorted by increasing age.
for jj in $SNAPSHOTS_OLD
# Match on both $GLOB *and* label.
while IFS=' ' read -ra arrjj
do
# Check whether this is an old snapshot of the filesystem.
if [ -z "${jj#$ii@$GLOB}" ]
if [ -z "${arrjj[1]#$ii@$GLOB}" ]
then
# Check whether the snapshot has the same label property
test "${arrjj[0]}" = "$LABEL" || continue

KEEP=$(( $KEEP - 1 ))
if [ "$KEEP" -le '0' ]
then
if do_run "zfs destroy $FLAGS '$jj'"
if do_run "zfs destroy $FLAGS '${arrjj[1]}'"
then
DESTRUCTION_COUNT=$(( $DESTRUCTION_COUNT + 1 ))
else
WARNING_COUNT=$(( $WARNING_COUNT + 1 ))
fi
fi
fi
done
done <<< "$SNAPSHOTS_OLD"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Debian Wheezy this line gives me a Syntax error: redirection unexpected. The dash shell does not have the <<< operator: http://stackoverflow.com/a/2462357/2728336

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quick workaround for me was to define bash as the specific shell: #!/bin/sh -> #!/bin/bash. This might affect Ubuntu also.

Copy link

@exenza exenza May 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've merged @leecallen as per instructions on the wiki and I've changed to /bin/bash but now I'm getting echoed the error "The filesystem argument list is empty." (line 350 is the print)
I'm on Debian 8.8. I've installed via "make install" because after the merge with leecallen it fails to build the deb package

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when running zfs-auto-snapshot using bash without arguments, I did get the error that you mention ("The filesystem argument list is empty"). However, when running it with the correct arguments, it worked fine. I used the following arguments:

bash ./zfs-auto-snapshot --quiet --syslog --label=frequent --keep=4 //

done
}

Expand Down Expand Up @@ -347,10 +352,10 @@ ZFS_LIST=$(env LC_ALL=C zfs list -H -t filesystem,volume -s name \

if [ -n "$opt_fast_zfs_list" ]
then
SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -o name -s name|grep $opt_prefix |awk '{ print substr( $0, length($0) - 14, length($0) ) " " $0}' |sort -r -k1,1 -k2,2|awk '{ print substr( $0, 17, length($0) )}') \
SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -o com.sun:auto-snapshot-label,name -s name|grep $opt_prefix |awk '{ print substr( $0, length($0) - 14, length($0) ) " " $0}' |sort -r -k1,1 -k3,2|awk '{ print substr( $0, 17, length($0) )}') \
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

produce the listing with label first, then name, so the awk/sort stuff works correctly

|| { print_log error "zfs list $?: $SNAPSHOTS_OLD"; exit 137; }
else
SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -S creation -o name) \
SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -S creation -o com.sun:auto-snapshot-label,name) \
|| { print_log error "zfs list $?: $SNAPSHOTS_OLD"; exit 137; }
fi

Expand Down Expand Up @@ -500,17 +505,17 @@ done

# Linux lacks SMF and the notion of an FMRI event, but always set this property
# because the SUNW program does. The dash character is the default.
SNAPPROP="-o com.sun:auto-snapshot-desc='$opt_event'"
SNAPPROP="-o com.sun:auto-snapshot-desc='$opt_event' -o com.sun:auto-snapshot-label=$opt_label"

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

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

# The expression for matching old snapshots. -YYYY-MM-DD-HHMM
SNAPGLOB="$opt_prefix${opt_label:+?$opt_label}????????????????"
SNAPGLOB="${opt_prefix}????????????????"

test -n "$TARGETS_REGULAR" \
&& print_log info "Doing regular snapshots of $TARGETS_REGULAR"
Expand All @@ -521,8 +526,8 @@ test -n "$TARGETS_RECURSIVE" \
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" "$SNAPGLOB" "$TARGETS_REGULAR" "$opt_label"
do_snapshots "$SNAPPROP" "-r" "$SNAPNAME" "$SNAPGLOB" "$TARGETS_RECURSIVE" "$opt_label"

print_log notice "@$SNAPNAME," \
"$SNAPSHOT_COUNT created," \
Expand Down