mochi/nixos/hosts/varda/resources/prune-backups.sh

21 lines
611 B
Bash
Raw Normal View History

2024-09-26 09:39:21 -05:00
# Set the backup directory
2025-01-03 17:38:24 -06:00
BACKUP_DIR="/mnt/storage/forgejo/backups"
2024-09-26 09:39:21 -05:00
# Keep the 3 most recent backups
2025-01-03 17:38:24 -06:00
KEEP_NUM=7
2024-09-26 09:39:21 -05:00
echo "Starting backup cleanup process..."
echo "Keeping the $KEEP_NUM most recent backups in $BACKUP_DIR"
# Find all backup files, sort by modification time (newest first),
# skip the first 3, and delete the rest
find "$BACKUP_DIR" -type f -name "forgejo-dump-*" -print0 |
sort -z -t_ -k2 -r |
tail -z -n +$((KEEP_NUM + 1)) |
while IFS= read -r -d '' file; do
echo "Deleting: $file"
rm -f "$file"
done
echo "Cleanup complete. Deleted all but the $KEEP_NUM most recent backups."