summaryrefslogtreecommitdiff
path: root/bin/backup-clean
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2020-06-10 22:22:22 +0200
committerMarc Vertes <mvertes@free.fr>2020-06-10 22:22:22 +0200
commit1eaeadf7c8cdf74f1d8edf6e892e408f4d98662d (patch)
tree1821bc5a650f54760fa271e666925e63fbdc7f63 /bin/backup-clean
parentbc43abee15ba8634fba1d279a3c8378111501960 (diff)
update
Diffstat (limited to 'bin/backup-clean')
-rwxr-xr-xbin/backup-clean40
1 files changed, 24 insertions, 16 deletions
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