add cleanup-backups script to remove older backups

This commit is contained in:
2026-03-15 01:06:58 +03:30
parent d559cdcf80
commit 1841ce8c59
2 changed files with 107 additions and 3 deletions
+15 -3
View File
@@ -41,7 +41,7 @@ echo
### 3️⃣ Synapse data backup
echo "📁 Backing up Synapse data directory..."
tar -cJvf "$BACKUP_DIR/synapse-data.tar.xz" \
tar -cJf "$BACKUP_DIR/synapse-data.tar.xz" \
-C "$SYNAPSE_DATA_DIR" .
echo "✅ Synapse data backup done"
echo
@@ -74,10 +74,22 @@ echo
### 8️⃣ Optional: Full combined archiveve (run with sudo)
echo "📦 Creating full backup archive..."
sudo tar -czvf "$BASE_BACKUP_DIR/full-backup-$DATE.tar.gz" \
tar -czvf "$BASE_BACKUP_DIR/full-backup-$DATE.tar.gz" \
-C "$BASE_BACKUP_DIR" "$DATE"
echo "✅ Full archive created"
echo
echo "✅✅ Backup completed successfully"
### Remove the backup dir after full archive
cd $BASE_BACKUP_DIR
echo "📦 Removing the Backup directory: $DATE"
sudo rm -r $BACKUP_DIR
echo "Backup directory removed."
echo
echo "✅✅ Backup completed successfully"
echo
echo "▶ Running cleanup script ..."
./cleanup-backups.sh --run
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env bash
set -euo pipefail
### CONFIG ###
DATE=$(date +%F)
BACKUPS_DIR="$HOME/backups/synapse"
DRY_RUN=true
KEEP_COUNT=4
# 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."