Skip to content

Commit

Permalink
fix: Correct deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
vinanrra committed Jun 29, 2024
1 parent fbaa847 commit 36746c2
Showing 1 changed file with 1 addition and 22 deletions.
23 changes: 1 addition & 22 deletions scripts/server_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,4 @@ sleep 3s

echo "[INFO] Backup complete"

# Find all .tar.gz files in the backup directory
backups=( "$BACKUP_DESTINATION"/*.tar.gz )

if [[ ${#backups[@]} -eq 0 ]]; then
echo "No backup files found in $BACKUP_DESTINATION"
exit 0
fi

# Sort backups by modification time (newest first)
shopt -s nullglob # Allow sorting empty arrays
backups=($(sort -r --key='{}' <<< "${backups[@]}")) # Double quotes for filenames with spaces

# Keep only the most recent 7 backups
backups_to_keep=("${backups[@]:0:$BACKUP_MAX}")

# Delete remaining backups
for backup in "${backups[@]:$BACKUP_MAX}"; do
rm -f "$backup"
echo "Deleted backup: $backup"
done

echo "Kept ${#backups_to_keep[@]} backups and deleted ${#backups[@]} - ${#backups_to_keep[@]} older ones."
find "$BACKUP_DESTINATION" -type f -name "*.tar.gz" -mtime +7 -delete

0 comments on commit 36746c2

Please sign in to comment.