16 Commits

Author SHA1 Message Date
31309f2576 version bump
All checks were successful
continuous-integration/drone/tag Build is passing
2024-10-22 11:21:55 +02:00
db8e656613 gather all logs 2024-10-22 11:21:13 +02:00
9725a09c06 version bump
All checks were successful
continuous-integration/drone/tag Build is passing
2024-10-16 11:57:01 +02:00
bd300718e7 fix error redir 2024-10-16 11:56:23 +02:00
cfd15aca3b version bump
All checks were successful
continuous-integration/drone/tag Build is passing
2024-10-15 09:23:30 +02:00
4b541698df ignored vscode dir 2024-10-15 09:21:25 +02:00
d5886da0a4 fix exact grep 2024-10-15 09:20:15 +02:00
784cda10eb run as root 2024-10-15 09:13:10 +02:00
2adcac027b stale lockfile check 2024-10-15 08:16:57 +02:00
4cb2c5caee version bump
All checks were successful
continuous-integration/drone/tag Build is passing
2024-10-14 17:34:45 +02:00
fc953c1bdd settings for "unreachable" exit status 2024-10-14 17:31:36 +02:00
359d346c33 exit if lockfile present 2024-10-14 17:27:44 +02:00
1e3638d474 version bump
All checks were successful
continuous-integration/drone/tag Build is passing
2024-09-24 16:09:23 +02:00
7b84ca86f4 docs 2024-09-24 16:09:10 +02:00
d856635747 added destination aliveness check 2024-09-24 15:50:22 +02:00
693ded862c fix name
All checks were successful
continuous-integration/drone/tag Build is passing
2024-09-18 18:49:03 +02:00
6 changed files with 115 additions and 7 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.vscode/

View File

@@ -208,7 +208,7 @@ If you develop a new program, and you want it to be of the greatest possible use
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
btrkbk-cron
btrbk-cron
Copyright (C) 2024 paspo
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
@@ -221,7 +221,7 @@ Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
btrkbk-cron Copyright (C) 2024 paspo
btrbk-cron Copyright (C) 2024 paspo
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.

View File

