Skip to content

Commit 6cbf040

Browse files
authored
Merge pull request #5 from buildplan/Improved_preflight_checks
Improved preflight checks
2 parents 1cb3cf7 + 229cd97 commit 6cbf040

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

restic-backup.sh

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

33
# =================================================================
4-
# Restic Backup Script v0.21 - 2025.09.09
4+
# Restic Backup Script v0.22 - 2025.09.09
55
# =================================================================
66

77
set -euo pipefail
88
umask 077
99

1010
# --- Script Constants ---
11-
SCRIPT_VERSION="0.21"
11+
SCRIPT_VERSION="0.22"
1212
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
1313
CONFIG_FILE="${SCRIPT_DIR}/restic-backup.conf"
1414
LOCK_FILE="/tmp/restic-backup.lock"
@@ -475,38 +475,85 @@ cleanup() {
475475

476476
run_preflight_checks() {
477477
local mode="${1:-backup}"
478+
echo -e "${C_BOLD}--- Running Pre-flight Checks ---${C_RESET}"
478479

479-
# Check required commands
480+
# System Dependencies
481+
echo -e "\n ${C_DIM}- Checking System Dependencies${C_RESET}"
482+
printf " %-65s" "Required commands (restic, curl, flock)..."
480483
local required_cmds=(restic curl flock)
481484
for cmd in "${required_cmds[@]}"; do
482485
if ! command -v "$cmd" &>/dev/null; then
486+
echo -e "[${C_RED} FAIL ${C_RESET}]"
483487
echo -e "${C_RED}ERROR: Required command '$cmd' not found${C_RESET}" >&2
484488
exit 10
485489
fi
486490
done
487-
488-
# Check password file
489-
if [ ! -f "$RESTIC_PASSWORD_FILE" ]; then
490-
echo -e "${C_RED}ERROR: Password file not found: $RESTIC_PASSWORD_FILE${C_RESET}" >&2
491+
echo -e "[${C_GREEN} OK ${C_RESET}]"
492+
493+
# Configuration Files
494+
echo -e "\n ${C_DIM}- Checking Configuration Files${C_RESET}"
495+
printf " %-65s" "Password file ('$RESTIC_PASSWORD_FILE')..."
496+
if [ ! -r "$RESTIC_PASSWORD_FILE" ]; then
497+
echo -e "[${C_RED} FAIL ${C_RESET}]"
498+
echo -e "${C_RED}ERROR: Password file not found or not readable: $RESTIC_PASSWORD_FILE${C_RESET}" >&2
491499
exit 11
492500
fi
501+
echo -e "[${C_GREEN} OK ${C_RESET}]"
502+
503+
if [ -n "${EXCLUDE_FILE:-}" ]; then
504+
printf " %-65s" "Exclude file ('$EXCLUDE_FILE')..."
505+
if [ ! -r "$EXCLUDE_FILE" ]; then
506+
echo -e "[${C_RED} FAIL ${C_RESET}]"
507+
echo -e "${C_RED}ERROR: The specified EXCLUDE_FILE is not readable: ${EXCLUDE_FILE}${C_RESET}" >&2
508+
exit 14
509+
fi
510+
echo -e "[${C_GREEN} OK ${C_RESET}]"
511+
fi
512+
513+
printf " %-65s" "Log file writability ('$LOG_FILE')..."
514+
if ! touch "$LOG_FILE" >/dev/null 2>&1; then
515+
echo -e "[${C_RED} FAIL ${C_RESET}]"
516+
echo -e "${C_RED}ERROR: The log file or its directory is not writable: ${LOG_FILE}${C_RESET}" >&2
517+
exit 15
518+
fi
519+
echo -e "[${C_GREEN} OK ${C_RESET}]"
493520

494-
# Check repository connectivity
521+
# Repository State
522+
echo -e "\n ${C_DIM}- Checking Repository State${C_RESET}"
523+
printf " %-65s" "Repository connectivity and credentials..."
495524
if ! restic cat config >/dev/null 2>&1; then
496525
if [[ "$mode" == "init" ]]; then
497-
return 0 # OK for init mode
526+
echo -e "[${C_YELLOW} SKIP ${C_RESET}] (OK for --init mode)"
527+
return 0
498528
fi
499-
echo -e "${C_RED}ERROR: Cannot access repository. Run with --init first${C_RESET}" >&2
529+
echo -e "[${C_RED} FAIL ${C_RESET}]"
530+
echo -e "${C_RED}ERROR: Cannot access repository. Check credentials or run --init first.${C_RESET}" >&2
500531
exit 12
501532
fi
533+
echo -e "[${C_GREEN} OK ${C_RESET}]"
534+
535+
printf " %-65s" "Stale repository locks..."
536+
local lock_info
537+
lock_info=$(restic list locks 2>/dev/null || true)
538+
if [ -n "$lock_info" ]; then
539+
echo -e "[${C_YELLOW} WARN ${C_RESET}]"
540+
echo -e "${C_YELLOW} ⚠️ Stale locks found! This may prevent backups from running.${C_RESET}"
541+
echo -e "${C_DIM} Run the --unlock command to remove them.${C_RESET}"
542+
else
543+
echo -e "[${C_GREEN} OK ${C_RESET}]"
544+
fi
502545

503-
# Check source directories (for backup mode)
504-
if [[ "$mode" == "backup" ]]; then
546+
# Backup Sources
547+
if [[ "$mode" == "backup" || "$mode" == "diff" ]]; then
548+
echo -e "\n ${C_DIM}- Checking Backup Sources${C_RESET}"
505549
for source in $BACKUP_SOURCES; do
550+
printf " %-65s" "Source directory ('$source')..."
506551
if [ ! -d "$source" ] || [ ! -r "$source" ]; then
507-
echo -e "${C_RED}ERROR: Source directory not accessible: $source${C_RESET}" >&2
552+
echo -e "[${C_RED} FAIL ${C_RESET}]"
553+
echo -e "${C_RED}ERROR: Source directory not found or not readable: $source${C_RESET}" >&2
508554
exit 13
509555
fi
556+
echo -e "[${C_GREEN} OK ${C_RESET}]"
510557
done
511558
fi
512559
}

restic-backup.sh.sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
63a9d3319cd1b3ade34bfc62c8ea832baae438a4ca512f6993f9d1ed2fce2f49 restic-backup.sh
1+
2ec7e966f42762fd284094c6208c338e7a87dfd513bcf3a6812a798b646eabae restic-backup.sh

0 commit comments

Comments
 (0)