#!/usr/bin/env bash set -euo pipefail ### CONFIG ### DATE=$(date +%F) #BACKUPS_DIR="$HOME/backups/synapse" BACKUPS_DIR="/mnt/disk/backups/synapse" DRY_RUN=true KEEP_COUNT=1 # Function to display usage information usage() { echo "Usage: $0 [--run]" exit 1 } # Parse command-line arguments for arg in "$@"; do case $arg in --run) DRY_RUN=false shift ;; *) usage ;; esac done ### 8️⃣ Optional: Full combined archiveve (run with sudo) echo "📅 Date: $DATE" echo "📁 Backups dir: $BACKUPS_DIR" echo "📦 Start reading backup files..." echo TIMESTAMP_FILE="$BACKUPS_DIR/backup_timestamps.txt" # Check if the backups directory exists if [ ! -d "$BACKUPS_DIR" ]; then echo "Backup directory does not exist: $BACKUPS_DIR" exit 1 fi cd $BACKUPS_DIR # Count the number of existing backups TOTAL_BACKUPS=$(find . -name 'full-backup-*.tar.gz' | wc -l) echo "Existing backup files ($TOTAL_BACKUPS) :" # Find all full-backup-*.tar.gz files and list them find . -name 'full-backup-*.tar.gz'| sort -k2,2 -t. -n -r | while IFS= read -r file; do echo "- $file" done echo # Find and list files that should be removed echo "Files that will be deleted:" files_to_delete=() while IFS= read -r -d $'\0' dir; do files_to_delete+=("$dir") echo "- $dir" done < <(find . -name 'full-backup-*.tar.gz' -mtime +$KEEP_COUNT -print0) echo # Prompt for confirmation if not in dry-run mode #if [ "$DRY_RUN" = false ]; then # read -p "Do you want to delete these files? (y/n): " confirm # if [[ $confirm != [yY] ]]; then # echo "Deletion cancelled." # exit 0 # fi # echo #fi # Delete the files in dry-run mode or actually delete them for file in "${files_to_delete[@]}"; do if [ "$DRY_RUN" = true ]; then echo "Will be deleted (Dry-Run): $file" else rm "$file" echo "Deleted $file" fi done echo # Update the timestamp file (optional, but good practice) if [ "$DRY_RUN" = false ]; then echo "$(date +%F)" > "$BACKUPS_DIR/backup_timestamps.txt" fi echo "Cleanup completed."