Skip to content

Commit d269e71

Browse files
authored
Merge pull request #5 from buildplan/fix
fix ssh
2 parents 5bdcb0e + 6ff9056 commit d269e71

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

backup_script.sh

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ EXCLUDE_FILE_TMP=$(mktemp)
2222

2323
# --- Securely parse the unified configuration file ---
2424
if [ -f "$CONFIG_FILE" ]; then
25-
# Initialize an empty array for SSH options for robustness
26-
SSH_OPTS_ARR=()
2725
in_exclude_block=false
2826
while IFS= read -r line; do
2927
# Handle the rsync exclusion block
@@ -50,11 +48,6 @@ if [ -f "$CONFIG_FILE" ]; then
5048

5149
# CRITICAL: Assign value as a literal string to prevent code injection
5250
declare "$key"="$value"
53-
54-
# Robustly handle SSH options by converting the string to an array
55-
if [[ "$key" == "SSH_OPTS_STR" ]]; then
56-
read -r -a SSH_OPTS_ARR <<< "$value"
57-
fi
5851
fi
5952
done < "$CONFIG_FILE"
6053
else
@@ -71,7 +64,7 @@ MAX_LOG_SIZE=10485760 # 10 MB in bytes
7164
RSYNC_BASE_OPTS=(
7265
-a -z --delete --partial --timeout=60
7366
--exclude-from="$EXCLUDE_FILE_TMP"
74-
-e "ssh ${SSH_OPTS_ARR[@]}"
67+
-e "ssh ${SSH_OPTS_STR:-}"
7568
)
7669

7770
# =================================================================
@@ -114,7 +107,7 @@ send_notification() {
114107
}
115108

116109
run_integrity_check() {
117-
local rsync_check_opts=(-ainc -c --delete --exclude-from="$EXCLUDE_FILE_TMP" --out-format="%n" -e "ssh ${SSH_OPTS_ARR[@]}")
110+
local rsync_check_opts=(-ainc -c --delete --exclude-from="$EXCLUDE_FILE_TMP" --out-format="%n" -e "ssh ${SSH_OPTS_STR:-}")
118111
LC_ALL=C rsync "${rsync_check_opts[@]}" "$LOCAL_DIR" "$REMOTE_TARGET" 2>> "${LOG_FILE:-/dev/null}"
119112
}
120113

@@ -125,12 +118,10 @@ format_backup_stats() {
125118
local files_created=""
126119
local files_deleted=""
127120

128-
# First, try parsing the machine-readable format from --info=stats2
129121
bytes_transferred=$(echo "$rsync_output" | grep 'Total_transferred_size:' | awk '{print $2}')
130122
files_created=$(echo "$rsync_output" | grep 'Number_of_created_files:' | awk '{print $2}')
131123
files_deleted=$(echo "$rsync_output" | grep 'Number_of_deleted_files:' | awk '{print $2}')
132124

133-
# If parsing failed, fall back to the human-readable --stats format
134125
if [[ -z "$bytes_transferred" && -z "$files_created" && -z "$files_deleted" ]]; then
135126
bytes_transferred=$(echo "$rsync_output" | grep 'Total transferred file size:' | awk '{gsub(/,/, ""); print $5}')
136127
files_created=$(echo "$rsync_output" | grep 'Number of created files:' | awk '{print $5}')
@@ -165,7 +156,7 @@ for cmd in "${REQUIRED_CMDS[@]}"; do
165156
fi
166157
done
167158

168-
if ! ssh "${SSH_OPTS_ARR[@]}" -o BatchMode=yes -o ConnectTimeout=10 "$HETZNER_BOX" 'exit' 2>/dev/null; then
159+
if ! ssh ${SSH_OPTS_STR:-} -o BatchMode=yes -o ConnectTimeout=10 "$HETZNER_BOX" 'exit' 2>/dev/null; then
169160
send_notification "❌ SSH FAILED: ${HOSTNAME}" "x" "high" "failure" "Unable to SSH into $HETZNER_BOX. Check keys and connectivity."
170161
trap - ERR; exit 6
171162
fi

0 commit comments

Comments
 (0)