Skip to content

Commit a6f960c

Browse files
authored
added prune based on backup count to tar/rsync (#194)
1 parent 3d04028 commit a6f960c

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/*.iml
22
/.idea/
3-
/examples/mc-backups
3+
/examples/mc-backups
4+
/*.code-workspace

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Provides a side-car container to back up [itzg/minecraft-server](https://github.
1717
- `PAUSE_IF_NO_PLAYERS`=false
1818
- `PLAYERS_ONLINE_CHECK_INTERVAL`=5m
1919
- `PRUNE_BACKUPS_DAYS`=7
20+
- `PRUNE_BACKUPS_COUNT`= -disabled unless set (only works with tar/rsync)
2021
- `PRUNE_RESTIC_RETENTION`=--keep-within 7d
2122
- `RCON_HOST`=localhost
2223
- `RCON_PORT`=25575

scripts/opt/backup-loop.sh

+39-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fi
2525
: "${TAR_COMPRESS_METHOD:=gzip}" # bzip2 gzip zstd
2626
: "${ZSTD_PARAMETERS:=-3 --long=25 --single-thread}"
2727
: "${PRUNE_BACKUPS_DAYS:=7}"
28+
: "${PRUNE_BACKUPS_COUNT:=}"
2829
: "${PRUNE_RESTIC_RETENTION:=--keep-within ${PRUNE_BACKUP_DAYS:-7}d}"
2930
: "${RCON_HOST:=localhost}"
3031
: "${RCON_PORT:=25575}"
@@ -230,6 +231,11 @@ tar() {
230231
find "${DEST_DIR}" -maxdepth 1 -name "*.${backup_extension}" -mtime "+${PRUNE_BACKUPS_DAYS}" "${@}"
231232
}
232233

234+
_find_extra_backups() {
235+
find "${DEST_DIR}" -maxdepth 1 -name "*.${backup_extension}" -exec ls -NtA {} \+ | \
236+
tail -n +$((PRUNE_BACKUPS_COUNT + 1))
237+
}
238+
233239
init() {
234240
mkdir -p "${DEST_DIR}"
235241
case "${TAR_COMPRESS_METHOD}" in
@@ -272,9 +278,17 @@ tar() {
272278
fi
273279
}
274280
prune() {
275-
log INFO "Pruning backup files older than ${PRUNE_BACKUPS_DAYS} days"
276-
if [ -n "$(_find_old_backups -print -quit)" ]; then
277-
_find_old_backups -print -delete | awk '{ printf "Removing %s\n", $0 }' | log INFO
281+
282+
if [ -n "${PRUNE_BACKUPS_DAYS}" ] && [ "${PRUNE_BACKUPS_DAYS}" -gt 0 ]; then
283+
log INFO "Pruning backup files older than ${PRUNE_BACKUPS_DAYS} days"
284+
if [ -n "$(_find_old_backups -print -quit)" ]; then
285+
_find_old_backups -print -delete | awk '{ printf "Removing %s\n", $0 }' | log INFO
286+
fi
287+
fi
288+
289+
if [ -n "${PRUNE_BACKUPS_COUNT}" ] && [ "${PRUNE_BACKUPS_COUNT}" -gt 0 ]; then
290+
log INFO "Pruning backup files to keep only the latest ${PRUNE_BACKUPS_COUNT} backups"
291+
_find_extra_backups | xargs -n 1 rm -v | log INFO
278292
fi
279293
}
280294
call_if_function_exists "${@}"
@@ -285,6 +299,12 @@ rsync() {
285299
find "${DEST_DIR}" -maxdepth 1 -type d -mtime "+${PRUNE_BACKUPS_DAYS}" "${@}"
286300
}
287301

302+
_find_extra_backups() {
303+
find "${DEST_DIR}" -maxdepth 1 -type d ! -path "${DEST_DIR}" -print0 -exec ls -NtAd {} \+ | \
304+
tail -n +$((PRUNE_BACKUPS_COUNT + 1)) | \
305+
tr '\n' '\0'
306+
}
307+
288308
init() {
289309
mkdir -p "${DEST_DIR}"
290310
}
@@ -318,9 +338,23 @@ rsync() {
318338
fi
319339
}
320340
prune() {
321-
if [ -n "$(_find_old_backups -print -quit)" ]; then
341+
342+
if [ -n "${PRUNE_BACKUPS_DAYS}" ] && [ "${PRUNE_BACKUPS_DAYS}" -gt 0 ]; then
343+
if [ -n "$(_find_old_backups -print -quit)" ]; then
322344
log INFO "Pruning backup files older than ${PRUNE_BACKUPS_DAYS} days"
323-
_find_old_backups -print -exec rm -r {} + | awk '{ printf "Removing %s\n", $0 }' | log INFO
345+
_find_old_backups -print -exec rm -r {} + | awk '{ printf "Removing %s\n", $0 }' | log INFO
346+
fi
347+
fi
348+
349+
if [ -n "${PRUNE_BACKUPS_COUNT}" ] && [ "${PRUNE_BACKUPS_COUNT}" -gt 0 ]; then
350+
log INFO "Pruning backup files to keep only the latest ${PRUNE_BACKUPS_COUNT} backups"
351+
_find_extra_backups | xargs -0 -I {} rm -rv {} | awk -v dest_dir="${DEST_DIR}" '
352+
{
353+
sub(/removed directory /, "")
354+
if ($0 !~ dest_dir "/.*/.*") {
355+
printf "Removing %s\n", $0
356+
}
357+
}'| log INFO
324358
fi
325359
}
326360
call_if_function_exists "${@}"

0 commit comments

Comments
 (0)