Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 68 lines (54 sloc) 1.65 KB
#!/bin/bash
# Start a new Tarsnap backup if no other backup is running and the computer is connected to a wifi.
# The idea is not to do a backup when it is connected to a mobile data connection (LTE/UMTS).
#
# Requirements: X11, tarnap and tarsnapper
#
# You have to configure sudo to allow `tarsnapper` and `tarsnap` without password.
set -e
source `dirname $0`/backup-shared.sh
lock_file=~/.backup-lock
last_backup=~/.last-backup # see the `outdated-backup' script
notify() {
local icon=$1
local title=$2
local body=$3
echo "${title}. ${body}" >&2
notify-send -i "${icon}" "${title}" "${body}"
}
do_backup() {
sudo /usr/local/bin/tarsnapper -c /root/.tarsnapper.conf make
}
backup_still_running() {
if [[ -f "${lock_file}" ]]; then
local now="$(date +%s)"
local running=$(cat "${lock_file}")
echo "${now} - ${running}" | bc
else
return 1
fi
}
if since=$(backup_still_running); then
notify info "Not starting another backup" "A previous backup job is still running (since ${since} seconds)."
exit 1
else
date +%s > "${lock_file}"
fi
if $(do_backup); then
start_time=$(cat "${lock_file}")
end_time=$(date +%s)
duration=$(echo "${end_time} - ${start_time}" | bc)
# see the `outdated-backup' script
date +%s > "${last_backup}"
else
if diff=$(outdated-backup); then
notify error "Backup failed too often" \
"Your backup has not been done for ${diff} seconds and can't be started. Do it manually ASAP!"
else
notify warning "Backup did not finish" \
"The backup returned with a non-zero exit code. Please execute it manually to find out what's going on."
fi
rm "${lock_file}"
exit 1
fi
rm "${lock_file}"