ansible/roles/restic/files/restic-job.sh

191 lines
4.5 KiB
Bash
Raw Normal View History

2019-12-19 04:09:05 +00:00
#!/bin/bash
# Ansible managed
error_exit() {
printf "%s\n" "$1"
exit 1
}
RESTIC_ETC_PATH=${RESTIC_ETC_PATH:-/etc/restic}
LOCK_PATH=/run/restic
2020-04-21 03:28:32 +00:00
# shellcheck source=/dev/null
source "${RESTIC_ETC_PATH}/env.sh"
2019-12-19 04:09:05 +00:00
KEEP_LOCK=
2019-12-19 04:09:05 +00:00
MAX_ATTEMPTS=60
NICE="ionice -c2 nice -n19"
JOB=$1
LOCK="/run/restic/${JOB}.lock"
2022-08-31 03:36:26 +00:00
KEEP_HOURLY=${KEEP_HOURLY:-24}
KEEP_DAILY=${KEEP_DAILY:-7}
KEEP_WEEKLY=${KEEP_WEEKLY:-5}
KEEP_MONTHLY=${KEEP_MONTHLY:-12}
KEEP_YEARLY=${KEEP_YEARLY:-10}
function finish {
2022-08-31 03:36:26 +00:00
if [ -z "$KEEP_LOCK" ]; then
rm -f "$LOCK"
fi
}
trap finish EXIT
if [ ! -d $LOCK_PATH ]; then
mkdir $LOCK_PATH
fi
2019-12-19 04:09:05 +00:00
if [ -z "$JOB" ]; then
error_exit "job is missing"
fi
JOB_PATH="${RESTIC_ETC_PATH}/jobs/${JOB}"
JOB_ENV="${JOB_PATH}/env.sh"
2019-12-23 17:30:07 +00:00
HOOKS_PATH="${JOB_PATH}/hooks.d"
2019-12-19 04:09:05 +00:00
if [ ! -r "$JOB_ENV" ]; then
error_exit "${JOB_ENV} does not exist"
fi
# shellcheck disable=SC1090
. "$JOB_ENV"
if [ -z "${REPO+x}" ]; then
error_exit "\$REPO is not set"
fi
REPO_PATH="${RESTIC_ETC_PATH}/repos/${REPO}"
REPO_ENV="${REPO_PATH}/env.sh"
if [ ! -r "$REPO_ENV" ]; then
error_exit "${REPO_ENV} does not exist"
fi
# shellcheck disable=SC1090
. "$REPO_ENV"
EXCLUDE_PATH="${JOB_PATH}/exclude.txt"
if [ -z "${PATHS+x}" ]; then
error_exit "\$PATHS is not set"
fi
START="$(date +%s)"
2024-04-14 22:25:38 +00:00
if [[ -n "$($RESTIC_PATH list locks -q)" ]]; then
error_exit "repo is locked"
fi
if [ -f "$LOCK" ]; then
pid=$(cat "$LOCK")
if ! kill -0 "$pid" 2> /dev/null; then
printf "removing orphaned lock, pid %d does not exist\n" "$pid"
rm -f "$LOCK"
else
2020-05-09 16:00:06 +00:00
if [[ -f "/proc/${pid}/cmdline" ]]; then
cmdline=$(tr "\0" " " <"/proc/${pid}/cmdline")
if ! [[ $cmdline =~ $(basename "$0") ]]; then
printf "removing orphaned lock, pid %d belongs to another process\n" "$pid"
rm -f "$LOCK"
2020-08-22 15:01:56 +00:00
else
KEEP_LOCK=1
error_exit "another job is running, pid=${pid}"
2020-05-09 16:00:06 +00:00
fi
fi
fi
fi
echo $$ > "$LOCK"
LOCKED=$(($(date +%s) - START))
2022-08-31 03:36:26 +00:00
printf "prune: started, keep hourly:%d daily:%d weekly:%d monthly:%d year:%d\n" \
"$KEEP_HOURLY" \
"$KEEP_DAILY" \
"$KEEP_WEEKLY" \
"$KEEP_MONTHLY" \
"$KEEP_YEARLY"
$RESTIC_PATH forget \
--quiet \
--host "$(hostname -f)" \
--keep-hourly "$KEEP_HOURLY" \
--keep-daily "$KEEP_DAILY" \
--keep-weekly "$KEEP_WEEKLY" \
--keep-monthly "$KEEP_MONTHLY" \
--keep-yearly "$KEEP_YEARLY" \
--prune
printf "prune: complete\n"
2019-12-23 17:29:36 +00:00
printf "job '%s' started\n" "$JOB"
2019-12-23 17:30:07 +00:00
if [ -d "${HOOKS_PATH}" ]; then
printf "running '%s' job pre-hooks\n" "$JOB"
2019-12-25 23:23:36 +00:00
if ! run-parts --exit-on-error -v -a pre "${HOOKS_PATH}"; then
printf "'%s' pre-hooks failed, running post-hooks and exiting\n" "$JOB"
run-parts --exit-on-error -v -a post "${HOOKS_PATH}"
exit 1
fi
2019-12-23 17:30:07 +00:00
fi
2019-12-19 04:09:05 +00:00
counter=0
sleep=1
rc=1
2019-12-27 06:04:59 +00:00
printf "restic started\n"
2019-12-19 04:09:05 +00:00
until [ $counter -eq "$MAX_ATTEMPTS" ] || [ $rc -eq 0 ]; do
if [ -r "$EXCLUDE_PATH" ]; then
$NICE "$RESTIC_PATH" backup -q --exclude-file="${EXCLUDE_PATH}" "${PATHS}"
else
$NICE "$RESTIC_PATH" backup -q "${PATHS}"
fi
rc=$?
if [ $rc -eq 0 ]; then
break
else
sleep=$((counter * 5))
printf "sleeping for %d seconds (%d)\n" $sleep $counter
sleep $sleep
2019-12-19 04:09:05 +00:00
fi
(( counter++ ))
done
2019-12-27 06:04:59 +00:00
printf "restic complete\n"
2019-12-19 04:09:05 +00:00
2022-08-31 03:36:26 +00:00
if [ $rc -ne 0 ] && [ "$counter" -eq "$MAX_ATTEMPTS" ]; then
2019-12-19 04:09:05 +00:00
printf "restic job timed out, exiting\n"
2019-12-23 17:29:36 +00:00
else
printf "job '%s' complete\n" "$JOB"
fi
2019-12-23 17:30:07 +00:00
if [ -d "${HOOKS_PATH}" ]; then
printf "running '%s' job post-hooks\n" "$JOB"
2019-12-25 23:23:36 +00:00
run-parts --exit-on-error -v -a post "${HOOKS_PATH}"
2019-12-19 04:09:05 +00:00
fi
END=$(date +%s)
if [ -d /var/spool/node_exporter/textfile_collector ]; then
cat << EOF > "/var/spool/node_exporter/textfile_collector/restic.prom.$$"
node_restic_duration_seconds{restic_job="${JOB}"} $((END - START))
node_restic_lock_duration_seconds{restic_job="${JOB}"} $LOCKED
node_restic_last_run_time{restic_job="${JOB}"} $END
node_restic_retries{restic_job="${JOB}"} $counter
EOF
if [ -f /var/spool/node_exporter/textfile_collector/restic.prom ]; then
cat /var/spool/node_exporter/textfile_collector/restic.prom "/var/spool/node_exporter/textfile_collector/restic.prom.$$" |
tac |
awk '!seen[$1]++' |
tac |
sponge "/var/spool/node_exporter/textfile_collector/restic.prom.$$"
fi
mv "/var/spool/node_exporter/textfile_collector/restic.prom.$$" \
/var/spool/node_exporter/textfile_collector/restic.prom
fi