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

@@ -59,14 +59,12 @@ RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Configuration
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
PLEX_DB_DIR="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
MAIN_DB="com.plexapp.plugins.library.db"
BLOBS_DB="com.plexapp.plugins.library.blobs.db"
PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite"
BACKUP_SUFFIX="recovery-$(date +%Y%m%d_%H%M%S)"
RECOVERY_LOG="$SCRIPT_DIR/logs/database-recovery-$(date +%Y%m%d_%H%M%S).log"
@@ -80,7 +78,8 @@ mkdir -p "$SCRIPT_DIR/logs"
# Logging function
log_message() {
local message="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
local message
message="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
echo -e "$message"
echo "$message" >> "$RECOVERY_LOG"
}
@@ -216,7 +215,8 @@ start_plex_service() {
# Check database integrity
check_database_integrity() {
local db_file="$1"
local db_name=$(basename "$db_file")
local db_name
db_name=$(basename "$db_file")
log_info "Checking integrity of $db_name..."
@@ -249,7 +249,8 @@ check_database_integrity() {
# Recovery Method 1: SQLite .recover command
recovery_method_sqlite_recover() {
local db_file="$1"
local db_name=$(basename "$db_file")
local db_name
db_name=$(basename "$db_file")
local recovered_sql="${db_file}.recovered.sql"
local new_db="${db_file}.recovered"
@@ -315,7 +316,8 @@ recovery_method_sqlite_recover() {
# Recovery Method 2: Partial table extraction
recovery_method_partial_extraction() {
local db_file="$1"
local db_name=$(basename "$db_file")
local db_name
db_name=$(basename "$db_file")
local partial_sql="${db_file}.partial.sql"
local new_db="${db_file}.partial"
@@ -336,7 +338,7 @@ recovery_method_partial_extraction() {
} > "$partial_sql"
# Extract schema
if sudo "$PLEX_SQLITE" "$db_file" ".schema" >> "$partial_sql" 2>/dev/null; then
if sudo "$PLEX_SQLITE" "$db_file" ".schema" | sudo tee -a "$partial_sql" >/dev/null 2>&1; then
log_success "Schema extracted successfully"
else
log_warning "Schema extraction failed, trying alternative method"
@@ -372,7 +374,7 @@ recovery_method_partial_extraction() {
local extract_success=false
local limit=10000
while [ $limit -le 100000 ] && [ "$extract_success" = false ]; do
while [ "$limit" -le 100000 ] && [ "$extract_success" = false ]; do
if sudo "$PLEX_SQLITE" "$db_file" "SELECT COUNT(*) FROM $table;" >/dev/null 2>&1; then
# Table exists and is readable
{
@@ -382,8 +384,9 @@ recovery_method_partial_extraction() {
} >> "$partial_sql"
if sudo "$PLEX_SQLITE" "$db_file" ".mode insert $table" >>/dev/null 2>&1 && \
sudo "$PLEX_SQLITE" "$db_file" "SELECT * FROM $table LIMIT $limit;" >> "$partial_sql" 2>/dev/null; then
local row_count=$(tail -n +3 "$partial_sql" | grep "INSERT INTO $table" | wc -l)
sudo "$PLEX_SQLITE" "$db_file" "SELECT * FROM $table LIMIT $limit;" | sudo tee -a "$partial_sql" >/dev/null 2>&1; then
local row_count
row_count=$(tail -n +3 "$partial_sql" | grep -c "INSERT INTO $table")
log_success "Extracted $row_count rows from $table"
extract_success=true
else
@@ -444,7 +447,8 @@ recovery_method_partial_extraction() {
# Recovery Method 3: Emergency data extraction
recovery_method_emergency_extraction() {
local db_file="$1"
local db_name=$(basename "$db_file")
local db_name
db_name=$(basename "$db_file")
log_info "Recovery Method 3: Emergency data extraction for $db_name"
@@ -544,7 +548,8 @@ recovery_method_backup_restore() {
fi
# Find most recent backup
local latest_backup=$(find "$backup_dir" -maxdepth 1 -name "plex-backup-*.tar.gz" -type f 2>/dev/null | sort -r | head -1)
local latest_backup
latest_backup=$(find "$backup_dir" -maxdepth 1 -name "plex-backup-*.tar.gz" -type f 2>/dev/null | sort -r | head -1)
if [ -z "$latest_backup" ]; then
log_error "No backup files found in $backup_dir"