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

Add holding replication snapshots - #29 #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 33 additions & 3 deletions zfsbud.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ for arg in "$@"; do
recursive_send="-R"
recursive_create="-r"
recursive_destroy="-r"
recursive_hold="-r"
shift
;;
-n | --no-resume)
Expand Down Expand Up @@ -276,8 +277,14 @@ rotate_snapshots() {
&& [[ "${source_snapshots[i]}" != "${source_snapshots[-1]}" ]] ; then
if [ ! -v last_snapshot_common ] \
|| [[ "${source_snapshots[i]}" != "$dataset@$last_snapshot_common" ]] ; then
msg "Deleting source snapshot: ${source_snapshots[i]}"
[ ! -v dry_run ] && zfs destroy $recursive_destroy -f "${source_snapshots[i]}"
# Do not delete held snapshots
zfs holds $recursive_hold -H ${source_snapshots[i]} | grep "keep"
if [ $? -eq 0 ]; then
msg "Deleting source snapshot: ${source_snapshots[i]} <<--- on hold"
else
msg "Deleting source snapshot: ${source_snapshots[i]}"
[ ! -v dry_run ] && zfs destroy $recursive_destroy -f "${source_snapshots[i]}"
fi
unset "source_snapshots[i]"
fi
fi
Expand Down Expand Up @@ -315,6 +322,15 @@ send_initial() {
fi
msg "Initial snapshot has been sent."
last_snapshot_common="${first_snapshot_source#*@}"
# keep on hold both source and destination replication snapshots to prevent even accidental deletion
if [ ! -v dry_run ]; then
zfs hold $recursive_hold keep $first_snapshot_source
if [ -v remote_shell ]; then
$remote_shell "zfs hold $recursive_hold keep $destination_parent_dataset/$dataset_name@$last_snapshot_common"
else
zfs hold $recursive_hold keep $destination_parent_dataset/$dataset_name@$last_snapshot_common
fi
fi
}

send_resume() {
Expand All @@ -340,14 +356,28 @@ send_incremental() {
fi
msg "Most recent common snapshot: $last_snapshot_common"
msg "Sending incremental changes to destination..."

if [ ! -v dry_run ]; then
if [ -v remote_shell ]; then
! zfs send -w $recursive_send $verbose -I "$dataset@$last_snapshot_common" "$last_snapshot_source" | $remote_shell "zfs recv $resume -F -d -u $destination_parent_dataset" && return 1
else
! zfs send -w $recursive_send $verbose -I "$dataset@$last_snapshot_common" "$last_snapshot_source" | zfs recv $resume -F -d -u "$destination_parent_dataset" && return 1
fi
fi

# release hold from last common snapshots and hold on new latest snapshots
if [ ! -v dry_run ]; then
zfs release $recursive_hold keep $dataset@$last_snapshot_common
zfs hold $recursive_hold keep $dataset@${last_snapshot_source#*@}
if [ -v remote_shell ]; then
$remote_shell "zfs release $recursive_hold keep $destination_parent_dataset/$dataset_name@$last_snapshot_common"
$remote_shell "zfs hold $recursive_hold keep $destination_parent_dataset/$dataset_name@${last_snapshot_source#*@}"
else
zfs release $recursive_hold keep $destination_parent_dataset/$dataset_name@$last_snapshot_common
zfs hold $recursive_hold keep $destination_parent_dataset/$dataset_name@${last_snapshot_source#*@}
fi
fi

msg "Incremental changes have been sent."
}

Expand Down