From 4dd17557a1f31446900c60aa5a2c96eb0fdea511 Mon Sep 17 00:00:00 2001 From: paspo Date: Wed, 18 Sep 2024 18:39:31 +0200 Subject: [PATCH] initial release --- README.md | 20 ++++++++++++++- btrbk-cron | 53 +++++++++++++++++++++++++++++++++++++++ btrbk-cron.conf.example | 2 ++ debian/.gitignore | 6 +++++ debian/btrbk-cron.cron.d | 1 + debian/btrbk-cron.example | 1 + debian/btrbk-cron.install | 1 + debian/changelog | 5 ++++ debian/compat | 1 + debian/control | 15 +++++++++++ debian/copyright | 27 ++++++++++++++++++++ debian/rules | 4 +++ debian/source/format | 1 + 13 files changed, 136 insertions(+), 1 deletion(-) create mode 100755 btrbk-cron create mode 100644 btrbk-cron.conf.example create mode 100644 debian/.gitignore create mode 100644 debian/btrbk-cron.cron.d create mode 100644 debian/btrbk-cron.example create mode 100644 debian/btrbk-cron.install create mode 100644 debian/changelog create mode 100644 debian/compat create mode 100644 debian/control create mode 100644 debian/copyright create mode 100755 debian/rules create mode 100644 debian/source/format diff --git a/README.md b/README.md index e04af66..56f9f66 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ # btrkbk-cron -btrbk cron wrapper \ No newline at end of file +btrbk cron wrapper with healthchecks support. + +[![Build Status](https://drone.asperti.com/api/badges/paspo/btrbk-cron/status.svg)](https://drone.asperti.com/paspo/btrbk-cron) + +## Build + +Run this: + +```bash +debuild -i -us -uc +``` + +## Installation + +Just install the package as usual. + +```bash +sudo dpkg -i btrbk-cron_*.deb +``` diff --git a/btrbk-cron b/btrbk-cron new file mode 100755 index 0000000..fd280f1 --- /dev/null +++ b/btrbk-cron @@ -0,0 +1,53 @@ +#!/bin/bash + +CONF=/etc/btrbk/btrbk-cron.conf +LOGFILE=$(mktemp) +trap 'rm -f -- "${LOGFILE}"' EXIT + +if [[ ! -f "${CONF}" ]] ; then + echo "Warning: Can't read config file (${CONF}), healthchecks disabled." +else + # shellcheck disable=SC1090,SC1091 + source "${CONF}" +fi + +HEALTHCHECK_URL=${HEALTHCHECK_URL:-http://127.0.0.1} +HEALTHCHECK_ENABLE=${HEALTHCHECK_ENABLE:-false} +CURLOPTS=(-fsS -m 10 --retry 5) + +if [[ ! -d /run/btrbk ]] ; then + mkdir -p /run/btrbk +fi + +echo "---" | tee -a "${LOGFILE}" +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 +ret=$? +if [[ "${ret}" -eq "0" ]]; then + if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then + curl "${CURLOPTS[@]}" "${HEALTHCHECK_URL}" + fi + echo + echo "healthcheck notified! (ok)" | tee -a "${LOGFILE}" +else + if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then + curl "${CURLOPTS[@]}" "${HEALTHCHECK_URL}/fail" + fi + echo + echo "healthcheck notified! (fail)" | tee -a "${LOGFILE}" +fi +echo "starting cleanup" | tee -a "${LOGFILE}" +(nice -n 19 /usr/bin/ionice -c idle /usr/bin/btrbk clean >> "${LOGFILE}" ; ) 2>&1 + +echo "backup finished, sending logs" | tee -a "${LOGFILE}" +if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then + curl "${CURLOPTS[@]}" --data-binary "@${LOGFILE}" "${HEALTHCHECK_URL}/log" +fi +echo +rm -f -- "${LOGFILE}" +echo "backup finished" diff --git a/btrbk-cron.conf.example b/btrbk-cron.conf.example new file mode 100644 index 0000000..268ed46 --- /dev/null +++ b/btrbk-cron.conf.example @@ -0,0 +1,2 @@ +HEALTHCHECK_ENABLE=true +HEALTHCHECK_URL=https://my.selfhosted.healthcheck.com/ping/e48e4add-c17c-467c-9a91-7b245ad57fe8 diff --git a/debian/.gitignore b/debian/.gitignore new file mode 100644 index 0000000..35f2bdb --- /dev/null +++ b/debian/.gitignore @@ -0,0 +1,6 @@ +debhelper-build-stamp +*.debhelper.log +*.substvars +files +.debhelper +btrbk-cron diff --git a/debian/btrbk-cron.cron.d b/debian/btrbk-cron.cron.d new file mode 100644 index 0000000..97c885b --- /dev/null +++ b/debian/btrbk-cron.cron.d @@ -0,0 +1 @@ +25 * * * * root /usr/bin/btrbk-cron diff --git a/debian/btrbk-cron.example b/debian/btrbk-cron.example new file mode 100644 index 0000000..3ae0429 --- /dev/null +++ b/debian/btrbk-cron.example @@ -0,0 +1 @@ +btrbk-cron.conf.example etc/btrbk/btrbk-cron.conf.example diff --git a/debian/btrbk-cron.install b/debian/btrbk-cron.install new file mode 100644 index 0000000..e650dc2 --- /dev/null +++ b/debian/btrbk-cron.install @@ -0,0 +1 @@ +btrbk-cron usr/bin diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..6d51ec1 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +btrbk-cron (0.1) UNRELEASED; urgency=medium + + * Initial release. + + -- Paolo Asperti Wed, 18 Sep 2024 09:14:20 +0200 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +12 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..02a7c3c --- /dev/null +++ b/debian/control @@ -0,0 +1,15 @@ +Source: btrbk-cron +Section: admin +Priority: optional +Maintainer: Paolo Asperti +Build-Depends: debhelper (>= 12) +Standards-Version: 4.5.0 +Homepage: https://asperti.com/ + +Package: btrbk-cron +Architecture: all +Depends: ${misc:Depends}, bash (>= 5), curl, btrbk +Description: btrbk wrapper script for cron and healthchecks + This packages automates btrbk and adds support for healthchecks notifications. + Healthchecks pings are sent at start and at success/failure. + Logs are also sent. diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..aa564b0 --- /dev/null +++ b/debian/copyright @@ -0,0 +1,27 @@ +Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: btrbk wrapper script for cron and healthchecks +Source: https://asperti.com + +Files: * +Copyright: Copyright 2024 Paolo Asperti +License: GPL-2+ + 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 2 of the License, or (at your option) any later + version. + . + This program is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied + warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the GNU General Public License for more + details. + . + You should have received a copy of the GNU General Public + License along with this package; if not, write to the Free + Software Foundation, Inc., 51 Franklin St, Fifth Floor, + Boston, MA 02110-1301 USA + . + On Debian systems, the full text of the GNU General Public + License version 2 can be found in the file + `/usr/share/common-licenses/GPL-2'. diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..2d33f6a --- /dev/null +++ b/debian/rules @@ -0,0 +1,4 @@ +#!/usr/bin/make -f + +%: + dh $@ diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..89ae9db --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (native)