From fa7b4d89eee3e9fa8d0d56388f3e725d4954f9c8 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Thu, 26 Oct 2023 18:16:18 +0200 Subject: [PATCH] fix dest path when using filters --- msynk.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/msynk.sh b/msynk.sh index 154e066..e5d6179 100755 --- a/msynk.sh +++ b/msynk.sh @@ -26,8 +26,8 @@ mkrypt=/usr/bin/mkrypt mkrypt_flags=() rsync_flags=(-ruh) -# rsync_flags+=(--rsh="ssh -p 42") -# r - relative +# rsync_flags+=(--rsh="ssh -p 22") +# r - recursive # v - verbose # Ut - preserve modification&access times # u - update, skip files that are newer on the receiver @@ -75,6 +75,27 @@ check_path_in_args() } +_sync_set_dest() { + if [[ -d "$path" ]]; then + if [[ -n "$relative" ]]; then + dest="$receiver" + else + dest="$receiver"$(basename "$path") + fi + elif [[ -f $path ]]; then + dest="$receiver" + elif [[ "$sender" == *:* ]]; then # if sender is remote, assume the path is a dir if it has a slash and a file otherwise + if [[ "$path" == *\/ ]]; then + dest="$receiver"$(basename "$path") + else + dest="$receiver" + fi + else + printf "$FMT_ERROR" "Invalid path: $path"; exit 1 + fi +} + + # SYNC SENDER TO RECEIVER sync_sender_to_receiver() { @@ -83,8 +104,14 @@ sync_sender_to_receiver() [[ $v -ge 1 ]] && printf "$FMT_MESSAGE" "Performing dryrun" "to generate list of files that will be deleted from $receiver..." echo "" > $TMP_FILE for path in "${filtered_paths[@]}"; do - dest_subdir=$receiver$(basename $path) - rsync "${rsync_flags[@]}" -v --dry-run --delete $path $dest_subdir | grep deleting | sed "s(deleting (${BACKUP_SUBDIR}/(" >> $TMP_FILE + _sync_set_dest + # if [ -n $relative ]; then + # dest_subdir="$receiver" + # else + # dest_subdir="$receiver"$(basename "$path") + # fi + [[ $v -ge 3 ]] && printf "$FMT_CMD" "rsync ${rsync_flags[*]} -v --dry-run --delete $path $dest | grep deleting | sed \"s(deleting (${BACKUP_SUBDIR}/(\" >> $TMP_FILE" + rsync "${rsync_flags[@]}" -v --dry-run --delete $path $dest | grep deleting | sed "s(deleting (${BACKUP_SUBDIR}/(" >> $TMP_FILE done # print files that will be deleted and ask if continue # grep deleting $TMP_FILE | sed "s(deleting (${receiver}/(" | less @@ -115,15 +142,21 @@ sync_sender_to_receiver() # actual syncing for path in "${filtered_paths[@]}"; do [[ $v -ge 1 ]] && printf "$FMT_SYNC" "$path" - if [[ -d "$path" ]]; then - dest="$receiver"$(basename "$path") - elif [[ -f $path ]]; then - dest="$receiver" - elif [[ "$sender" == *:* ]]; then # if sender is remote, assume the path is a dir if it has a slash and a file otherwise - [[ "$path" == *"/" ]] && dest="$receiver"$(basename "$path") || dest="$receiver" - else - printf "$FMT_ERROR" "Invalid path: $path"; exit 1 - fi + _sync_set_dest + #if [[ -d "$path" ]]; then + # if [[ -n "$relative" ]]; then + # dest="$receiver" + # else + # dest="$receiver"$(basename "$path") + # fi + #elif [[ -f $path ]]; then + # dest="$receiver" + #elif [[ "$sender" == *:* ]]; then # if sender is remote, assume the path is a dir if it has a slash and a file otherwise + # #[[ "$path" == *"/" ]] && dest="$receiver"$(basename "$path") || dest="$receiver" + # dest=$receiver + #else + # printf "$FMT_ERROR" "Invalid path: $path"; exit 1 + #fi if [[ -n $encrypt || -n $decrypt ]]; then mkdir -p "$TMP_DIR" tmp_source="$TMP_DIR" @@ -323,6 +356,9 @@ while (( "$#" )); do --exclude) exclude=1 shift ;; + -R|--relative) + relative=1 + shift ;; -*|--*=) # unsupported flags printf "$FMT_ERROR" "Unsupported flag $1" >&2 exit 1 ;; @@ -404,6 +440,11 @@ fi [[ $v -ge 3 ]] && printf "$FMT_MESSAGE" "Filtered paths:" "${filtered_paths[*]}" IFS=$ifs +# apply correct base flags +if [[ -n $relative ]]; then + rsync_flags+=(--relative) +fi + # sanity checks [[ -z $filtered_paths ]] && { printf "$FMT_ERROR" "Missing valid paths."; exit 1; } [[ -z $receiver ]] && { printf "$FMT_ERROR" "Missing receiver. Specifiy with --receiver"; exit 1; }