Skip to content

Commit 138a669

Browse files
authored
Merge pull request #14 from buildplan/improvements
Improvements
2 parents 19839f5 + 0286ad8 commit 138a669

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ END_EXCLUDES
222222

223223
# =================================================================
224224
# SCRIPT INITIALIZATION & SETUP
225-
# v0.11 - 2025.08.09
225+
# v0.12 - 2025.08.10
226226
# =================================================================
227227
set -Euo pipefail
228228
umask 077
@@ -332,9 +332,12 @@ run_integrity_check() {
332332
local rsync_check_opts=(-ainc -c --delete --exclude-from="$EXCLUDE_FILE_TMP" --out-format="%n" -e "ssh ${SSH_OPTS_STR:-}")
333333

334334
for dir in $BACKUP_DIRS; do
335-
local remote_subdir="${REMOTE_TARGET}/$(basename "$dir")/"
335+
local remote_path="${REMOTE_TARGET}${dir#/}"
336+
337+
echo "--- Integrity Check: $dir ---" >&2
338+
336339
# shellcheck disable=SC2086
337-
LC_ALL=C rsync "${rsync_check_opts[@]}" "$dir" "$remote_subdir" 2>> "${LOG_FILE:-/dev/null}"
340+
LC_ALL=C rsync "${rsync_check_opts[@]}" "$dir" "$remote_path" 2>> "${LOG_FILE:-/dev/null}"
338341
done
339342
}
340343

@@ -355,7 +358,6 @@ format_backup_stats() {
355358
local files_created=$(parse_stat "$rsync_output" 'Number_of_created_files:' '{s+=$2} END {print s}')
356359
local files_deleted=$(parse_stat "$rsync_output" 'Number_of_deleted_files:' '{s+=$2} END {print s}')
357360

358-
# Fallback for older rsync versions
359361
if [[ -z "$bytes_transferred" && -z "$files_created" && -z "$files_deleted" ]]; then
360362
bytes_transferred=$(parse_stat "$rsync_output" 'Total transferred file size:' '{gsub(/,/, ""); s+=$5} END {print s}')
361363
files_created=$(parse_stat "$rsync_output" 'Number of created files:' '{s+=$5} END {print s}')
@@ -422,10 +424,10 @@ if [[ "${1:-}" ]]; then
422424
echo "--- DRY RUN MODE ACTIVATED ---"
423425
DRY_RUN_FAILED=false
424426
for dir in $BACKUP_DIRS; do
425-
remote_subdir="${REMOTE_TARGET}/$(basename "$dir")/"
426-
echo "--- Checking dry run for: $dir -> $remote_subdir"
427+
remote_path="${REMOTE_TARGET}${dir#/}"
428+
echo "--- Checking dry run for: $dir -> $remote_path"
427429
# shellcheck disable=SC2086
428-
if ! rsync "${RSYNC_BASE_OPTS[@]}" --dry-run --info=progress2 "$dir" "$remote_subdir"; then
430+
if ! rsync "${RSYNC_BASE_OPTS[@]}" --dry-run --info=progress2 "$dir" "$remote_path"; then
429431
DRY_RUN_FAILED=true
430432
fi
431433
done
@@ -440,23 +442,28 @@ if [[ "${1:-}" ]]; then
440442
--checksum | --summary)
441443
echo "--- INTEGRITY CHECK MODE ACTIVATED ---"
442444
echo "Calculating differences..."
445+
START_TIME_INTEGRITY=$(date +%s)
443446
FILE_DISCREPANCIES=$(run_integrity_check)
447+
END_TIME_INTEGRITY=$(date +%s)
448+
DURATION_INTEGRITY=$((END_TIME_INTEGRITY - START_TIME_INTEGRITY))
449+
450+
CLEAN_DISCREPANCIES=$(echo "$FILE_DISCREPANCIES" | grep -v '^\*')
444451

