Improve logging

This commit is contained in:
Thorsten Spille 2023-02-18 23:17:11 +01:00
parent 02e330de61
commit 68ac6b7bca

View File

@ -56,9 +56,13 @@ while getopts "hdc:" opt; do
done
shift $((OPTIND-1))
function log() {
echo "$(date +'%b %d %T') $1"
}
# load config file
if [ -f $conf ]; then
echo "Reading configuration $conf"
log "Reading configuration $conf"
source $conf
else
mkdir -p $(dirname $conf)
@ -70,18 +74,18 @@ tag=$tag
subvol_source="$subvol_source"
snapshot_filter="$snapshot_filter"
EOF
echo "Initial config file created. Please adjust and restart script. Exiting..."
log "Initial config file created. Please adjust and restart script. Exiting..."
usage 0
fi
if [[ $source == "" ]]; then
echo "source is empty, switching to local mode."
log "source is empty, switching to local mode."
ssh=
sshport=
echo -e "Configuration:\n\ttarget=$target\n\ttag=$tag\n\tsubvol_source=$subvol_source\n\tsnapshot_filter=$snapshot_filter\n"
log -e "Configuration:\n\ttarget=$target\n\ttag=$tag\n\tsubvol_source=$subvol_source\n\tsnapshot_filter=$snapshot_filter\n"
else
sshport=-p$sshport
echo -e "Configuration:\n\ttarget=$target\n\tsource=$source\n\tsshport=$sshport\n\ttag=$tag\n\tsubvol_source=$subvol_source\n\tsnapshot_filter=$snapshot_filter\n"
log -e "Configuration:\n\ttarget=$target\n\tsource=$source\n\tsshport=$sshport\n\ttag=$tag\n\tsubvol_source=$subvol_source\n\tsnapshot_filter=$snapshot_filter\n"
fi
# query source datasets/subvols to replicate
@ -89,13 +93,13 @@ IFS=$'\n'
for zvol in $($ssh $sshport $source "zfs get -H -o name,value,source -t filesystem,volume $tag"); do
name=$(echo $zvol | cut -f1)
if [[ "$(echo $zvol | cut -f2)" == "subvols" ]] && [[ $(echo $zvol | grep -E $subvol_source) ]]; then
echo "Including $name"
log "Including $name"
syncvols=("${syncvols[@]}" "$name")
elif [[ "$(echo $zvol | cut -f2)" == "all" ]];then
echo "Including $name"
log "Including $name"
syncvols=("${syncvols[@]}" "$name")
else
echo "Excluding $name"
log "Excluding $name"
fi
done
@ -107,7 +111,7 @@ for name in "${syncvols[@]}"; do
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
echo "Creating $prefix$part"
log "Creating $prefix$part"
$zfs create -p $prefix$part
fi
prefix="$prefix$part/"
@ -116,7 +120,7 @@ for name in "${syncvols[@]}"; do
# start initial replication
IFS=$'\n'
for snap in $($ssh $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do
echo "Start initial replication: $snap => $target/$(echo $name | cut -d'/' -f1)"
log "Start initial replication: $snap => $target/$(echo $name | cut -d'/' -f1)"
$ssh $sshport $source "zfs send -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
@ -126,7 +130,7 @@ for name in "${syncvols[@]}"; do
last=$($ssh $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1")
IFS=$'\n'
for snap in $($ssh $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
echo "Replicating delta of $last <=> $snap to $target/$name"
log "Replicating delta of $last => $snap to $target/$name"
$ssh $sshport $source "zfs send $debug -i $last $snap" | zfs receive -x mountpoint -x canmount -x $tag -x com.sun:auto-snapshot -F $debug $target/$name
last=$snap
done
@ -138,7 +142,7 @@ for name in "${syncvols[@]}"; do
guid=$($ssh $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
for snap in $($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | grep --after-context=200 $guid | grep -v $guid | cut -f1); do
echo "Deleting $snap"
log "Deleting $snap"
$zfs destroy $debug $snap
done
fi