9 Commits

Author SHA1 Message Date
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
3f4c9fc6f4 drone 2024-09-18 18:45:47 +02:00
4dd17557a1 initial release 2024-09-18 18:39:31 +02:00
15 changed files with 257 additions and 4 deletions

44
.drone.yml Normal file
View File

@@ -0,0 +1,44 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: build
image: docker.asperti.com/paspo/docker-deb-builder:debian_bullseye
commands:
- debuild -i -us -uc
- mkdir debs
- mv ../*.deb debs/
- name: gitea release
image: plugins/gitea-release
settings:
checksum:
- md5
- sha256
api_key:
from_secret: gitea_token
base_url: https://git.asperti.com
files:
- debs/*.deb
title: server01-crowdsec-whitelist
- name: push to repo
image: docker.asperti.com/paspo/docker-aptly-pusher
settings:
api_url: https://deb.server01.it/api
repo: server01
distribution: server01
http_user:
from_secret: repo_http_user
http_pass:
from_secret: repo_http_pass
passphrase:
from_secret: repo_passphrase
files:
- debs/*.deb
trigger:
event:
- tag

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,3 +1,36 @@
# btrkbk-cron
# btrbk-cron
btrbk cron wrapper
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
```
## 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.
```bash
sudo dpkg -i btrbk-cron_*.deb
```

98
btrbk-cron Executable file
View File

@@ -0,0 +1,98 @@
#!/bin/bash
CONF=/etc/btrbk/btrbk-cron.conf
LOCKFILE=/run/btrbk/btrbk.lock
LOGFILE=$(mktemp)
trap 'rm -f -- "${LOGFILE}"' EXIT
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." | tee -a "${LOGFILE}"
else
# shellcheck disable=SC1090,SC1091
source "${CONF}"
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
if [[ "${HEALTHCHECK_ENABLE}" = "true" ]] ; then
echo "Info: Healthchecks enabled with URL: ${HEALTHCHECK_URL}"
else
echo "Info: Healthchecks disabled"
fi
if [[ -f "${LOCKFILE}" ]] ; then
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
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
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"

3
btrbk-cron.conf.example Normal file
View File

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

6
debian/.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
debhelper-build-stamp
*.debhelper.log
*.substvars
files
.debhelper
btrbk-cron

1
debian/btrbk-cron.cron.d vendored Normal file
View File

@@ -0,0 +1 @@
25 * * * * root /usr/bin/btrbk-cron

1
debian/btrbk-cron.example vendored Normal file
View File

@@ -0,0 +1 @@
btrbk-cron.conf.example etc/btrbk/btrbk-cron.conf.example

1
debian/btrbk-cron.install vendored Normal file
View File

@@ -0,0 +1 @@
btrbk-cron usr/bin

18
debian/changelog vendored Normal file
View File

@@ -0,0 +1,18 @@
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.
-- Paolo Asperti <paolo@asperti.com> Wed, 18 Sep 2024 09:14:20 +0200

1
debian/compat vendored Normal file
View File

@@ -0,0 +1 @@
12

15
debian/control vendored Normal file
View File

@@ -0,0 +1,15 @@
Source: btrbk-cron
Section: admin
Priority: optional
Maintainer: Paolo Asperti <paolo@asperti.com>
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.

27
debian/copyright vendored Normal file
View File

@@ -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 <paolo@asperti.com>
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'.

4
debian/rules vendored Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/make -f
%:
dh $@

1
debian/source/format vendored Normal file
View File

@@ -0,0 +1 @@
3.0 (native)