diff --git a/backup-synapse.sh b/backup-synapse.sh index 974f25f..fc177fa 100755 --- a/backup-synapse.sh +++ b/backup-synapse.sh @@ -3,6 +3,10 @@ set -euo pipefail ### CONFIG ### DATE=$(date +%F) + +# for deleting older files +DRY_RUN=false +KEEP_COUNT=1 #BASE_BACKUP_DIR="$HOME/backups/synapse" BASE_BACKUP_DIR="/mnt/disk/backups/synapse" BACKUP_DIR="$BASE_BACKUP_DIR/$DATE" @@ -18,18 +22,51 @@ NGINX_DIR="/etc/nginx" LETSENCRYPT_DIR="/etc/letsencrypt" -### remove old files ### -cd $BASE_BACKUP_DIR -echo "▶ Running cleanup script ..." - -./cleanup-backups.sh --run - -### PREP ### +### Startup Info ### echo "📦 Synapse backup started" echo "📅 Date: $DATE" +echo "📁 Base Backups dir: $BASE_BACKUP_DIR" echo "📁 Backup dir: $BACKUP_DIR" echo +### remove old files ### +cd $BASE_BACKUP_DIR +echo "▶ cleanup older backup files ..." + +cd $BASE_BACKUP_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' -newermt "$(( $KEEP_COUNT+1 )) days ago" ! -newermt "$KEEP_COUNT days ago" -print0) +echo + +# 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 + +echo "Cleanup completed." + +### PREP ### mkdir -p "$BACKUP_DIR" cd "$SYNAPSE_DIR" @@ -89,7 +126,6 @@ echo "✅ Full archive created" echo ### Remove the backup dir after full archive -cd $BASE_BACKUP_DIR echo "📦 Removing the Backup directory: $DATE" sudo rm -r $BACKUP_DIR diff --git a/cleanup-backups.sh b/cleanup-backups.sh deleted file mode 100755 index 61d9455..0000000 --- a/cleanup-backups.sh +++ /dev/null @@ -1,93 +0,0 @@ -#!/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."