Refactor variable assignments and improve script readability in validate-plex-backups.sh and validate-plex-recovery.sh

- Changed inline variable assignments to separate declaration and assignment for clarity.
- Updated condition checks and log messages for better readability and consistency.
- Added a backup of validate-plex-recovery.sh for safety.
- Introduced a new script run-docker-tests.sh for testing setup in Docker containers.
- Enhanced ssh-login.sh to improve condition checks and logging functionality.
This commit is contained in:
Peter Wood
2025-06-05 17:14:02 -04:00
parent c3f237a321
commit 58b5dea8b4
31 changed files with 5024 additions and 539 deletions

View File

@@ -35,6 +35,7 @@ trap cleanup EXIT SIGINT SIGTERM
ENV_FILE="$(dirname "$0")/../.env"
if [ -f "$ENV_FILE" ]; then
echo "Loading environment variables from $ENV_FILE"
# shellcheck source=/dev/null
source "$ENV_FILE"
else
echo "Error: .env file not found in $(dirname "$0")/.."
@@ -133,7 +134,8 @@ send_notification() {
local title="$1"
local message="$2"
local status="${3:-info}" # success, error, warning, info
local hostname=$(hostname)
local hostname
hostname=$(hostname)
# Console notification
log_message "$title: $message"
@@ -157,7 +159,8 @@ send_notification() {
# Function to upload to Backblaze B2
upload_to_b2() {
local file_path="$1"
local filename=$(basename "$file_path")
local filename
filename=$(basename "$file_path")
# Check if B2 is configured
if [ -z "$B2_APPLICATION_KEY_ID" ] || [ -z "$B2_APPLICATION_KEY" ] || [ -z "$B2_BUCKET_NAME" ]; then
@@ -344,14 +347,11 @@ echo ""
echo "=== PHASE 1: DATABASE BACKUP ==="
log_message "Taking database backup using pg_dumpall as recommended by Immich documentation..."
# Use pg_dumpall with recommended flags: --clean and --if-exists
docker exec -t immich_postgres pg_dumpall \
if ! docker exec -t immich_postgres pg_dumpall \
--clean \
--if-exists \
--username="${DB_USERNAME}" \
> "${DB_BACKUP_PATH}"
# Check if the dump was successful
if [ $? -ne 0 ] || [ ! -s "${DB_BACKUP_PATH}" ]; then
> "${DB_BACKUP_PATH}" || [ ! -s "${DB_BACKUP_PATH}" ]; then
log_message "Error: Database backup failed or created an empty file."
exit 1
fi

View File

@@ -8,7 +8,6 @@ set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
@@ -215,7 +214,8 @@ test_env_validation() {
log_test "Environment Variable Validation"
# Temporarily move .env file to test missing env
local env_file="$(dirname "$RESTORE_SCRIPT")/../.env"
local env_file
env_file="$(dirname "$RESTORE_SCRIPT")/../.env"
if [ -f "$env_file" ]; then
mv "$env_file" "${env_file}.backup"
@@ -285,11 +285,12 @@ test_logging() {
TESTS_RUN=$((TESTS_RUN + 1))
log_test "Logging Functionality"
local log_dir="$(dirname "$RESTORE_SCRIPT")/../logs"
local log_dir
log_dir="$(dirname "$RESTORE_SCRIPT")/../logs"
local restore_log="${log_dir}/immich-restore.log"
# Clear previous log entries
[ -f "$restore_log" ] && > "$restore_log"
[ -f "$restore_log" ] && true > "$restore_log"
local mock_db_backup="${TEST_DIR}/mock_immich_db_backup_20250603_120000.sql.gz"
local mock_uploads_backup="${TEST_DIR}/mock_immich_uploads_20250603_120000.tar.gz"
@@ -330,10 +331,12 @@ test_performance() {
done
tar -czf "$large_uploads_backup" -C "${TEST_DIR}" large_mock_uploads
local start_time=$(date +%s)
local start_time
start_time=$(date +%s)
local output
if output=$("$RESTORE_SCRIPT" --db-backup "$large_db_backup" --uploads-backup "$large_uploads_backup" --dry-run 2>&1); then
local end_time=$(date +%s)
local end_time
end_time=$(date +%s)
local duration=$((end_time - start_time))
if [ $duration -lt 30 ]; then # Should complete dry run in under 30 seconds