@@ -1,4 +1,4 @@
# btrkbk-cron
# btrbk-cron
btrbk cron wrapper with healthchecks support.
@@ -12,6 +12,21 @@ Run this:
debuild -i -us -uc
```
## New release
Add comments:
```sh
dch -v 0.2 "comment 1"
dch -v 0.2 "comment 2"
```
Release:
```sh
dch --release --distribution stable
```
## Installation
Just install the package as usual.

View File

@@ -1,11 +1,21 @@
#!/bin/bash
CONF=/etc/btrbk/btrbk-cron.conf
LOCKFILE=/run/btrbk/btrbk.lock
LOGFILE=$(mktemp)
trap 'rm -f -- "${LOGFILE}"' EXIT
# check for superpowers
if [[ "${EUID}" -ne 0 ]] ; then
echo "Please run me as root"
exit 1
fi
echo "---" | tee -a "${LOGFILE}"
echo "Start btrbk-cron: $(date||true)" | tee -a "${LOGFILE}"
if [[ ! -f "${CONF}" ]] ; then
echo "Warning: Can't read config file (${CONF}), healthchecks disabled."
echo "Warning: Can't read config file (${CONF}), healthchecks disabled." | tee -a "${LOGFILE}"
else
# shellcheck disable=SC1090,SC1091
source "${CONF}"
@@ -13,20 +23,69 @@ fi
HEALTHCHECK_URL=${HEALTHCHECK_URL:-http://127.0.0.1}
HEALTHCHECK_ENABLE=${HEALTHCHECK_ENABLE:-false}
FAIL_IF_TARGET_UNREACHABLE=${FAIL_IF_TARGET_UNREACHABLE:-true}
CURLOPTS=(-fsS -m 10 --retry 5)
if [[ ! -d /run/btrbk ]] ; then
echo "Warning: Creating missing directory: /run/btrbk " | tee -a "${LOGFILE}"
mkdir -p /run/btrbk
fi
echo "---" | tee -a "${LOGFILE}"
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
echo "Info: Healthchecks enabled with URL: ${HEALTHCHECK_URL}"
else
echo "Info: Healthchecks disabled"
fi
# lockfile check
if [[ -f "${LOCKFILE}" ]] ; then
# check if stale lockfile
if [[ $(pgrep --exact --count btrbk || true) -eq 0 ]] ; then
echo "Removing stale lock file: ${LOCKFILE}"
rm "${LOCKFILE}"
else
# another instance is really running, we exit
echo "Another instance is still running" | tee -a "${LOGFILE}"
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" --data-binary "@${LOGFILE}" "${HEALTHCHECK_URL}/log"
fi
rm -f -- "${LOGFILE}"
exit 0
fi
fi
HOST=$(grep -E '^[\ \t]*target[\ \t]+send-receive' /etc/btrbk/btrbk.conf)
# TODO: support for dashes in hostname and IPv6 ("[2001:db8::7]")
HOST=$(echo "${HOST}" | sed -r 's/.*ssh:\/\/([0-9\.a-zA-Z]+).*/\1/g' )
IDENTITYFILE=$(grep -E '^[\ \t]*ssh_identity[\ \t].*' /etc/btrbk/btrbk.conf )
IDENTITYFILE=$(echo "${IDENTITYFILE}" | sed -r 's/.*ssh_identity[\ \t]+//g')
SSHUSER=$(grep -E '^[\ \t]*ssh_user[\ \t].*' /etc/btrbk/btrbk.conf )
SSHUSER=$(echo "${SSHUSER}" | sed -r 's/.*ssh_user[\ \t]+//g')
SSH_OK=$(ssh -i "${IDENTITYFILE}" "${SSHUSER}@${HOST}" "which btrfs")
if [[ "${SSH_OK}" = "" ]] ; then
echo "Warning: exiting because of backup destination unreachable" | tee -a "${LOGFILE}"
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" --data-binary "@${LOGFILE}" "${HEALTHCHECK_URL}/log"
fi
rm -f -- "${LOGFILE}"
if [[ "${FAIL_IF_TARGET_UNREACHABLE}" = "true" ]] ; then
exit 1
else
exit 0
fi
fi
echo "Start backup: $(date||true)" | tee -a "${LOGFILE}"
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
curl "${CURLOPTS[@]}" "${HEALTHCHECK_URL}/start"
fi
echo
echo "healthcheck notified! (start)" | tee -a "${LOGFILE}"
(nice -n 19 /usr/bin/ionice -c idle /usr/bin/btrbk -v --progress run >> "${LOGFILE}" ; ) 2>&1
(nice -n 19 /usr/bin/ionice -c idle /usr/bin/btrbk -v --progress run 2>&1 || true) | tee -a "${LOGFILE}" 2>&1
ret=$?
if [[ "${ret}" -eq "0" ]]; then
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then

View File

@@ -1,2 +1,3 @@
HEALTHCHECK_ENABLE=true
HEALTHCHECK_URL=https://my.selfhosted.healthcheck.com/ping/e48e4add-c17c-467c-9a91-7b245ad57fe8
FAIL_IF_TARGET_UNREACHABLE=true

34
debian/changelog vendored
View File

@@ -1,4 +1,36 @@
btrbk-cron (0.1) UNRELEASED; urgency=medium
btrbk-cron (0.4.2) stable; urgency=medium
* gather all logs
-- Paolo Asperti <paolo@asperti.com> Tue, 22 Oct 2024 11:21:43 +0200
btrbk-cron (0.4.1) stable; urgency=medium
* fix error redir
-- Paolo Asperti <paolo@asperti.com> Wed, 16 Oct 2024 11:56:39 +0200
btrbk-cron (0.4) stable; urgency=medium
* stale lockfile check
* require to run as root
-- Paolo Asperti <paolo@asperti.com> Tue, 15 Oct 2024 09:23:11 +0200
btrbk-cron (0.3) stable; urgency=medium
* clean exit if lockfile present
* added FAIL_IF_TARGET_UNREACHABLE setting
-- Paolo Asperti <paolo@asperti.com> Mon, 14 Oct 2024 17:33:58 +0200
btrbk-cron (0.2) stable; urgency=medium
* Added destination aliveness check
-- Paolo Asperti <paolo@asperti.com> Tue, 24 Sep 2024 16:08:04 +0200
btrbk-cron (0.1) stable; urgency=medium
* Initial release.