Improve logging

This commit is contained in:
Thorsten Spille 2023-03-04 22:40:47 +01:00
parent 6e1d973f61
commit 221fd22ba5

View File

@ -105,43 +105,50 @@ else
sshcipher=-cchacha20-poly1305@openssh.com sshcipher=-cchacha20-poly1305@openssh.com
fi fi
include_list="Included datasets:\n"
exclude_list="Excluded datasets:\n"
IFS=$'\n' IFS=$'\n'
for zvol in $($ssh $sshcipher $sshport $source "zfs get -H -o name,value,source -t filesystem,volume $tag"); do for zvol in $($ssh $sshcipher $sshport $source "zfs get -H -o name,value,source -t filesystem,volume $tag"); do
name=$(echo $zvol | cut -f1) name=$(echo $zvol | cut -f1)
if [[ "$(echo $zvol | cut -f2)" == "subvols" ]] && [[ $(echo $zvol | grep -E $subvol_source) ]]; then if [[ "$(echo $zvol | cut -f2)" == "subvols" ]] && [[ $(echo $zvol | grep -E $subvol_source) ]]; then
log "Including $name" include_list="${include_list}${name}\n"
syncvols=("${syncvols[@]}" "$name") syncvols=("${syncvols[@]}" "$name")
elif [[ "$(echo $zvol | cut -f2)" == "all" ]];then elif [[ "$(echo $zvol | cut -f2)" == "all" ]];then
log "Including $name" include_list="${include_list}${name}\n"
syncvols=("${syncvols[@]}" "$name") syncvols=("${syncvols[@]}" "$name")
else else
log "Excluding $name" exclude_list="${exclude_list}${name}\n"
fi fi
done done
log "$include_list\n"
log "$exclude_list\n"
for name in "${syncvols[@]}"; do for name in "${syncvols[@]}"; do
if [[ $debug == "-v" ]]; then log "[DEBUG] Checking $name"; fi
IFS=$' ' IFS=$' '
if [ $($zfs list -H -s creation -t snapshot $target/$name > /dev/null 2>&1 ; echo $?) -gt 0 ]; then if [ $($zfs list -H $target/$name > /dev/null 2>&1 ; echo $?) -gt 0 ]; then
# create parent datasets if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi
prefix="" prefix=""
for part in $(echo $target/$(echo $name | cut -d'/' -f1) | sed "s/\// /g"); do for part in $(echo $target/$(echo $name | cut -d'/' -f1) | sed "s/\// /g"); do
if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then
if [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi
log "Creating $prefix$part" log "Creating $prefix$part"
if [[ $prefix/$part == $target ]]; then if [[ $prefix/$part == $target ]]; then
if [[ $debug == "-v" ]]; then log "[DEBUG] Disabling com.sun:auto-snapshot on $prefix$part"; fi
autosnap=-ocom.sun:auto-snapshot=false autosnap=-ocom.sun:auto-snapshot=false
fi fi
$zfs create $autosnap -p $prefix$part $zfs create $autosnap -p $prefix$part
else else
if [[ $prefix/$part == $target ]] && [[ $(zfs get -H -o value,source com.sun:auto-snapshot $prefix/$part) != "false local" ]]; then 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 $zfs set com.sun:auto-snapshot=false $prefix/$part
fi fi
fi fi
prefix="$prefix$part/" prefix="$prefix$part/"
done done
# start initial replication if [[ $debug == "-v" ]]; then log "[DEBUG] Start initial replication"; fi
IFS=$'\n' 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 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)" log "Start initial replication: $snap => $target/$(echo $name | cut -d'/' -f1)"
@ -149,7 +156,7 @@ for name in "${syncvols[@]}"; do
done done
fi fi
# replicate incremental if [[ $debug == "-v" ]]; then log "[DEBUG] Start incremental replication"; fi
guid=$($zfs list -H -o guid -s creation -t snapshot $target/$name | tail -1) 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") last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1")
IFS=$'\n' IFS=$'\n'
@ -159,22 +166,24 @@ for name in "${syncvols[@]}"; do
last=$snap last=$snap
done done
# cleanup old snapshots if [[ $debug == "-v" ]]; then log "[DEBUG] Start deletion of old snapshots"; fi
filter=$(echo -e $snapshot_filter | sed "s/|/\n/g") filter=$(echo -e $snapshot_filter | sed "s/|/\n/g")
IFS=$'\n' IFS=$'\n'
for interval in $filter ; do 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") 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 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) 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)
delete_count=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | wc -l) 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 for snap in $snaps_to_delete; do
if [[ $delete_count -gt $min_keep ]]; then if [[ $snap_count -gt $min_keep ]]; then
log "Deleting $snap" log "Deleting $snap"
if [[ $debug == "-v" ]]; then log "[DEBUG] delete_count=$delete_count, min_keep=$min_keep"; fi if [[ $debug == "-v" ]]; then log "[DEBUG] delete_count=$delete_count, min_keep=$min_keep"; fi
$zfs destroy $debug $snap $zfs destroy $debug $snap
delete_count=$(expr $delete_count - 1) snap_count=$(expr $snap_count - 1)
else else
if [[ $debug == "-v" ]]; then log "[DEBUG] Skipping deletion of $snap. delete_count=$delete_count, min_keep=$min_keep"; fi if [[ $debug == "-v" ]]; then log "[DEBUG] Skipping deletion of $snap. snap_count=$snap_count, min_keep=$min_keep"; fi
fi fi
done done
fi fi