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 | 5 +++-- bin/backup-clean | 40 ++++++++++++++++++++++++---------------- bin/diffdir | 12 ++++++------ 3 files changed, 33 insertions(+), 24 deletions(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index cc98d63..3f1d586 100755 --- a/bin/backup +++ b/bin/backup @@ -1,10 +1,12 @@ #!/bin/sh +[ "$USER" = root ] || exec sudo "$0" "$@" + # incremental backup using rsync(1) die() { echo "$0: fatal: $@" >&2; exit 1; } -[ "$(id -u)" = 0 ] || die must run as root +#[ "$(id -u)" = 0 ] || die must run as root while getopts :v opt; do case $opt in @@ -14,7 +16,6 @@ while getopts :v opt; do done shift $((OPTIND - 1)) -#dest=${1:-/backup/$(hostname)} dest=${1:-/.history} date=$(date +%Y%m%d_%H%M%S) last=$(rsync --list-only $dest/ 2>/dev/null | cut -b 47- | tail -1) 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 diff --git a/bin/diffdir b/bin/diffdir index 57997ed..d1a8ae2 100755 --- a/bin/diffdir +++ b/bin/diffdir @@ -4,8 +4,8 @@ Version='diffdir-0.1' diffdir() { [ -f "/etc/backup/ignore" ] && xf=--exclude-from=/etc/backup/ignore || xf= - rsync -aDSniv $xf --delete $Optx "$2/" "$1" | - awk -v OptF="${OptF#./}" -v prefix=$prefix ' + rsync -aDSnivx "$xf" --delete "$2/" "$1" | + awk -v OptF="${OptF#./}" -v prefix="$prefix" ' NF == 0 {exit} NR < 2 || /\/*\.dv$/ || /\/$/ {next} # Match an itemized status for all versions of rsync -i @@ -15,13 +15,13 @@ diffdir() { key == "*deleting" {print "deleted " prefix file; next} substr(key, 3, 7) == "+++++++" {print "created " prefix file; next} { # Avoid false positive if only mtime is changed. - of = "'$1'/" file; gsub("'\''", "'\'\\\\\'\''", of) - nf = "'$2'/" file; gsub("'\''", "'\'\\\\\'\''", nf) + of = "'"$1"'/" file; gsub("'\''", "'\'\\\\\'\''", of) + nf = "'"$2"'/" file; gsub("'\''", "'\'\\\\\'\''", nf) if (substr(key, 2, 1) == "L") { # Symlink src = target = file sub(/.* -> /, "", target); sub(/ -> .*/, "", src); - "readlink '$2'/" src | getline otarget + "readlink '"$2"'/" src | getline otarget if (target != otarget) print "changed " src } else if (system("cmp -s '\''" of "'\'\ \''" nf "'\''")) @@ -35,7 +35,7 @@ diffdir() { while getopts :V opt; do case $opt in (V) echo "$Version"; exit ;; - (*) echo "Usage: $0 [-V]"; exit 1 ;; + (*) echo "Usage: $0 [-V] old new"; exit 1 ;; esac done shift $((OPTIND - 1)) -- cgit v1.2.3