25
25
: " ${TAR_COMPRESS_METHOD:= gzip} " # bzip2 gzip zstd
26
26
: " ${ZSTD_PARAMETERS:= -3 --long=25 --single-thread} "
27
27
: " ${PRUNE_BACKUPS_DAYS:= 7} "
28
+ : " ${PRUNE_BACKUPS_COUNT:= } "
28
29
: " ${PRUNE_RESTIC_RETENTION:= --keep-within ${PRUNE_BACKUP_DAYS:- 7} d} "
29
30
: " ${RCON_HOST:= localhost} "
30
31
: " ${RCON_PORT:= 25575} "
@@ -230,6 +231,11 @@ tar() {
230
231
find " ${DEST_DIR} " -maxdepth 1 -name " *.${backup_extension} " -mtime " +${PRUNE_BACKUPS_DAYS} " " ${@ } "
231
232
}
232
233
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
+
233
239
init () {
234
240
mkdir -p " ${DEST_DIR} "
235
241
case " ${TAR_COMPRESS_METHOD} " in
@@ -272,9 +278,17 @@ tar() {
272
278
fi
273
279
}
274
280
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
278
292
fi
279
293
}
280
294
call_if_function_exists " ${@ } "
@@ -285,6 +299,12 @@ rsync() {
285
299
find " ${DEST_DIR} " -maxdepth 1 -type d -mtime " +${PRUNE_BACKUPS_DAYS} " " ${@ } "
286
300
}
287
301
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
+
288
308
init () {
289
309
mkdir -p " ${DEST_DIR} "
290
310
}
@@ -318,9 +338,23 @@ rsync() {
318
338
fi
319
339
}
320
340
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
322
344
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
324
358
fi
325
359
}
326
360
call_if_function_exists " ${@ } "
0 commit comments