#!/bin/bash # # A script to continuously synchronize Kubernetes pod logs from a source # directory to a destination directory using rsync and inotifywait. # set -e # --- Configuration --- # Source directory for logs SOURCE_DIR="/var/log/pods" # Destination directory for backup DEST_DIR="/opt/k8s-log" # Log file for the script's output LOG_FILE="/var/log/k8s-log-sync.log" # --- Functions --- # Function to log messages with a timestamp log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } # Function to perform synchronization sync_dirs() { # The -a option is for archive mode (preserves permissions, etc.) # The -v option is for verbose output (can be removed if not needed) # The --delete option ensures that files deleted from the source are also deleted from the destination rsync --omit-dir-times --no-perms --ignore-errors -v -r --delete "$SOURCE_DIR/" "$DEST_DIR/" } # --- Main Execution --- # Ensure the destination directory exists mkdir -p "$DEST_DIR" touch "$LOG_FILE" log "Service started." # Perform an initial full sync when the script starts log "Starting initial sync from $SOURCE_DIR to $DEST_DIR" sync_dirs log "Initial sync complete." # Use inotifywait to monitor the source directory for changes log "Watching for changes in $SOURCE_DIR..." # -m: monitor continuously # -r: recursive watch # -q: quiet mode (suppress verbose output from inotifywait itself) # -e: events to watch for (create, modify, delete, move) # --format: specifies the output format, '%w%f' gives the full path of the changed file inotifywait -m -r -q -e create,modify,delete,move --format '%w%f' "$SOURCE_DIR" | while read FILE do log "Change detected in '$FILE'. Syncing directories..." sync_dirs log "Sync complete. Waiting for the next change." done
#!/bin/bash # # A script to continuously synchronize Kubernetes pod logs from a source # directory to a destination directory using rsync and inotifywait. # set -e # --- Configuration --- # Source directory for logs SOURCE_DIR="/var/log/pods" # Destination directory for backup DEST_DIR="/opt/k8s-log" # Log file for the script's output LOG_FILE="/var/log/k8s-log-sync.log"
EXCLUDE_OPTIONS="--exclude='hskj-ai-middleware-log' --exclude='hskj-aimage-test-log'" # --- Functions --- # Function to log messages with a timestamp log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } # Function to perform synchronization sync_dirs() { # The -a option is for archive mode (preserves permissions, etc.) # The -v option is for verbose output (can be removed if not needed) # The --delete option ensures that files deleted from the source are also deleted from the destination rsync --omit-dir-times --no-perms --ignore-errors -v -r --delete "$EXCLUDE_OPTIONS" "$SOURCE_DIR" "$DEST_DIR" } # --- Main Execution --- # Ensure the destination directory exists mkdir -p "$DEST_DIR" touch "$LOG_FILE" log "Service started." # Perform an initial full sync when the script starts log "Starting initial sync from $SOURCE_DIR to $DEST_DIR" sync_dirs log "Initial sync complete." # Use inotifywait to monitor the source directory for changes log "Watching for changes in $SOURCE_DIR..." # -m: monitor continuously # -r: recursive watch # -q: quiet mode (suppress verbose output from inotifywait itself) # -e: events to watch for (create, modify, delete, move) # --format: specifies the output format, '%w%f' gives the full path of the changed file inotifywait -m -r -q -e create,modify,delete,move --format '%w%f' "$SOURCE_DIR" | while read FILE do log "Change detected in '$FILE'. Syncing directories..." sync_dirs log "Sync complete. Waiting for the next change." sleep 30m done
[Unit] Description=Real-time Log Sync Service for K8s Pods Documentation=https://your-documentation-link-here # (Optional) After=network.target
[Service] # User that will run the script. Use root if you need to access /var/log. User=root # Group for the process. Group=root # Path to the executable script. ExecStart=/usr/local/bin/sync_script.sh # Restart policy: always restart the service if it fails. Restart=always # Time to wait before restarting the service. RestartSec=10 # Standard output and error logging configuration. # We are redirecting output to a file within the script, # but systemd can also handle it via journald. StandardOutput=journal StandardError=journal
[Install] # Defines the target to link this service to when enabling it. # multi-user.target is a standard target for services that should start at boot. WantedBy=multi-user.target