445452
if [[ "$1" == "--summary" ]]; then
446-
MISMATCH_COUNT=$(echo "$FILE_DISCREPANCIES" | wc -l)
453+
MISMATCH_COUNT=$(echo "$CLEAN_DISCREPANCIES" | wc -l)
447454
printf "🚨 Total files with checksum mismatches: %d\n" "$MISMATCH_COUNT"
448455
log_message "Summary mode check found $MISMATCH_COUNT mismatched files."
449456
send_notification "📊 Backup Summary: ${HOSTNAME}" "bar_chart" "${NTFY_PRIORITY_SUCCESS}" "success" "Mismatched files found: $MISMATCH_COUNT"
450457
else # --checksum
451-
if [ -z "$FILE_DISCREPANCIES" ]; then
458+
if [ -z "$CLEAN_DISCREPANCIES" ]; then
452459
echo "✅ Checksum validation passed. No discrepancies found."
453460
log_message "Checksum validation passed. No discrepancies found."
454461
send_notification "✅ Backup Integrity OK: ${HOSTNAME}" "white_check_mark" "${NTFY_PRIORITY_SUCCESS}" "success" "Checksum validation passed. No discrepancies found."
455462
else
456463
log_message "Backup integrity check FAILED. Found discrepancies."
457-
ISSUE_LIST=$(echo "${FILE_DISCREPANCIES}" | head -n 10)
458-
printf "Backup integrity check FAILED. First 10 differing files:\n%s\n" "${ISSUE_LIST}"
459-
printf -v FAILURE_MSG "Backup integrity check FAILED.\n\nFirst 10 differing files:\n%s\n\nCheck log for full details." "${ISSUE_LIST}"
464+
ISSUE_LIST=$(echo "$CLEAN_DISCREPANCIES" | head -n 10)
465+
printf -v FAILURE_MSG "Backup integrity check FAILED.\n\nFirst 10 differing files:\n%s\n\nCheck duration: %dm %ds" "${ISSUE_LIST}" $((DURATION_INTEGRITY / 60)) $((DURATION_INTEGRITY % 60))
466+
printf "%s\n" "$FAILURE_MSG"
460467
send_notification "❌ Backup Integrity FAILED: ${HOSTNAME}" "x" "${NTFY_PRIORITY_FAILURE}" "failure" "${FAILURE_MSG}"
461468
fi
462469
fi
@@ -486,7 +493,7 @@ full_rsync_output=""
486493
for dir in $BACKUP_DIRS; do
487494
log_message "Backing up directory: $dir"
488495

489-
remote_subdir="${REMOTE_TARGET}/$(basename "$dir")/"
496+
remote_path="${REMOTE_TARGET}${dir#/}"
490497

491498
RSYNC_LOG_TMP=$(mktemp)
492499
RSYNC_EXIT_CODE=0
@@ -495,12 +502,12 @@ for dir in $BACKUP_DIRS; do
495502
if [[ "$VERBOSE_MODE" == "true" ]]; then
496503
RSYNC_OPTS+=(--info=stats2,progress2)
497504
# shellcheck disable=SC2086
498-
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_subdir" 2>&1 | tee "$RSYNC_LOG_TMP"
505+
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_path" 2>&1 | tee "$RSYNC_LOG_TMP"
499506
RSYNC_EXIT_CODE=${PIPESTATUS[0]}
500507
else
501508
RSYNC_OPTS+=(--info=stats2)
502509
# shellcheck disable=SC2086
503-
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_subdir" > "$RSYNC_LOG_TMP" 2>&1 || RSYNC_EXIT_CODE=$?
510+
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_path" > "$RSYNC_LOG_TMP" 2>&1 || RSYNC_EXIT_CODE=$?
504511
fi
505512

506513
cat "$RSYNC_LOG_TMP" >> "$LOG_FILE"

backup_script.sh

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# =================================================================
44
# SCRIPT INITIALIZATION & SETUP
5-
# v0.11 - 2025.08.09
5+
# v0.12 - 2025.08.10
66
# =================================================================
77
set -Euo pipefail
88
umask 077
@@ -112,9 +112,12 @@ run_integrity_check() {
112112
local rsync_check_opts=(-ainc -c --delete --exclude-from="$EXCLUDE_FILE_TMP" --out-format="%n" -e "ssh ${SSH_OPTS_STR:-}")
113113

114114
for dir in $BACKUP_DIRS; do
115-
local remote_subdir="${REMOTE_TARGET}/$(basename "$dir")/"
115+
local remote_path="${REMOTE_TARGET}${dir#/}"
116+
117+
echo "--- Integrity Check: $dir ---" >&2
118+
116119
# shellcheck disable=SC2086
117-
LC_ALL=C rsync "${rsync_check_opts[@]}" "$dir" "$remote_subdir" 2>> "${LOG_FILE:-/dev/null}"
120+
LC_ALL=C rsync "${rsync_check_opts[@]}" "$dir" "$remote_path" 2>> "${LOG_FILE:-/dev/null}"
118121
done
119122
}
120123

@@ -135,7 +138,6 @@ format_backup_stats() {
135138
local files_created=$(parse_stat "$rsync_output" 'Number_of_created_files:' '{s+=$2} END {print s}')
136139
local files_deleted=$(parse_stat "$rsync_output" 'Number_of_deleted_files:' '{s+=$2} END {print s}')
137140

138-
# Fallback for older rsync versions
139141
if [[ -z "$bytes_transferred" && -z "$files_created" && -z "$files_deleted" ]]; then
140142
bytes_transferred=$(parse_stat "$rsync_output" 'Total transferred file size:' '{gsub(/,/, ""); s+=$5} END {print s}')
141143
files_created=$(parse_stat "$rsync_output" 'Number of created files:' '{s+=$5} END {print s}')
@@ -202,10 +204,10 @@ if [[ "${1:-}" ]]; then
202204
echo "--- DRY RUN MODE ACTIVATED ---"
203205
DRY_RUN_FAILED=false
204206
for dir in $BACKUP_DIRS; do
205-
remote_subdir="${REMOTE_TARGET}/$(basename "$dir")/"
206-
echo "--- Checking dry run for: $dir -> $remote_subdir"
207+
remote_path="${REMOTE_TARGET}${dir#/}"
208+
echo "--- Checking dry run for: $dir -> $remote_path"
207209
# shellcheck disable=SC2086
208-
if ! rsync "${RSYNC_BASE_OPTS[@]}" --dry-run --info=progress2 "$dir" "$remote_subdir"; then
210+
if ! rsync "${RSYNC_BASE_OPTS[@]}" --dry-run --info=progress2 "$dir" "$remote_path"; then
209211
DRY_RUN_FAILED=true
210212
fi
211213
done
@@ -220,23 +222,28 @@ if [[ "${1:-}" ]]; then
220222
--checksum | --summary)
221223
echo "--- INTEGRITY CHECK MODE ACTIVATED ---"
222224
echo "Calculating differences..."
225+
START_TIME_INTEGRITY=$(date +%s)
223226
FILE_DISCREPANCIES=$(run_integrity_check)
227+
END_TIME_INTEGRITY=$(date +%s)
228+
DURATION_INTEGRITY=$((END_TIME_INTEGRITY - START_TIME_INTEGRITY))
229+
230+
CLEAN_DISCREPANCIES=$(echo "$FILE_DISCREPANCIES" | grep -v '^\*')
224231

