#!/usr/bin/env bash set -euo pipefail # Script to run scheduled database backups and clean up old files # This script calls db-backup-standalone.sh and then removes backups older than 7 days SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 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 if [ ! -f "$BACKUP_SCRIPT" ]; then log "Error: Backup script not found at $BACKUP_SCRIPT" exit 1 fi if [ ! -x "$BACKUP_SCRIPT" ]; then log "Making backup script executable..." chmod +x "$BACKUP_SCRIPT" fi # Run the backup log "Starting database backup..." "$BACKUP_SCRIPT" # Delete backups older than 7 days BACKUP_DIR="${BACKUP_DIR:-backups}" if [ -d "$BACKUP_DIR" ]; then 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 log "Cleanup completed." else log "Warning: Backup directory $BACKUP_DIR not found, skipping cleanup." fi log "Scheduled backup completed successfully."