From 1eaeadf7c8cdf74f1d8edf6e892e408f4d98662d Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 10 Jun 2020 22:22:22 +0200 Subject: update --- bin/backup-clean | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'bin/backup-clean') diff --git a/bin/backup-clean b/bin/backup-clean index 5034c44..fba4279 100755 --- a/bin/backup-clean +++ b/bin/backup-clean @@ -1,32 +1,40 @@ #!/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 +[ "$USER" = root ] || exec sudo "$0" "$@" + +# Durations, in number of seconds. +hd=3600 # hour duration: 60s * 60 +h12=43200 # 12h: hd * 12 +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') +# The following works only for busybox date, not coreutils +# Kept for reference only. #date2ts() { d=${1%_*} t=${1#*_}; date -d "$d${t%??}" +%s; } + +# Convert date to timestamp, using date from GNU coreutils, +# works also with busybox date. 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#??} + t=$1; r=${t#????}; Y=${t%$r} # Year (with century) + t=$r; r=${t#??}; m=${t%$r} # Month + t=$r; r=${t#??}; d=${t%$r} # Day + t=${r#_}; r=${t#??}; H=${t%$r} # Hour + t=$r; r=${t#??}; M=${t%$r} # Minute + S=${t#??} # Second date -d "$Y-$m-$d $H:$M:$S" +%s + #date -jf "%Y-%m-%d %H:%M:%S" "$Y-$m-$d $H:$M:$S" +%s # BSD, MacOSX } ts2date() { date -d "@$1" +'%Y%m%d_%H%M%S'; } tsn=$(date2ts "$now") -# minimal retention delay, according to backup age +# Minimal retention delay in seconds, according to backup age, +# implemented using POSIX shell arithmetic. retention_delay() { d=$((tsn - $1)) if [ $((d < h12)) = 1 ]; then @@ -54,7 +62,7 @@ while getopts :nv opt; do done shift $((OPTIND - 1)) -# sorted list of backups, most recent first +# Sorted list of backups, most recent first dest=${1:-/.history} lbu=$(ls -rv "$dest") for d in $lbu; do -- cgit v1.2.3