fix dest path when using filters

This commit is contained in:
matthias@arch 2023-10-26 18:16:18 +02:00
parent ca20881999
commit fa7b4d89ee

View File

@ -26,8 +26,8 @@ mkrypt=/usr/bin/mkrypt
mkrypt_flags=() mkrypt_flags=()
rsync_flags=(-ruh) rsync_flags=(-ruh)
# rsync_flags+=(--rsh="ssh -p 42") # rsync_flags+=(--rsh="ssh -p 22")
# r - relative # r - recursive
# v - verbose # v - verbose
# Ut - preserve modification&access times # Ut - preserve modification&access times
# u - update, skip files that are newer on the receiver # 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
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..." [[ $v -ge 1 ]] && printf "$FMT_MESSAGE" "Performing dryrun" "to generate list of files that will be deleted from $receiver..."
echo "" > $TMP_FILE echo "" > $TMP_FILE
for path in "${filtered_paths[@]}"; do for path in "${filtered_paths[@]}"; do
dest_subdir=$receiver$(basename $path) _sync_set_dest
rsync "${rsync_flags[@]}" -v --dry-run --delete $path $dest_subdir | grep deleting | sed "s(deleting (${BACKUP_SUBDIR}/(" >> $TMP_FILE # 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 done
# print files that will be deleted and ask if continue # print files that will be deleted and ask if continue
# grep deleting $TMP_FILE | sed "s(deleting (${receiver}/(" | less # grep deleting $TMP_FILE | sed "s(deleting (${receiver}/(" | less
@ -115,15 +142,21 @@ sync_sender_to_receiver()
# actual syncing # actual syncing
for path in "${filtered_paths[@]}"; do for path in "${filtered_paths[@]}"; do
[[ $v -ge 1 ]] && printf "$FMT_SYNC" "$path" [[ $v -ge 1 ]] && printf "$FMT_SYNC" "$path"
if [[ -d "$path" ]]; then _sync_set_dest
dest="$receiver"$(basename "$path") #if [[ -d "$path" ]]; then
elif [[ -f $path ]]; then # if [[ -n "$relative" ]]; then
dest="$receiver" # dest="$receiver"
elif [[ "$sender" == *:* ]]; then # if sender is remote, assume the path is a dir if it has a slash and a file otherwise # else
[[ "$path" == *"/" ]] && dest="$receiver"$(basename "$path") || dest="$receiver" # dest="$receiver"$(basename "$path")
else # fi
printf "$FMT_ERROR" "Invalid path: $path"; exit 1 #elif [[ -f $path ]]; then
fi # 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 if [[ -n $encrypt || -n $decrypt ]]; then
mkdir -p "$TMP_DIR" mkdir -p "$TMP_DIR"
tmp_source="$TMP_DIR" tmp_source="$TMP_DIR"
@ -323,6 +356,9 @@ while (( "$#" )); do
--exclude) --exclude)
exclude=1 exclude=1
shift ;; shift ;;
-R|--relative)
relative=1
shift ;;
-*|--*=) # unsupported flags -*|--*=) # unsupported flags
printf "$FMT_ERROR" "Unsupported flag $1" >&2 printf "$FMT_ERROR" "Unsupported flag $1" >&2
exit 1 ;; exit 1 ;;
@ -404,6 +440,11 @@ fi
[[ $v -ge 3 ]] && printf "$FMT_MESSAGE" "Filtered paths:" "${filtered_paths[*]}" [[ $v -ge 3 ]] && printf "$FMT_MESSAGE" "Filtered paths:" "${filtered_paths[*]}"
IFS=$ifs IFS=$ifs
# apply correct base flags
if [[ -n $relative ]]; then
rsync_flags+=(--relative)
fi
# sanity checks # sanity checks
[[ -z $filtered_paths ]] && { printf "$FMT_ERROR" "Missing valid paths."; exit 1; } [[ -z $filtered_paths ]] && { printf "$FMT_ERROR" "Missing valid paths."; exit 1; }
[[ -z $receiver ]] && { printf "$FMT_ERROR" "Missing receiver. Specifiy with --receiver"; exit 1; } [[ -z $receiver ]] && { printf "$FMT_ERROR" "Missing receiver. Specifiy with --receiver"; exit 1; }