mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 00:00:13 -08:00
feat: Refactor logging system to use local log directory and add synchronization and cleanup functions
This commit is contained in:
@@ -14,8 +14,9 @@ NC='\033[0m'
|
||||
# Configuration
|
||||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||||
BACKUP_ROOT="/mnt/share/media/backups/plex"
|
||||
JSON_LOG_FILE="$SCRIPT_DIR/logs/plex-backup.json"
|
||||
REPORT_FILE="$SCRIPT_DIR/logs/backup-validation-$(date +%Y%m%d_%H%M%S).log"
|
||||
SHARED_LOG_ROOT="/mnt/share/media/backups/logs"
|
||||
LOCAL_LOG_ROOT="$SCRIPT_DIR/logs"
|
||||
REPORT_FILE="$LOCAL_LOG_ROOT/backup-validation-$(date +%Y%m%d_%H%M%S).log"
|
||||
|
||||
# Expected files in backup
|
||||
EXPECTED_FILES=(
|
||||
@@ -62,6 +63,94 @@ log_info() {
|
||||
log_message "${BLUE}INFO: $1${NC}" "INFO: $1"
|
||||
}
|
||||
|
||||
# Log synchronization functions
|
||||
sync_logs_to_shared() {
|
||||
local sync_start_time=$(date +%s)
|
||||
log_info "Starting log synchronization to shared location"
|
||||
|
||||
# Ensure shared log directory exists
|
||||
if ! mkdir -p "$SHARED_LOG_ROOT" 2>/dev/null; then
|
||||
log_warning "Could not create shared log directory: $SHARED_LOG_ROOT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if shared location is accessible
|
||||
if [ ! -w "$SHARED_LOG_ROOT" ]; then
|
||||
log_warning "Shared log directory is not writable: $SHARED_LOG_ROOT"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Sync log files (one-way: local -> shared)
|
||||
local sync_count=0
|
||||
local error_count=0
|
||||
|
||||
for log_file in "$LOCAL_LOG_ROOT"/*.log; do
|
||||
if [ -f "$log_file" ]; then
|
||||
local filename=$(basename "$log_file")
|
||||
local shared_file="$SHARED_LOG_ROOT/$filename"
|
||||
|
||||
# Only copy if file doesn't exist in shared location or local is newer
|
||||
if [ ! -f "$shared_file" ] || [ "$log_file" -nt "$shared_file" ]; then
|
||||
if cp "$log_file" "$shared_file" 2>/dev/null; then
|
||||
((sync_count++))
|
||||
log_info "Synced: $filename"
|
||||
else
|
||||
((error_count++))
|
||||
log_warning "Failed to sync: $filename"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
local sync_end_time=$(date +%s)
|
||||
local sync_duration=$((sync_end_time - sync_start_time))
|
||||
|
||||
if [ $error_count -eq 0 ]; then
|
||||
log_success "Log sync completed: $sync_count files synced in ${sync_duration}s"
|
||||
else
|
||||
log_warning "Log sync completed with errors: $sync_count synced, $error_count failed in ${sync_duration}s"
|
||||
fi
|
||||
|
||||
return $error_count
|
||||
}
|
||||
|
||||
# Cleanup old local logs (30 day retention)
|
||||
cleanup_old_local_logs() {
|
||||
local cleanup_start_time=$(date +%s)
|
||||
log_info "Starting cleanup of old local logs (30+ days)"
|
||||
|
||||
if [ ! -d "$LOCAL_LOG_ROOT" ]; then
|
||||
log_info "Local log directory does not exist, nothing to clean up"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local cleanup_count=0
|
||||
local error_count=0
|
||||
|
||||
# Find and remove log files older than 30 days
|
||||
while IFS= read -r -d '' old_file; do
|
||||
local filename=$(basename "$old_file")
|
||||
if rm "$old_file" 2>/dev/null; then
|
||||
((cleanup_count++))
|
||||
log_info "Removed old log: $filename"
|
||||
else
|
||||
((error_count++))
|
||||
log_warning "Failed to remove old log: $filename"
|
||||
fi
|
||||
done < <(find "$LOCAL_LOG_ROOT" -name "*.log" -mtime +30 -print0 2>/dev/null)
|
||||
|
||||
local cleanup_end_time=$(date +%s)
|
||||
local cleanup_duration=$((cleanup_end_time - cleanup_start_time))
|
||||
|
||||
if [ $cleanup_count -gt 0 ]; then
|
||||
log_success "Cleanup completed: $cleanup_count items removed in ${cleanup_duration}s"
|
||||
else
|
||||
log_info "Cleanup completed: no old items found to remove in ${cleanup_duration}s"
|
||||
fi
|
||||
|
||||
return $error_count
|
||||
}
|
||||
|
||||
# Check dependencies
|
||||
check_dependencies() {
|
||||
local missing_deps=()
|
||||
@@ -71,10 +160,6 @@ check_dependencies() {
|
||||
missing_deps+=("tar")
|
||||
fi
|
||||
|
||||
if ! command -v jq >/dev/null 2>&1; then
|
||||
missing_deps+=("jq")
|
||||
fi
|
||||
|
||||
if ! command -v find >/dev/null 2>&1; then
|
||||
missing_deps+=("find")
|
||||
fi
|
||||
@@ -279,34 +364,9 @@ check_backup_freshness() {
|
||||
|
||||
# Validate JSON log file
|
||||
validate_json_log() {
|
||||
log_info "Validating JSON log file..."
|
||||
|
||||
if [ ! -f "$JSON_LOG_FILE" ]; then
|
||||
log_error "JSON log file not found: $JSON_LOG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if jq is available (should be caught by dependency check)
|
||||
if ! command -v jq >/dev/null 2>&1; then
|
||||
log_error "jq command not found - cannot validate JSON"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Validate JSON syntax
|
||||
local jq_output
|
||||
if ! jq_output=$(jq empty "$JSON_LOG_FILE" 2>&1); then
|
||||
log_error "JSON log file is invalid: $jq_output"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Get entry count safely
|
||||
local entry_count
|
||||
if ! entry_count=$(jq 'length' "$JSON_LOG_FILE" 2>/dev/null); then
|
||||
log_error "Could not count entries in JSON log file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_success "JSON log file is valid ($entry_count entries)"
|
||||
# This function has been removed as the JSON log file is no longer used
|
||||
# The plex backup system only uses the performance log file
|
||||
log_info "JSON log validation skipped (feature removed)"
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -440,14 +500,6 @@ generate_report() {
|
||||
fix_issues() {
|
||||
log_info "Attempting to fix common issues..."
|
||||
|
||||
# Fix JSON log file
|
||||
if [ ! -f "$JSON_LOG_FILE" ] || ! jq empty "$JSON_LOG_FILE" 2>/dev/null; then
|
||||
log_info "Fixing JSON log file..."
|
||||
mkdir -p "$(dirname "$JSON_LOG_FILE")"
|
||||
echo "{}" > "$JSON_LOG_FILE"
|
||||
log_success "JSON log file created/fixed"
|
||||
fi
|
||||
|
||||
# Create corrupted backups directory
|
||||
local corrupted_dir="$(dirname "$REPORT_FILE")/corrupted-backups"
|
||||
mkdir -p "$corrupted_dir"
|
||||
@@ -604,6 +656,11 @@ main() {
|
||||
echo "Use --report for a detailed backup analysis"
|
||||
fi
|
||||
|
||||
# Sync logs to shared location and cleanup old local logs
|
||||
log_info "Post-validation: synchronizing logs and cleaning up old files"
|
||||
sync_logs_to_shared
|
||||
cleanup_old_local_logs
|
||||
|
||||
exit $overall_status
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user