225232
if [[ "$1" == "--summary" ]]; then
226-
MISMATCH_COUNT=$(echo "$FILE_DISCREPANCIES" | wc -l)
233+
MISMATCH_COUNT=$(echo "$CLEAN_DISCREPANCIES" | wc -l)
227234
printf "🚨 Total files with checksum mismatches: %d\n" "$MISMATCH_COUNT"
228235
log_message "Summary mode check found $MISMATCH_COUNT mismatched files."
229236
send_notification "📊 Backup Summary: ${HOSTNAME}" "bar_chart" "${NTFY_PRIORITY_SUCCESS}" "success" "Mismatched files found: $MISMATCH_COUNT"
230237
else # --checksum
231-
if [ -z "$FILE_DISCREPANCIES" ]; then
238+
if [ -z "$CLEAN_DISCREPANCIES" ]; then
232239
echo "✅ Checksum validation passed. No discrepancies found."
233240
log_message "Checksum validation passed. No discrepancies found."
234241
send_notification "✅ Backup Integrity OK: ${HOSTNAME}" "white_check_mark" "${NTFY_PRIORITY_SUCCESS}" "success" "Checksum validation passed. No discrepancies found."
235242
else
236243
log_message "Backup integrity check FAILED. Found discrepancies."
237-
ISSUE_LIST=$(echo "${FILE_DISCREPANCIES}" | head -n 10)
238-
printf "Backup integrity check FAILED. First 10 differing files:\n%s\n" "${ISSUE_LIST}"
239-
printf -v FAILURE_MSG "Backup integrity check FAILED.\n\nFirst 10 differing files:\n%s\n\nCheck log for full details." "${ISSUE_LIST}"
244+
ISSUE_LIST=$(echo "$CLEAN_DISCREPANCIES" | head -n 10)
245+
printf -v FAILURE_MSG "Backup integrity check FAILED.\n\nFirst 10 differing files:\n%s\n\nCheck duration: %dm %ds" "${ISSUE_LIST}" $((DURATION_INTEGRITY / 60)) $((DURATION_INTEGRITY % 60))
246+
printf "%s\n" "$FAILURE_MSG"
240247
send_notification "❌ Backup Integrity FAILED: ${HOSTNAME}" "x" "${NTFY_PRIORITY_FAILURE}" "failure" "${FAILURE_MSG}"
241248
fi
242249
fi
@@ -266,7 +273,7 @@ full_rsync_output=""
266273
for dir in $BACKUP_DIRS; do
267274
log_message "Backing up directory: $dir"
268275

269-
remote_subdir="${REMOTE_TARGET}/$(basename "$dir")/"
276+
remote_path="${REMOTE_TARGET}${dir#/}"
270277

271278
RSYNC_LOG_TMP=$(mktemp)
272279
RSYNC_EXIT_CODE=0
@@ -275,12 +282,12 @@ for dir in $BACKUP_DIRS; do
275282
if [[ "$VERBOSE_MODE" == "true" ]]; then
276283
RSYNC_OPTS+=(--info=stats2,progress2)
277284
# shellcheck disable=SC2086
278-
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_subdir" 2>&1 | tee "$RSYNC_LOG_TMP"
285+
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_path" 2>&1 | tee "$RSYNC_LOG_TMP"
279286
RSYNC_EXIT_CODE=${PIPESTATUS[0]}
280287
else
281288
RSYNC_OPTS+=(--info=stats2)
282289
# shellcheck disable=SC2086
283-
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_subdir" > "$RSYNC_LOG_TMP" 2>&1 || RSYNC_EXIT_CODE=$?
290+
nice -n 19 ionice -c 3 rsync "${RSYNC_OPTS[@]}" "$dir" "$remote_path" > "$RSYNC_LOG_TMP" 2>&1 || RSYNC_EXIT_CODE=$?
284291
fi
285292

286293
cat "$RSYNC_LOG_TMP" >> "$LOG_FILE"

0 commit comments

Comments
 (0)