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

@@ -50,12 +50,12 @@ monitor_realtime() {
for tag in "${BACKUP_TAGS[@]}"; do
tags_filter="$tags_filter -t $tag"
done
log_info "Starting real-time monitoring of backup logs"
log_info "Press Ctrl+C to stop monitoring"
echo
sudo journalctl -f $tags_filter --no-hostname --output=short-iso | while read -r line; do
sudo journalctl -f "$tags_filter" --no-hostname --output=short-iso | while read -r line; do
# Color code different log levels and services
if [[ "$line" =~ ERROR ]]; then
echo -e "${RED}$line${NC}"
@@ -78,9 +78,9 @@ monitor_realtime() {
show_recent_logs() {
local hours="${1:-24}"
local service="${2:-all}"
log_info "Showing logs from the last $hours hours"
local tags_filter=""
if [ "$service" = "all" ]; then
for tag in "${BACKUP_TAGS[@]}"; do
@@ -89,9 +89,9 @@ show_recent_logs() {
else
tags_filter="-t $service"
fi
echo
sudo journalctl --since "${hours} hours ago" $tags_filter --no-hostname --output=short-iso | \
sudo journalctl --since "${hours} hours ago" "$tags_filter" --no-hostname --output=short-iso | \
while read -r line; do
# Color code the output
if [[ "$line" =~ ERROR ]]; then
@@ -108,14 +108,13 @@ show_recent_logs() {
show_error_summary() {
local days="${1:-7}"
log_info "Error summary for the last $days days"
echo
local error_file="/tmp/backup_errors_$$.tmp"
for tag in "${BACKUP_TAGS[@]}"; do
local error_count=$(sudo journalctl --since "${days} days ago" -t "$tag" --grep="ERROR" --output=cat | wc -l)
local error_count
error_count=$(sudo journalctl --since "${days} days ago" -t "$tag" --grep="ERROR" --output=cat | wc -l)
if [ "$error_count" -gt 0 ]; then
echo -e "${RED}$tag: $error_count errors${NC}"
sudo journalctl --since "${days} days ago" -t "$tag" --grep="ERROR" --output=short-iso | head -5
@@ -128,36 +127,40 @@ show_error_summary() {
generate_backup_report() {
local days="${1:-7}"
local report_file="$REPORT_DIR/backup-report-$(date +%Y%m%d_%H%M%S).txt"
local report_file
report_file="$REPORT_DIR/backup-report-$(date +%Y%m%d_%H%M%S).txt"
log_info "Generating comprehensive backup report for the last $days days"
log_info "Report will be saved to: $report_file"
{
echo "=== BACKUP SYSTEM REPORT ==="
echo "Generated: $(date)"
echo "Period: Last $days days"
echo "System: $(uname -n)"
echo
for tag in "${BACKUP_TAGS[@]}"; do
echo "=== $tag ==="
echo "=== $tag ==="
# Count entries
local total_entries=$(sudo journalctl --since "${days} days ago" -t "$tag" --output=cat | wc -l)
local error_count=$(sudo journalctl --since "${days} days ago" -t "$tag" --grep="ERROR" --output=cat | wc -l)
local success_count=$(sudo journalctl --since "${days} days ago" -t "$tag" --grep="SUCCESS" --output=cat | wc -l)
local total_entries
local error_count
local success_count
total_entries=$(sudo journalctl --since "${days} days ago" -t "$tag" --output=cat | wc -l)
error_count=$(sudo journalctl --since "${days} days ago" -t "$tag" --grep="ERROR" --output=cat | wc -l)
success_count=$(sudo journalctl --since "${days} days ago" -t "$tag" --grep="SUCCESS" --output=cat | wc -l)
echo "Total log entries: $total_entries"
echo "Errors: $error_count"
echo "Successes: $success_count"
if [ "$error_count" -gt 0 ]; then
echo
echo "Recent errors:"
sudo journalctl --since "${days} days ago" -t "$tag" --grep="ERROR" --output=short-iso | head -10
fi
echo
echo "Recent activity:"
sudo journalctl --since "${days} days ago" -t "$tag" --output=short-iso | tail -5
@@ -165,7 +168,7 @@ generate_backup_report() {
echo "----------------------------------------"
echo
done
# System resource usage during backups
echo "=== SYSTEM ANALYSIS ==="
echo "Disk usage in backup directories:"
@@ -173,16 +176,16 @@ generate_backup_report() {
du -sh /mnt/share/media/backups/* 2>/dev/null || echo "No backup directories found"
fi
echo
# Cron job status
echo "Active cron jobs related to backups:"
sudo crontab -l 2>/dev/null | grep -E "(backup|plex|immich)" || echo "No backup-related cron jobs found"
echo
} > "$report_file"
log_success "Report generated: $report_file"
# Show summary
echo
log_info "Report Summary:"
@@ -198,10 +201,10 @@ generate_backup_report() {
check_backup_health() {
log_info "Checking backup system health for $HOSTNAME"
echo
local health_score=100
local issues=()
# Check if backup scripts exist
local backup_scripts=(
"/home/acedanger/shell/backup-plex.sh"
@@ -209,7 +212,7 @@ check_backup_health() {
"/home/acedanger/shell/validate-plex-backups.sh"
"/home/acedanger/shell/crontab/crontab-backup-system.sh"
)
for script in "${backup_scripts[@]}"; do
if [ ! -f "$script" ]; then
issues+=("Missing script: $script")
@@ -219,28 +222,28 @@ check_backup_health() {
((health_score -= 10))
fi
done
# Check if backup directories exist
local backup_dirs=(
"/mnt/share/media/backups/plex"
"/mnt/share/media/backups/docker-data"
"/mnt/share/media/backups/immich"
)
for dir in "${backup_dirs[@]}"; do
if [ ! -d "$dir" ]; then
issues+=("Missing backup directory: $dir")
((health_score -= 15))
fi
done
# Check crontab backup system structure
local crontab_backup_dir="$SCRIPT_DIR/crontab-backups/$HOSTNAME"
if [ ! -d "$crontab_backup_dir" ]; then
issues+=("Missing crontab backup directory for $HOSTNAME: $crontab_backup_dir")
((health_score -= 10))
fi
# Check recent backup activity
local recent_activity=false
for tag in "${BACKUP_TAGS[@]}"; do
@@ -249,29 +252,30 @@ check_backup_health() {
break
fi
done
if [ "$recent_activity" = false ]; then
issues+=("No backup activity in the last 24 hours")
((health_score -= 25))
fi
# Check for recent errors
local recent_errors=0
for tag in "${BACKUP_TAGS[@]}"; do
local error_count=$(sudo journalctl --since "24 hours ago" -t "$tag" --grep="ERROR" --output=cat | wc -l)
local error_count
error_count=$(sudo journalctl --since "24 hours ago" -t "$tag" --grep="ERROR" --output=cat | wc -l)
((recent_errors += error_count))
done
if [ "$recent_errors" -gt 0 ]; then
issues+=("$recent_errors errors in the last 24 hours")
((health_score -= $((recent_errors * 5))))
fi
# Ensure health score doesn't go below 0
if [ "$health_score" -lt 0 ]; then
health_score=0
fi
# Display results
if [ "$health_score" -ge 90 ]; then
echo -e "${GREEN}Backup System Health ($HOSTNAME): ${health_score}% - EXCELLENT${NC}"
@@ -282,14 +286,14 @@ check_backup_health() {
else
echo -e "${RED}Backup System Health ($HOSTNAME): ${health_score}% - POOR${NC}"
fi
if [ ${#issues[@]} -gt 0 ]; then
echo
log_warning "Issues found:"
for issue in "${issues[@]}"; do
echo -e " ${RED}$issue${NC}"
done
echo
log_info "Recommended actions:"
echo " • Run: ./manage-enhanced-crontab.sh verify"
@@ -302,22 +306,27 @@ check_backup_health() {
show_service_status() {
log_info "Backup Service Status Overview"
echo
printf "%-20s %-15s %-20s %-30s\n" "Service" "Status" "Last Activity" "Last Message"
printf "%-20s %-15s %-20s %-30s\n" "-------" "------" "-------------" "------------"
for tag in "${BACKUP_TAGS[@]}"; do
local last_entry=$(sudo journalctl -t "$tag" --output=short-iso -n 1 2>/dev/null | tail -1)
local last_entry
last_entry=$(sudo journalctl -t "$tag" --output=short-iso -n 1 2>/dev/null | tail -1)
if [ -n "$last_entry" ]; then
local timestamp=$(echo "$last_entry" | cut -d' ' -f1-2)
local message=$(echo "$last_entry" | cut -d' ' -f4- | cut -c1-30)
local timestamp
local message
local entry_time
local current_time
timestamp=$(echo "$last_entry" | cut -d' ' -f1-2)
message=$(echo "$last_entry" | cut -d' ' -f4- | cut -c1-30)
# Check if it's recent (within 48 hours)
local entry_time=$(date -d "$timestamp" +%s 2>/dev/null || echo "0")
local current_time=$(date +%s)
entry_time=$(date -d "$timestamp" +%s 2>/dev/null || echo "0")
current_time=$(date +%s)
local hours_diff=$(( (current_time - entry_time) / 3600 ))
local status
if [ "$hours_diff" -le 24 ]; then
status="${GREEN}Active${NC}"
@@ -326,7 +335,7 @@ show_service_status() {
else
status="${RED}Stale${NC}"
fi
printf "%-20s %-25s %-20s %-30s\n" "$tag" "$status" "$timestamp" "$message"
else
printf "%-20s %-25s %-20s %-30s\n" "$tag" "${RED}No logs${NC}" "Never" "No activity found"