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 ###
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