From e4b13d264a7450ca753422f167a58748c041f354 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 6 Jun 2020 12:39:42 +0200 Subject: add backup garbage collect bgc --- bin/bgc | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 bin/bgc (limited to 'bin/bgc') diff --git a/bin/bgc b/bin/bgc new file mode 100755 index 0000000..6ff6874 --- /dev/null +++ b/bin/bgc @@ -0,0 +1,65 @@ +#!/bin/sh +# backup garbage collector +# keep: +# 1 backup per year the previous years +hd=3600 # hour duration: 60s * 60 +h12=43200 # 12h +dd=86400 # day duration: hd * 24 +wd=604800 # week duration: dd * 7 +md=2592000 # month duration: dd * 30 +yd=31557600 # year duration: dd * 365.25 + +now=$(date +%Y%m%d_%H%M%S) + +date2ts() { + t=$1; r=${t#????}; Y=${t%$r} + t=$r; r=${t#??}; m=${t%$r} + t=$r; r=${t#??}; d=${t%$r} + t=${r#_}; r=${t#??}; H=${t%$r} + t=$r; r=${t#??}; M=${t%$r} + S=${t#??} + date -d "$Y-$m-$d $H:$M:$S" +%s +} + +# timestamp to date +ts2date() { date -d @$ts +%Y%m%d_%H%M%S; } + +tsn=$(date2ts $now) + +# minimal retention delay, according to age +retention_delay() { + d=$(($tsn - $1)) + if (($d < $h12)); then R1=0 + # keep all backups in the last 12 hours + elif (($d < $dd)); then R1=$hd + # keep 1 backup per hour in the last day + elif (($d < $wd)); then R1=$dd + # keep 1 backup per day in the last week + elif (($d < $md)); then R1=$wd + # keep 1 backup per week in the last month + elif (($d < $yd)); then R1=$md + # keep 1 backup per month in the last year + else R1=$yd + # keep 1 backup per year in the previous years + fi + echo $R1 +} + +# sorted list of backups, most recent first +lbu=$(ssh bip ls /home/backup/swift | sort -r) +for d in $lbu; do + tsc=$(date2ts $d) + if ! [ "$tsp" ]; then + echo keep $d + tsp=$tsc + continue + fi + mrd=$(retention_delay $tsp) + dp=$(($tsp - $tsc)) + if (($dp < $mrd)); then + echo "delete $d" + else + echo "keep $d" + tsp=$tsc + fi +done -- cgit v1.2.3