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:
@@ -21,9 +21,10 @@ SERVICE_START_TIME=""
|
||||
MAX_BACKUP_AGE_DAYS=30
|
||||
MAX_BACKUPS_TO_KEEP=10
|
||||
BACKUP_ROOT="/mnt/share/media/backups/plex"
|
||||
LOG_ROOT="/mnt/share/media/backups/logs"
|
||||
SHARED_LOG_ROOT="/mnt/share/media/backups/logs"
|
||||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||||
PERFORMANCE_LOG_FILE="${SCRIPT_DIR}/logs/plex-backup-performance.json"
|
||||
LOCAL_LOG_ROOT="${SCRIPT_DIR}/logs"
|
||||
PERFORMANCE_LOG_FILE="${LOCAL_LOG_ROOT}/plex-backup-performance.json"
|
||||
|
||||
# Backup strategy configuration - Always perform full backups
|
||||
|
||||
@@ -107,35 +108,40 @@ log_message() {
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo -e "${CYAN}[${timestamp}]${NC} ${message}"
|
||||
echo "[${timestamp}] $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
mkdir -p "$LOCAL_LOG_ROOT"
|
||||
echo "[${timestamp}] $message" >> "${LOCAL_LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
}
|
||||
|
||||
log_error() {
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo -e "${RED}[${timestamp}] ERROR:${NC} ${message}"
|
||||
echo "[${timestamp}] ERROR: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
mkdir -p "$LOCAL_LOG_ROOT"
|
||||
echo "[${timestamp}] ERROR: $message" >> "${LOCAL_LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
}
|
||||
|
||||
log_success() {
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo -e "${GREEN}[${timestamp}] SUCCESS:${NC} ${message}"
|
||||
echo "[${timestamp}] SUCCESS: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
mkdir -p "$LOCAL_LOG_ROOT"
|
||||
echo "[${timestamp}] SUCCESS: $message" >> "${LOCAL_LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo -e "${YELLOW}[${timestamp}] WARNING:${NC} ${message}"
|
||||
echo "[${timestamp}] WARNING: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
mkdir -p "$LOCAL_LOG_ROOT"
|
||||
echo "[${timestamp}] WARNING: $message" >> "${LOCAL_LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
}
|
||||
|
||||
log_info() {
|
||||
local message="$1"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo -e "${BLUE}[${timestamp}] INFO:${NC} ${message}"
|
||||
echo "[${timestamp}] INFO: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
mkdir -p "$LOCAL_LOG_ROOT"
|
||||
echo "[${timestamp}] INFO: $message" >> "${LOCAL_LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Performance tracking functions
|
||||
@@ -181,6 +187,118 @@ initialize_logs() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 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 "$LOCAL_LOG_ROOT"/*.json; 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)
|
||||
|
||||
# Also clean up old performance log entries (keep structure, remove old entries)
|
||||
if [ -f "$PERFORMANCE_LOG_FILE" ]; then
|
||||
local thirty_days_ago=$(date -d '30 days ago' -Iseconds)
|
||||
local temp_perf_file="${PERFORMANCE_LOG_FILE}.cleanup.tmp"
|
||||
|
||||
if jq --arg cutoff "$thirty_days_ago" '[.[] | select(.timestamp >= $cutoff)]' "$PERFORMANCE_LOG_FILE" > "$temp_perf_file" 2>/dev/null; then
|
||||
local old_count=$(jq length "$PERFORMANCE_LOG_FILE" 2>/dev/null || echo "0")
|
||||
local new_count=$(jq length "$temp_perf_file" 2>/dev/null || echo "0")
|
||||
local removed_count=$((old_count - new_count))
|
||||
|
||||
if [ "$removed_count" -gt 0 ]; then
|
||||
mv "$temp_perf_file" "$PERFORMANCE_LOG_FILE"
|
||||
log_info "Cleaned up $removed_count old performance entries"
|
||||
((cleanup_count += removed_count))
|
||||
else
|
||||
rm -f "$temp_perf_file"
|
||||
fi
|
||||
else
|
||||
rm -f "$temp_perf_file"
|
||||
log_warning "Failed to clean up old performance log entries"
|
||||
((error_count++))
|
||||
fi
|
||||
fi
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Enhanced notification system
|
||||
send_notification() {
|
||||
local title="$1"
|
||||
@@ -1170,6 +1288,11 @@ main() {
|
||||
log_message "Files backed up: $files_backed_up"
|
||||
log_message "Errors encountered: $backup_errors"
|
||||
|
||||
# Sync logs to shared location and cleanup old local logs
|
||||
log_info "Post-backup: synchronizing logs and cleaning up old files"
|
||||
sync_logs_to_shared
|
||||
cleanup_old_local_logs
|
||||
|
||||
if [ "$backup_errors" -gt 0 ]; then
|
||||
log_error "Backup completed with errors"
|
||||
send_notification "Backup Error" "Backup completed with $backup_errors errors" "error"
|
||||
|
||||
Reference in New Issue
Block a user