Add logging with timestamps to scheduled backup script
- Add log file at backups/scheduled-backup.log (overwritten on each run) - Create log() function that outputs to both console and log file - Add timestamp prefix to all log messages in format [YYYY-MM-DD HH:MM:SS] - Replace all echo statements with log function calls 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -6,30 +6,41 @@ set -euo pipefail
|
|||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
BACKUP_SCRIPT="$SCRIPT_DIR/db-backup-standalone.sh"
|
BACKUP_SCRIPT="$SCRIPT_DIR/db-backup-standalone.sh"
|
||||||
|
LOG_FILE="$SCRIPT_DIR/backups/scheduled-backup.log"
|
||||||
|
|
||||||
|
# Initialize log file (overwrite if exists)
|
||||||
|
mkdir -p "$(dirname "$LOG_FILE")"
|
||||||
|
> "$LOG_FILE"
|
||||||
|
|
||||||
|
# Function to log messages
|
||||||
|
log() {
|
||||||
|
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||||
|
echo "[$timestamp] $*" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
# Ensure the backup script exists and is executable
|
# Ensure the backup script exists and is executable
|
||||||
if [ ! -f "$BACKUP_SCRIPT" ]; then
|
if [ ! -f "$BACKUP_SCRIPT" ]; then
|
||||||
echo "Error: Backup script not found at $BACKUP_SCRIPT"
|
log "Error: Backup script not found at $BACKUP_SCRIPT"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -x "$BACKUP_SCRIPT" ]; then
|
if [ ! -x "$BACKUP_SCRIPT" ]; then
|
||||||
echo "Making backup script executable..."
|
log "Making backup script executable..."
|
||||||
chmod +x "$BACKUP_SCRIPT"
|
chmod +x "$BACKUP_SCRIPT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Run the backup
|
# Run the backup
|
||||||
echo "Starting database backup..."
|
log "Starting database backup..."
|
||||||
"$BACKUP_SCRIPT"
|
"$BACKUP_SCRIPT"
|
||||||
|
|
||||||
# Delete backups older than 7 days
|
# Delete backups older than 7 days
|
||||||
BACKUP_DIR="${BACKUP_DIR:-backups}"
|
BACKUP_DIR="${BACKUP_DIR:-backups}"
|
||||||
if [ -d "$BACKUP_DIR" ]; then
|
if [ -d "$BACKUP_DIR" ]; then
|
||||||
echo "Cleaning up backups older than 7 days in $BACKUP_DIR..."
|
log "Cleaning up backups older than 7 days in $BACKUP_DIR..."
|
||||||
find "$BACKUP_DIR" -name "mongo-volume-backup-*.tar.gz" -type f -mtime +7 -delete
|
find "$BACKUP_DIR" -name "mongo-volume-backup-*.tar.gz" -type f -mtime +7 -delete
|
||||||
echo "Cleanup completed."
|
log "Cleanup completed."
|
||||||
else
|
else
|
||||||
echo "Warning: Backup directory $BACKUP_DIR not found, skipping cleanup."
|
log "Warning: Backup directory $BACKUP_DIR not found, skipping cleanup."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Scheduled backup completed successfully."
|
log "Scheduled backup completed successfully."
|
||||||
|
|||||||
Reference in New Issue
Block a user