feat: Refactor backup script to initialize performance log file and ensure full backups every time

This commit is contained in:
Peter Wood
2025-05-26 09:13:32 -04:00
parent 1149e9656c
commit 1d01ca8c61
2 changed files with 50 additions and 93 deletions

View File

@@ -23,9 +23,10 @@ MAX_BACKUPS_TO_KEEP=10
BACKUP_ROOT="/mnt/share/media/backups/plex"
LOG_ROOT="/mnt/share/media/backups/logs"
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
JSON_LOG_FILE="${SCRIPT_DIR}/logs/plex-backup.json"
PERFORMANCE_LOG_FILE="${SCRIPT_DIR}/logs/plex-backup-performance.json"
# Backup strategy configuration - Always perform full backups
# Plex SQLite path (custom Plex SQLite binary)
PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite"
@@ -81,6 +82,7 @@ while [[ $# -gt 0 ]]; do
echo " --webhook=URL Send notifications to webhook URL"
echo " --email=ADDRESS Send notifications to email address"
echo " -h, --help Show this help message"
echo ""
exit 0
;;
*)
@@ -170,48 +172,15 @@ track_performance() {
log_info "Performance: $operation completed in ${duration}s"
}
# Initialize JSON log file
initialize_json_log() {
if [ ! -f "${JSON_LOG_FILE}" ] || ! jq empty "${JSON_LOG_FILE}" 2>/dev/null; then
echo "{}" > "${JSON_LOG_FILE}"
log_message "Initialized JSON log file"
# Initialize log directory
initialize_logs() {
mkdir -p "$(dirname "$PERFORMANCE_LOG_FILE")"
if [ ! -f "$PERFORMANCE_LOG_FILE" ]; then
echo "[]" > "$PERFORMANCE_LOG_FILE"
log_message "Initialized performance log file"
fi
}
# Check if file needs backup based on modification time
needs_backup() {
local file="$1"
if [ ! -f "$file" ]; then
log_warning "File not found: $file"
return 1
fi
local current_mod_date=$(stat -c %Y "$file")
local last_backup_date=$(jq -r --arg file "$file" '.[$file] // 0' "${JSON_LOG_FILE}")
if [ "$last_backup_date" == "null" ] || [ "$last_backup_date" == "0" ]; then
log_message "File has never been backed up: $(basename "$file")"
return 0
fi
if [ "$current_mod_date" -gt "$last_backup_date" ]; then
log_message "File modified since last backup: $(basename "$file")"
return 0
fi
log_message "File unchanged since last backup: $(basename "$file")"
return 1
}
# Update backup time in JSON log
update_backup_time() {
local file="$1"
local timestamp="$2"
jq --arg file "$file" --arg timestamp "$timestamp" '.[$file] = ($timestamp | tonumber)' "${JSON_LOG_FILE}" > "${JSON_LOG_FILE}.tmp" && mv "${JSON_LOG_FILE}.tmp" "${JSON_LOG_FILE}"
}
# Enhanced notification system
send_notification() {
local title="$1"
@@ -754,7 +723,7 @@ estimate_backup_size() {
for nickname in "${!PLEX_FILES[@]}"; do
local file="${PLEX_FILES[$nickname]}"
if [ -f "$file" ] && needs_backup "$file" >/dev/null 2>&1; then
if [ -f "$file" ]; then
local size_kb=$(du -k "$file" 2>/dev/null | cut -f1)
total_size=$((total_size + size_kb))
fi
@@ -907,8 +876,8 @@ main() {
mkdir -p "${BACKUP_ROOT}"
mkdir -p "${LOG_ROOT}"
# Initialize JSON log
initialize_json_log
# Initialize logs
initialize_logs
# Check if only doing integrity check
if [ "$INTEGRITY_CHECK_ONLY" = true ]; then
@@ -983,43 +952,34 @@ main() {
# Handle WAL files backup
handle_wal_files "backup" "$BACKUP_PATH"
# Backup files
# Backup files - always perform full backup
local backup_start=$(date +%s)
for nickname in "${!PLEX_FILES[@]}"; do
local file="${PLEX_FILES[$nickname]}"
if [ -f "$file" ]; then
if needs_backup "$file" ]; then
log_message "Backing up: $(basename "$file")"
log_message "Backing up: $(basename "$file")"
# Create backup filename without timestamp (use original filename)
local backup_file="${BACKUP_PATH}/$(basename "$file")"
# Copy file
if sudo cp "$file" "$backup_file"; then
log_success "Copied: $(basename "$file")"
# Create backup filename without timestamp (use original filename)
local backup_file="${BACKUP_PATH}/$(basename "$file")"
# Copy file
if sudo cp "$file" "$backup_file"; then
log_success "Copied: $(basename "$file")"
# Verify backup
if verify_backup "$file" "$backup_file"; then
log_success "Verified: $(basename "$file")"
# Update backup time in JSON log
local current_timestamp=$(date +%s)
update_backup_time "$file" "$current_timestamp"
files_backed_up=$((files_backed_up + 1))
else
log_error "Verification failed: $(basename "$file")"
backup_errors=$((backup_errors + 1))
# Remove failed backup
rm -f "$backup_file"
fi
# Verify backup
if verify_backup "$file" "$backup_file"; then
log_success "Verified: $(basename "$file")"
files_backed_up=$((files_backed_up + 1))
else
log_error "Failed to copy: $(basename "$file")"
log_error "Verification failed: $(basename "$file")"
backup_errors=$((backup_errors + 1))
# Remove failed backup
rm -f "$backup_file"
fi
else
log_message "Skipping unchanged file: $(basename "$file")"
log_error "Failed to copy: $(basename "$file")"
backup_errors=$((backup_errors + 1))
fi
else
log_warning "File not found: $file"