diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 84021f2..3116335 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -126,66 +126,70 @@ log "$exclude_list" for name in "${syncvols[@]}"; do if [[ $debug == "-v" ]]; then log "[DEBUG] Checking $name"; fi - IFS=$' ' - if [ $($zfs list -H $target/$name > /dev/null 2>&1 ; echo $?) -gt 0 ]; then - if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi - prefix="" - 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 [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi - log "Creating $prefix$part" - 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 - fi - $zfs create $autosnap -p $prefix$part - else - 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) + 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 + IFS=$' ' + if [ $($zfs list -H $target/$name > /dev/null 2>&1 ; echo $?) -gt 0 ]; then + if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi + prefix="" + 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 [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi + log "Creating $prefix$part" + 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 + fi + $zfs create $autosnap -p $prefix$part 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 + 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 - 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 \ No newline at end of file