Commit fdd93406 by Philipp Adolf

Initial version

parents
#!/usr/bin/env bash
declare -r basedir="${HOME}/.backup"
declare -r lock="${basedir}/lock"
declare -ra rsnapshotargs=("-c" "${HOME}/.rsnapshot.home.conf")
shopt -s nullglob
getoldest() {
local -r interval="$1"
local oldest="-1" current="" dir=""
for dir in "${basedir}/${interval}."*; do
current="${dir##*.}"
if (( $current > $oldest )); then
oldest="${current}"
fi
done
if (( oldest >= 0 )); then
echo "${interval}.${oldest}"
return 0
else
return 1
fi
}
getnewest() {
local -r newest="${interval}.0"
[[ -e "${basedir}/$newest" ]] && echo "$newest"
}
getmtime() {
stat -c %Y "${basedir}/${1}"
}
epochtodate() {
dconv -i %s "$1"
}
getmtimeepoch() {
epochtodate "$(getmtime "$1")"
}
testdifference() {
local -r date1="$1" date2="$2" difference="$3"
dtest "$(dadd "$date1" "$difference")" -ge "$date2"
}
needsdaily() {
local -r newestdaily="$(getnewest daily)"
if [[ -z "$newestdaily" ]]; then
return 0
fi
local -r oldesthourly="$(getoldest hourly)"
if testdifference "$(getmtimeepoch "$newestdaily")" "$(getmtimeepoch "$oldesthourly")" 1d; then
return 0
fi
return 1
}
dobackup() {
ionice -c 2 -n 7 nice rsnapshot "${rsnapshotargs[@]}" -v "$1"
}
(
if ! flock -x -w 60 200; then
notify-send -u critical -a backup "Backup failed" "Failed to acquire lock"
exit 1
fi
if needsdaily; then
dobackup daily
fi
dobackup hourly
) 200>"$lock"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment