remove backups cleanup script and move to main backup-synapse script

fix remove files in cleanup stage than near to be removed
This commit is contained in:
2026-04-15 16:17:59 +03:30
parent 8a08bbc28a
commit d890ded30a
2 changed files with 44 additions and 101 deletions
+44 -8
View File
@@ -3,6 +3,10 @@ set -euo pipefail
### CONFIG ### ### CONFIG ###
DATE=$(date +%F) DATE=$(date +%F)
# for deleting older files
DRY_RUN=false
KEEP_COUNT=1
#BASE_BACKUP_DIR="$HOME/backups/synapse" #BASE_BACKUP_DIR="$HOME/backups/synapse"
BASE_BACKUP_DIR="/mnt/disk/backups/synapse" BASE_BACKUP_DIR="/mnt/disk/backups/synapse"
BACKUP_DIR="$BASE_BACKUP_DIR/$DATE" BACKUP_DIR="$BASE_BACKUP_DIR/$DATE"
@@ -18,18 +22,51 @@ NGINX_DIR="/etc/nginx"
LETSENCRYPT_DIR="/etc/letsencrypt" LETSENCRYPT_DIR="/etc/letsencrypt"
### remove old files ### ### Startup Info ###
cd $BASE_BACKUP_DIR
echo "▶ Running cleanup script ..."
./cleanup-backups.sh --run
### PREP ###
echo "📦 Synapse backup started" echo "📦 Synapse backup started"
echo "📅 Date: $DATE" echo "📅 Date: $DATE"
echo "📁 Base Backups dir: $BASE_BACKUP_DIR"
echo "📁 Backup dir: $BACKUP_DIR" echo "📁 Backup dir: $BACKUP_DIR"
echo 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" mkdir -p "$BACKUP_DIR"
cd "$SYNAPSE_DIR" cd "$SYNAPSE_DIR"
@@ -89,7 +126,6 @@ echo "✅ Full archive created"
echo echo
### Remove the backup dir after full archive ### Remove the backup dir after full archive
cd $BASE_BACKUP_DIR
echo "📦 Removing the Backup directory: $DATE" echo "📦 Removing the Backup directory: $DATE"
sudo rm -r $BACKUP_DIR sudo rm -r $BACKUP_DIR
-93
View File
@@ -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."