+ExecJob() {
+ local what="$1"
+ local cmd="$2"
+
+ echo "Running job ${what}-exec command ..."
+ [ "$local" -eq 0 ] \
+ && cmd="$ssh_cmd ${user}@${system} $cmd"
+ echo "Start date (${what}-exec): $(date)"
+ echo "$cmd"
+ if [ "$DRYRUN" -eq 0 ]; then
+ $SHELL -c "$cmd"; local ret=$?
+ else
+ echo " *** Trial run, not executing ${what}-exec command!"
+ ret=0
+ fi
+ if [ $ret -eq 0 ]; then
+ echo "The ${what}-exec command completed with status 0, OK."
+ else
+ ErrorMsg "The ${what}-exec command completed with ERRORS, code $ret!"
+ fi
+ return $ret
+}
+
+GetFS() {
+ local dir="$1"
+
+ while [ -n "$dir" ]; do
+ findmnt -fn -o FSTYPE --raw "$dir" 2>/dev/null; local r=$?
+ if [ $r -eq 0 ]; then
+ return 0
+ elif [ $r -eq 127 ]; then
+ echo "UNKNOWN"
+ return 1
+ fi
+ dir=$(dirname "$dir") || return 1
+ done
+}
+
+CreateSubvolume() {
+ local volume="$1"
+ local fs
+ local dir
+
+ dir=$(dirname "$volume")
+ fs=$(GetFS "$dir")
+ case "$fs" in
+ "btrfs")
+ btrfs subvolume create "$volume" >/dev/null || return 1
+ ;;
+ "zfs")
+ zfs create "$(echo "$volume" | cut -c2-)" || return 1
+ ;;
+ *)
+ ErrorMsg "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
+ return 9
+ esac
+ return 0
+}
+
+CloneSubvolume() {
+ local source="$1"
+ local volume="$2"
+ local snapshot="$3"
+ local dir
+ local fs
+ local link_name
+
+ dir=$(dirname "source")
+ fs=$(GetFS "$source")
+ case "$fs" in
+ "btrfs")
+ btrfs subvolume snapshot "$source" "$snapshot" >/dev/null || return 1
+ ;;
+ "zfs")
+ zfs snapshot "$snapshot" || return 1
+ link_name="$(echo "$snapshot" | cut -d@ -f2-)"
+ ln -s \
+ "current/.zfs/snapshot/$link_name" \
+ "$(dirname "$volume")/$link_name"
+ ;;
+ *)
+ ErrorMsg "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
+ return 9
+ esac
+ return 0
+}
+
+RenameSubvolume() {
+ local source="$1"
+ local target="$2"
+ local fs
+
+ fs=$(GetFS "$source")
+ case "$fs" in
+ "btrfs")
+ mv "$source" "$target" || return 1
+ ;;
+ "zfs")
+ zfs rename \
+ "$(echo "$source" | cut -c2-)" \
+ "$(echo "$target" | cut -c2-)" \
+ || return 1
+ ;;
+ *)
+ ErrorMsg "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
+ return 9
+ esac
+ return 0
+}
+
+DeleteSubvolume() {
+ local volume="$1"
+ local fs
+ local id
+ local snapshot
+
+ fs=$(GetFS "$volume")
+ case "$fs" in
+ "btrfs")
+ btrfs subvolume delete "$volume" >/dev/null || return 1
+ ;;
+ "zfs")
+ id="$(basename "$volume")"
+ if [ -h "$volume" ]; then
+ snapshot="$(dirname "$volume")/current@$id"
+ else
+ snapshot="$volume"
+ fi
+ zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
+ [ -h "$volume" ] && rm "$volume"
+ ;;
+ *)
+ ErrorMsg "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
+ return 9
+ esac
+ return 0
+}
+
+Initialize_Last_SysTarget_Snapshot() {
+ sys_target="$1"
+ unset last
+ unset snapshot
+
+ fs=$(GetFS "$sys_target")
+ case "$fs" in
+ "btrfs")
+ # Search directory of last generation, if any
+ # shellcheck disable=SC2012
+ last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
+ if [ -n "$last" ]; then
+ if [ ! -d "$last" ]; then
+ ErrorMsg "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
+ echo
+ return 1
+ fi
+ fi
+ sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
+ snapshot="$sys_target"
+ ;;
+ "zfs")
+ # On ZFS, the last generation is always named "current"
+ if [ -e "$sys_target/current" ]; then
+ last="$sys_target/current"
+ if [ "$(uname)" = "Linux" ]; then
+ date=$(LC_ALL=C stat "$1" | grep "^Modify: " \
+ | cut -d':' -f2- | cut -d. -f1)
+ else
+ date=$(LC_ALL=C stat -f "%Sc" "$1")
+ fi
+ date=$(echo "$date" | sed -e's/^ //g' -e 's/[-:]//g' -e 's/ /-/g')
+
+ else
+ last=""
+ date="$(date +%Y%m%d-%H%M%S)"
+ fi
+ snapshot="$(echo "$sys_target/current" | cut -c2-)@$date"
+ sys_target="$sys_target/current"
+ ;;
+ *)
+ ErrorMsg "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
+ return 1
+ esac
+ return 0
+}
+
+# Search configuration file (last one is used as default!)
+for conf in \
+ "/usr/local/etc/backup-script.conf" \
+ "/etc/backup-script.conf" \
+ "${conf_d}/backup-script.conf" \
+ "/usr/local/etc/backup-script.conf" \
+; do
+ [ -r "$conf" ] && break
+done
+
+# Read in configuration file
+config_info="Configuration file is \"$conf\""
+if [ -r "$conf" ]; then
+ # shellcheck source=/dev/null
+ source "$conf"
+else
+ config_info="${config_info} (not readable, using defaults)"
+fi
+config_info="${config_info},\\nusing \"$conf_d\" as configuration directory."
+