feat: Add comprehensive Plex recovery validation script

- Introduced `validate-plex-recovery.sh` for validating Plex database recovery.
- Implemented checks for service status, database integrity, web interface accessibility, API functionality, and recent logs.
- Added detailed recovery summary and next steps for users.

fix: Improve Debian patching script for compatibility

- Enhanced `debian-patches.sh` to securely download and execute bootstrap scripts.
- Updated package mapping logic and ensured proper permissions for patched files.

fix: Update Docker test scripts for better permission handling

- Modified `run-docker-tests.sh` to set appropriate permissions on logs directory.
- Ensured log files have correct permissions after test runs.

fix: Enhance setup scripts for secure installations

- Updated `setup.sh` to securely download and execute installation scripts for zoxide and nvm.
- Improved error handling for failed downloads.

fix: Refine startup script for log directory permissions

- Adjusted `startup.sh` to set proper permissions for log directories and files.

chore: Revamp update-containers.sh for better error handling and logging

- Rewrote `update-containers.sh` to include detailed logging and error handling.
- Added validation for Docker image names and improved overall script robustness.
This commit is contained in:
Peter Wood
2025-06-05 07:22:28 -04:00
parent 8b514ac0b2
commit 0123fc6007
25 changed files with 4407 additions and 608 deletions

View File

@@ -1,5 +1,51 @@
#!/bin/bash
################################################################################
# Plex Media Server Backup Restoration Script
################################################################################
#
# Author: Peter Wood <peter@peterwood.dev>
# Description: Safe and reliable restoration script for Plex Media Server
# backups with validation, dry-run capability, and automatic
# backup of current data before restoration.
#
# Features:
# - Interactive backup selection from available archives
# - Backup validation before restoration
# - Dry-run mode for testing restoration process
# - Automatic backup of current data before restoration
# - Service management (stop/start Plex during restoration)
# - Comprehensive logging and error handling
# - File ownership and permission restoration
#
# Related Scripts:
# - backup-plex.sh: Creates backups that this script restores
# - validate-plex-backups.sh: Validates backup integrity
# - monitor-plex-backup.sh: Monitors backup system health
# - test-plex-backup.sh: Tests backup/restore operations
# - plex.sh: General Plex service management
#
# Usage:
# ./restore-plex.sh # List available backups
# ./restore-plex.sh plex-backup-20250125_143022.tar.gz # Restore specific backup
# ./restore-plex.sh --dry-run backup-file.tar.gz # Test restoration process
# ./restore-plex.sh --list # List all available backups
#
# Dependencies:
# - tar (for archive extraction)
# - Plex Media Server
# - systemctl (for service management)
# - Access to backup directory
#
# Exit Codes:
# 0 - Success
# 1 - General error
# 2 - Backup file not found or invalid
# 3 - Service management failure
# 4 - Restoration failure
#
################################################################################
# Plex Backup Restoration Script
# Usage: ./restore-plex.sh [backup_date] [--dry-run]
@@ -57,18 +103,18 @@ list_backups() {
# Validate backup integrity
validate_backup() {
local backup_file="$1"
if [ ! -f "$backup_file" ]; then
log_error "Backup file not found: $backup_file"
return 1
fi
log_message "Validating backup integrity for $(basename "$backup_file")..."
# Test archive integrity
if tar -tzf "$backup_file" >/dev/null 2>&1; then
log_success "Archive integrity check passed"
# List contents to verify expected files are present
log_message "Archive contents:"
tar -tzf "$backup_file" | while read file; do
@@ -85,10 +131,10 @@ validate_backup() {
backup_current_data() {
local backup_suffix=$(date '+%Y%m%d_%H%M%S')
local current_backup_dir="$SCRIPT_DIR/plex_current_backup_$backup_suffix"
log_message "Creating backup of current Plex data..."
mkdir -p "$current_backup_dir"
for file in "${!RESTORE_LOCATIONS[@]}"; do
local src="${RESTORE_LOCATIONS[$file]}$file"
if [ -f "$src" ]; then
@@ -100,7 +146,7 @@ backup_current_data() {
fi
fi
done
log_success "Current data backed up to: $current_backup_dir"
echo "$current_backup_dir"
}
@@ -109,31 +155,31 @@ backup_current_data() {
restore_files() {
local backup_file="$1"
local dry_run="$2"
if [ ! -f "$backup_file" ]; then
log_error "Backup file not found: $backup_file"
return 1
fi
# Create temporary extraction directory
local temp_dir="/tmp/plex-restore-$(date +%Y%m%d_%H%M%S)"
mkdir -p "$temp_dir"
log_message "Extracting backup archive..."
if ! tar -xzf "$backup_file" -C "$temp_dir"; then
log_error "Failed to extract backup archive"
rm -rf "$temp_dir"
return 1
fi
log_message "Restoring files..."
local restore_errors=0
for file in "${!RESTORE_LOCATIONS[@]}"; do
local src_file="$temp_dir/$file"
local dest_path="${RESTORE_LOCATIONS[$file]}"
local dest_file="$dest_path$file"
if [ -f "$src_file" ]; then
if [ "$dry_run" == "true" ]; then
log_message "Would restore: $file to $dest_file"
@@ -152,10 +198,10 @@ restore_files() {
restore_errors=$((restore_errors + 1))
fi
done
# Clean up temporary directory
rm -rf "$temp_dir"
return $restore_errors
}
@@ -163,7 +209,7 @@ restore_files() {
manage_plex_service() {
local action="$1"
log_message "$action Plex Media Server..."
case "$action" in
"stop")
sudo systemctl stop plexmediaserver.service
@@ -182,12 +228,12 @@ manage_plex_service() {
main() {
local backup_file="$1"
local dry_run=false
# Check for dry-run flag
if [ "$2" = "--dry-run" ] || [ "$1" = "--dry-run" ]; then
dry_run=true
fi
# If no backup file provided, list available backups
if [ -z "$backup_file" ] || [ "$backup_file" = "--dry-run" ]; then
list_backups
@@ -197,39 +243,39 @@ main() {
echo " $0 /mnt/share/media/backups/plex/plex-backup-20250125_143022.tar.gz"
exit 0
fi
# If relative path, prepend BACKUP_ROOT
if [[ "$backup_file" != /* ]]; then
backup_file="$BACKUP_ROOT/$backup_file"
fi
# Validate backup exists and is complete
if ! validate_backup "$backup_file"; then
log_error "Backup validation failed"
exit 1
fi
if [ "$dry_run" = "true" ]; then
restore_files "$backup_file" true
log_message "Dry run completed. No changes were made."
exit 0
fi
# Confirm restoration
echo
log_warning "This will restore Plex data from backup $(basename "$backup_file")"
log_warning "Current Plex data will be backed up before restoration"
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_message "Restoration cancelled"
exit 0
fi
# Stop Plex service
manage_plex_service stop
# Backup current data
local current_backup=$(backup_current_data)
if [ $? -ne 0 ]; then
@@ -237,7 +283,7 @@ main() {
manage_plex_service start
exit 1
fi
# Restore files
if restore_files "$backup_file" false; then
log_success "Restoration completed successfully"
@@ -247,10 +293,10 @@ main() {
manage_plex_service start
exit 1
fi
# Start Plex service
manage_plex_service start
log_success "Plex restoration completed. Please verify your server is working correctly."
}