Skip datasets without snapshots

This commit is contained in:
Thorsten Spille 2023-03-04 23:13:36 +01:00
parent 120e057ea2
commit 034431a935

View File

@ -126,66 +126,70 @@ log "$exclude_list"
for name in "${syncvols[@]}"; do for name in "${syncvols[@]}"; do
if [[ $debug == "-v" ]]; then log "[DEBUG] Checking $name"; fi if [[ $debug == "-v" ]]; then log "[DEBUG] Checking $name"; fi
IFS=$' ' if [[ $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name 2>/dev/null | grep -E \"$snapshot_filter\" | wc -l") -gt 0 ]]; then
if [ $($zfs list -H $target/$name > /dev/null 2>&1 ; echo $?) -gt 0 ]; then IFS=$' '
if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi if [ $($zfs list -H $target/$name > /dev/null 2>&1 ; echo $?) -gt 0 ]; then
prefix="" if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi
for part in $(echo $target/$(echo $name | cut -d'/' -f1) | sed "s/\// /g"); do prefix=""
if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then for part in $(echo $target/$(echo $name | cut -d'/' -f1) | sed "s/\// /g"); do
if [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then
log "Creating $prefix$part" if [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi
if [[ $prefix/$part == $target ]]; then log "Creating $prefix$part"
if [[ $debug == "-v" ]]; then log "[DEBUG] Disabling com.sun:auto-snapshot on $prefix$part"; fi if [[ $prefix/$part == $target ]]; then
autosnap=-ocom.sun:auto-snapshot=false if [[ $debug == "-v" ]]; then log "[DEBUG] Disabling com.sun:auto-snapshot on $prefix$part"; fi
fi autosnap=-ocom.sun:auto-snapshot=false
$zfs create $autosnap -p $prefix$part fi
else $zfs create $autosnap -p $prefix$part
if [[ $prefix/$part == $target ]] && [[ $(zfs get -H -o value,source com.sun:auto-snapshot $prefix/$part) != "false local" ]]; then
if [[ $debug == "-v" ]]; then log "[DEBUG] Disabling com.sun:auto-snapshot on $prefix$part"; fi
$zfs set com.sun:auto-snapshot=false $prefix/$part
fi
fi
prefix="$prefix$part/"
done
if [[ $debug == "-v" ]]; then log "[DEBUG] Start initial replication"; fi
IFS=$'\n'
for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do
log "Start initial replication: $snap => $target/$(echo $name | cut -d'/' -f1)"
$ssh $ssh_ipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive -x mountpoint -x canmount -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | cut -d'/' -f1)
done
fi
if [[ $debug == "-v" ]]; then log "[DEBUG] Start incremental replication"; fi
guid=$($zfs list -H -o guid -s creation -t snapshot $target/$name | tail -1)
last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1")
IFS=$'\n'
for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do
log "Replicating delta of $last => $snap to $target/$name"
$ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x mountpoint -x canmount -x $tag -x com.sun:auto-snapshot -F $debug $target/$name
last=$snap
done
if [[ $debug == "-v" ]]; then log "[DEBUG] Start deletion of old snapshots"; fi
filter=$(echo -e $snapshot_filter | sed "s/|/\n/g")
IFS=$'\n'
for interval in $filter ; do
if [[ $debug == "-v" ]]; then log "[DEBUG] Checking interval $interval"; fi
guid=$($ssh $sshcipher $sshport $source "zfs list -H -o guid,name -S creation -t snapshot $name | grep $interval | cut -f1 | tail -1")
if [[ "$(echo -e "$guid" | sed 's/\n//g')" != "" ]]; then
snaps_to_delete=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | grep --after-context=200 $guid | grep -v $guid | cut -f1)
snap_count=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | wc -l)
for snap in $snaps_to_delete; do
if [[ $snap_count -gt $min_keep ]]; then
log "Deleting $snap"
if [[ $debug == "-v" ]]; then log "[DEBUG] snap_count=$snap_count, min_keep=$min_keep"; fi
$zfs destroy $debug $snap
snap_count=$(expr $snap_count - 1)
else else
if [[ $debug == "-v" ]]; then log "[DEBUG] Skipping deletion of $snap. snap_count=$snap_count, min_keep=$min_keep"; fi if [[ $prefix/$part == $target ]] && [[ $(zfs get -H -o value,source com.sun:auto-snapshot $prefix/$part) != "false local" ]]; then
if [[ $debug == "-v" ]]; then log "[DEBUG] Disabling com.sun:auto-snapshot on $prefix$part"; fi
$zfs set com.sun:auto-snapshot=false $prefix/$part
fi
fi fi
prefix="$prefix$part/"
done
if [[ $debug == "-v" ]]; then log "[DEBUG] Start initial replication"; fi
IFS=$'\n'
for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do
log "Start initial replication: $snap => $target/$(echo $name | cut -d'/' -f1)"
$ssh $ssh_ipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive -x mountpoint -x canmount -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | cut -d'/' -f1)
done done
fi fi
done
if [[ $debug == "-v" ]]; then log "[DEBUG] Start incremental replication"; fi
guid=$($zfs list -H -o guid -s creation -t snapshot $target/$name | tail -1)
last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1")
IFS=$'\n'
for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do
log "Replicating delta of $last => $snap to $target/$name"
$ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x mountpoint -x canmount -x $tag -x com.sun:auto-snapshot -F $debug $target/$name
last=$snap
done
if [[ $debug == "-v" ]]; then log "[DEBUG] Start deletion of old snapshots"; fi
filter=$(echo -e $snapshot_filter | sed "s/|/\n/g")
IFS=$'\n'
for interval in $filter ; do
if [[ $debug == "-v" ]]; then log "[DEBUG] Checking interval $interval"; fi
guid=$($ssh $sshcipher $sshport $source "zfs list -H -o guid,name -S creation -t snapshot $name | grep $interval | cut -f1 | tail -1")
if [[ "$(echo -e "$guid" | sed 's/\n//g')" != "" ]]; then
snaps_to_delete=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | grep --after-context=200 $guid | grep -v $guid | cut -f1)
snap_count=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | wc -l)
for snap in $snaps_to_delete; do
if [[ $snap_count -gt $min_keep ]]; then
log "Deleting $snap"
if [[ $debug == "-v" ]]; then log "[DEBUG] snap_count=$snap_count, min_keep=$min_keep"; fi
$zfs destroy $debug $snap
snap_count=$(expr $snap_count - 1)
else
if [[ $debug == "-v" ]]; then log "[DEBUG] Skipping deletion of $snap. snap_count=$snap_count, min_keep=$min_keep"; fi
fi
done
fi
done
else
if [[ $debug == "-v" ]]; then log "[DEBUG] No snapshots found for $name with filter $snapshot_filter"; fi
fi
done done