Compare commits
11 Commits
prom-mon
...
feature/do
Author | SHA1 | Date | |
---|---|---|---|
2a7e564a24 | |||
5f381f395d | |||
e0ee16426b | |||
92845c4142 | |||
d0d0f29c79 | |||
88168de90e | |||
71c88d0428 | |||
15b2d656bb | |||
46522a2e9a | |||
30e88a972a | |||
64e09a6472 |
@ -9,16 +9,10 @@ RESTIC_REPOSITORY=/backups/restic
|
|||||||
CRON_SCHEDULE='30 3 * * *'
|
CRON_SCHEDULE='30 3 * * *'
|
||||||
|
|
||||||
# Push Notifiactions
|
# Push Notifiactions
|
||||||
#PUSH_PROMETHEUS_URL=https://pushgateway.example.com/metrics/job/backup
|
|
||||||
# or
|
|
||||||
#PUSH_URL_START=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=start
|
#PUSH_URL_START=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=start
|
||||||
#PUSH_URL_SUCCESS=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=OK
|
#PUSH_URL_SUCCESS=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=OK
|
||||||
#PUSH_URL_FAIL=https://status.example.com/api/push/xxxxxxxxxx?status=down&msg=fail
|
#PUSH_URL_FAIL=https://status.example.com/api/push/xxxxxxxxxx?status=down&msg=fail
|
||||||
|
|
||||||
# Push Basic Auth
|
|
||||||
#COMPOSE_FILE="$COMPOSE_FILE:compose.pushbasicauth.yml"
|
|
||||||
#SECRET_PUSH_BASICAUTH=v1
|
|
||||||
|
|
||||||
# swarm-cronjob, instead of built-in cron
|
# swarm-cronjob, instead of built-in cron
|
||||||
#COMPOSE_FILE="$COMPOSE_FILE:compose.swarm-cronjob.yml"
|
#COMPOSE_FILE="$COMPOSE_FILE:compose.swarm-cronjob.yml"
|
||||||
|
|
||||||
|
27
README.md
27
README.md
@ -10,7 +10,7 @@ Automatically take backups from all volumes of running Docker Swarm services and
|
|||||||
|
|
||||||
* **Category**: Utilities
|
* **Category**: Utilities
|
||||||
* **Status**: 0, work-in-progress
|
* **Status**: 0, work-in-progress
|
||||||
* **Image**: [`git.coopcloud.tech/coop-cloud/backup-bot-two`](https://git.coopcloud.tech/coop-cloud/-/packages/container/backup-bot-two), 4, upstream
|
* **Image**: [`thecoopcloud/backup-bot-two`](https://hub.docker.com/r/thecoopcloud/backup-bot-two), 4, upstream
|
||||||
* **Healthcheck**: No
|
* **Healthcheck**: No
|
||||||
* **Backups**: N/A
|
* **Backups**: N/A
|
||||||
* **Email**: N/A
|
* **Email**: N/A
|
||||||
@ -104,38 +104,15 @@ See [restic REST docs](https://restic.readthedocs.io/en/latest/030_preparing_a_n
|
|||||||
|
|
||||||
## Push notifications
|
## Push notifications
|
||||||
|
|
||||||
It is possible to configure three push events, that may trigger on the backup cronjob. Those can be used to detect failures from mointoring systems.
|
|
||||||
The events are:
|
|
||||||
- start
|
|
||||||
- success
|
|
||||||
- fail
|
|
||||||
|
|
||||||
### Using a Prometheus Push Gateway
|
|
||||||
|
|
||||||
[A prometheus push gateway](https://git.coopcloud.tech/coop-cloud/monitoring-ng#setup-push-gateway) can be used by setting the following env variables:
|
|
||||||
- `PUSH_PROMETHEUS_URL=pushgateway.example.com/metrics/job/backup`
|
|
||||||
|
|
||||||
### Using custom URLs
|
|
||||||
|
|
||||||
The following env variables can be used to setup push notifications for backups. `PUSH_URL_START` is requested just before the backups starts, `PUSH_URL_SUCCESS` is only requested if the backup was successful and if the backup fails `PUSH_URL_FAIL` will be requested.
|
The following env variables can be used to setup push notifications for backups. `PUSH_URL_START` is requested just before the backups starts, `PUSH_URL_SUCCESS` is only requested if the backup was successful and if the backup fails `PUSH_URL_FAIL` will be requested.
|
||||||
Each variable is optional and independent of the other.
|
Each variable is optional and independent of the other.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
PUSH_URL_START=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=start
|
PUSH_URL_START=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=start
|
||||||
PUSH_URL_SUCCESS=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=OK
|
PUSH_URL_SUCCESS=https://status.example.com/api/push/xxxxxxxxxx?status=up&msg=OK
|
||||||
PUSH_URL_FAIL=https://status.example.com/api/push/xxxxxxxxxx?status=down&msg=fail
|
PUSH_URL_FAIL=https://status.example.com/api/push/xxxxxxxxxx?status=down&msg=fail
|
||||||
```
|
```
|
||||||
|
|
||||||
### Push endpoint behind basic auth
|
|
||||||
|
|
||||||
Insert the basic auth secret
|
|
||||||
`abra app secret insert <backupbot_name> push_basicauth v1 "user:password"`
|
|
||||||
|
|
||||||
Enable basic auth in the env file, by uncommenting the following line:
|
|
||||||
```
|
|
||||||
#COMPOSE_FILE="$COMPOSE_FILE:compose.pushbasicauth.yml"
|
|
||||||
#SECRET_PUSH_BASICAUTH=v1
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
2
abra.sh
2
abra.sh
@ -1,7 +1,5 @@
|
|||||||
export BACKUPBOT_VERSION=v1
|
export BACKUPBOT_VERSION=v1
|
||||||
export SSH_CONFIG_VERSION=v1
|
export SSH_CONFIG_VERSION=v1
|
||||||
export ENTRYPOINT_VERSION=v17
|
|
||||||
export CRONJOB_VERSION=v2
|
|
||||||
|
|
||||||
run_cron () {
|
run_cron () {
|
||||||
schedule="$(crontab -l | tr -s " " | cut -d ' ' -f-5)"
|
schedule="$(crontab -l | tr -s " " | cut -d ' ' -f-5)"
|
||||||
|
23
backupbot.py
23
backupbot.py
@ -42,7 +42,7 @@ sys.excepthook = handle_exception
|
|||||||
@click.option('-l', '--log', 'loglevel')
|
@click.option('-l', '--log', 'loglevel')
|
||||||
@click.option('-m', '--machine-logs', 'machine_logs', is_flag=True)
|
@click.option('-m', '--machine-logs', 'machine_logs', is_flag=True)
|
||||||
@click.option('service', '--host', '-h', envvar='SERVICE')
|
@click.option('service', '--host', '-h', envvar='SERVICE')
|
||||||
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY')
|
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY', required=True)
|
||||||
def cli(loglevel, service, repository, machine_logs):
|
def cli(loglevel, service, repository, machine_logs):
|
||||||
global SERVICE
|
global SERVICE
|
||||||
if service:
|
if service:
|
||||||
@ -54,25 +54,21 @@ def cli(loglevel, service, repository, machine_logs):
|
|||||||
if not isinstance(numeric_level, int):
|
if not isinstance(numeric_level, int):
|
||||||
raise ValueError('Invalid log level: %s' % loglevel)
|
raise ValueError('Invalid log level: %s' % loglevel)
|
||||||
logger.setLevel(numeric_level)
|
logger.setLevel(numeric_level)
|
||||||
logHandler = logging.StreamHandler()
|
|
||||||
if machine_logs:
|
if machine_logs:
|
||||||
|
logHandler = logging.StreamHandler()
|
||||||
formatter = jsonlogger.JsonFormatter(
|
formatter = jsonlogger.JsonFormatter(
|
||||||
"%(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", rename_fields={"levelname": "message_type"})
|
"%(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", rename_fields={"levelname": "message_type"})
|
||||||
logHandler.setFormatter(formatter)
|
logHandler.setFormatter(formatter)
|
||||||
logger.addHandler(logHandler)
|
logger.addHandler(logHandler)
|
||||||
|
|
||||||
export_secrets()
|
export_secrets()
|
||||||
init_repo()
|
init_repo()
|
||||||
|
|
||||||
|
|
||||||
def init_repo():
|
def init_repo():
|
||||||
if repo:= os.environ.get('RESTIC_REPOSITORY_FILE'):
|
repo = os.environ['RESTIC_REPOSITORY']
|
||||||
# RESTIC_REPOSITORY_FILE and RESTIC_REPOSITORY are mutually exclusive
|
|
||||||
del os.environ['RESTIC_REPOSITORY']
|
|
||||||
else:
|
|
||||||
repo = os.environ['RESTIC_REPOSITORY']
|
|
||||||
restic.repository = repo
|
|
||||||
logger.debug(f"set restic repository location: {repo}")
|
logger.debug(f"set restic repository location: {repo}")
|
||||||
|
restic.repository = repo
|
||||||
restic.password_file = '/var/run/secrets/restic_password'
|
restic.password_file = '/var/run/secrets/restic_password'
|
||||||
try:
|
try:
|
||||||
restic.cat.config()
|
restic.cat.config()
|
||||||
@ -108,7 +104,7 @@ def create(retries):
|
|||||||
def get_backup_cmds():
|
def get_backup_cmds():
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container_by_service = {
|
container_by_service = {
|
||||||
c.labels.get('com.docker.swarm.service.name'): c for c in client.containers.list()}
|
c.labels['com.docker.swarm.service.name']: c for c in client.containers.list()}
|
||||||
backup_paths = set()
|
backup_paths = set()
|
||||||
backup_apps = set()
|
backup_apps = set()
|
||||||
pre_commands = {}
|
pre_commands = {}
|
||||||
@ -143,14 +139,14 @@ def copy_secrets(apps):
|
|||||||
os.mkdir(SECRET_PATH)
|
os.mkdir(SECRET_PATH)
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container_by_service = {
|
container_by_service = {
|
||||||
c.labels.get('com.docker.swarm.service.name'): c for c in client.containers.list()}
|
c.labels['com.docker.swarm.service.name']: c for c in client.containers.list()}
|
||||||
services = client.services.list()
|
services = client.services.list()
|
||||||
for s in services:
|
for s in services:
|
||||||
app_name = s.attrs['Spec']['Labels']['com.docker.stack.namespace']
|
app_name = s.attrs['Spec']['Labels']['com.docker.stack.namespace']
|
||||||
if (app_name in apps and
|
if (app_name in apps and
|
||||||
(app_secs := s.attrs['Spec']['TaskTemplate']['ContainerSpec'].get('Secrets'))):
|
(app_secs := s.attrs['Spec']['TaskTemplate']['ContainerSpec'].get('Secrets'))):
|
||||||
if not container_by_service.get(s.name):
|
if not container_by_service.get(s.name):
|
||||||
logger.warning(
|
logger.error(
|
||||||
f"Container {s.name} is not running, secrets can not be copied.")
|
f"Container {s.name} is not running, secrets can not be copied.")
|
||||||
continue
|
continue
|
||||||
container_id = container_by_service[s.name].id
|
container_id = container_by_service[s.name].id
|
||||||
@ -161,7 +157,6 @@ def copy_secrets(apps):
|
|||||||
f"For the secret {sec['SecretName']} the file {src} does not exist for {s.name}")
|
f"For the secret {sec['SecretName']} the file {src} does not exist for {s.name}")
|
||||||
continue
|
continue
|
||||||
dst = SECRET_PATH + sec['SecretName']
|
dst = SECRET_PATH + sec['SecretName']
|
||||||
logger.debug("Copy Secret {sec['SecretName']}")
|
|
||||||
copyfile(src, dst)
|
copyfile(src, dst)
|
||||||
|
|
||||||
|
|
||||||
@ -189,8 +184,6 @@ def run_commands(commands):
|
|||||||
def backup_volumes(backup_paths, apps, retries, dry_run=False):
|
def backup_volumes(backup_paths, apps, retries, dry_run=False):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
logger.info("Start volume backup")
|
|
||||||
logger.debug(backup_paths)
|
|
||||||
result = restic.backup(backup_paths, dry_run=dry_run, tags=apps)
|
result = restic.backup(backup_paths, dry_run=dry_run, tags=apps)
|
||||||
logger.summary("backup finished", extra=result)
|
logger.summary("backup finished", extra=result)
|
||||||
return
|
return
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
---
|
|
||||||
version: "3.8"
|
|
||||||
services:
|
|
||||||
app:
|
|
||||||
secrets:
|
|
||||||
- push_basicauth
|
|
||||||
|
|
||||||
secrets:
|
|
||||||
push_basicauth:
|
|
||||||
external: true
|
|
||||||
name: ${STACK_NAME}_push_basicauth_${SECRET_PUSH_BASICAUTH}
|
|
17
compose.yml
17
compose.yml
@ -14,13 +14,6 @@ services:
|
|||||||
- RESTIC_PASSWORD_FILE=/run/secrets/restic_password
|
- RESTIC_PASSWORD_FILE=/run/secrets/restic_password
|
||||||
secrets:
|
secrets:
|
||||||
- restic_password
|
- restic_password
|
||||||
configs:
|
|
||||||
- source: entrypoint
|
|
||||||
target: /entrypoint.sh
|
|
||||||
mode: 666
|
|
||||||
- source: cronjob
|
|
||||||
target: /cronjob.sh
|
|
||||||
mode: 666
|
|
||||||
deploy:
|
deploy:
|
||||||
labels:
|
labels:
|
||||||
- coop-cloud.${STACK_NAME}.version=0.1.0+latest
|
- coop-cloud.${STACK_NAME}.version=0.1.0+latest
|
||||||
@ -38,14 +31,6 @@ secrets:
|
|||||||
restic_password:
|
restic_password:
|
||||||
external: true
|
external: true
|
||||||
name: ${STACK_NAME}_restic_password_${SECRET_RESTIC_PASSWORD_VERSION}
|
name: ${STACK_NAME}_restic_password_${SECRET_RESTIC_PASSWORD_VERSION}
|
||||||
|
|
||||||
configs:
|
|
||||||
entrypoint:
|
|
||||||
name: ${STACK_NAME}_entrypoint_${ENTRYPOINT_VERSION}
|
|
||||||
file: entrypoint.sh
|
|
||||||
cronjob:
|
|
||||||
name: ${STACK_NAME}_cronjob_${CRONJOB_VERSION}
|
|
||||||
file: cronjob.sh
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
backups:
|
backups:
|
||||||
|
40
cronjob.sh
40
cronjob.sh
@ -1,40 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
CURL_OPTS="-s"
|
|
||||||
# Check for basic auth
|
|
||||||
if [ -n "$(cat /run/secrets/push_basicauth)" ]
|
|
||||||
then
|
|
||||||
CURL_OPTS="$CURL_OPTS -u $(cat /run/secrets/push_basicauth)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$PUSH_PROMETHEUS_URL" ]
|
|
||||||
then
|
|
||||||
push_start_notification="(echo 'backup 1' | curl $CURL_OPTS --data-binary @- $PUSH_PROMETHEUS_URL)"
|
|
||||||
push_success_notification="(echo 'backup 0' | curl $CURL_OPTS --data-binary @- $PUSH_PROMETHEUS_URL)"
|
|
||||||
push_fail_notification="(echo 'backup -1' | curl $CURL_OPTS --data-binary @- $PUSH_PROMETHEUS_URL)"
|
|
||||||
else
|
|
||||||
if [ -n "$PUSH_URL_START" ]
|
|
||||||
then
|
|
||||||
push_start_notification="curl $CURL_OPTS '$PUSH_URL_START'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$PUSH_URL_FAIL" ]
|
|
||||||
then
|
|
||||||
push_fail_notification="curl $CURL_OPTS '$PUSH_URL_FAIL'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$PUSH_URL_SUCCESS" ]
|
|
||||||
then
|
|
||||||
push_success_notification="curl $CURL_OPTS '$PUSH_URL_SUCCESS'"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
eval "$push_start_notification"
|
|
||||||
if [ "$(backup --machine-logs create 2>&1 | tee /tmp/backup.log && (grep -q 'backup finished' /tmp/backup.log))" ]
|
|
||||||
then
|
|
||||||
eval "$push_success_notification"
|
|
||||||
else
|
|
||||||
eval "$push_fail_notification"
|
|
||||||
fi
|
|
@ -9,7 +9,22 @@ fi
|
|||||||
|
|
||||||
cron_schedule="${CRON_SCHEDULE:?CRON_SCHEDULE not set}"
|
cron_schedule="${CRON_SCHEDULE:?CRON_SCHEDULE not set}"
|
||||||
|
|
||||||
echo "$cron_schedule /cronjob.sh" | crontab -
|
if [ -n "$PUSH_URL_START" ]
|
||||||
|
then
|
||||||
|
push_start_notification="curl -s '$PUSH_URL_START' &&"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$PUSH_URL_FAIL" ]
|
||||||
|
then
|
||||||
|
push_fail_notification="|| curl -s '$PUSH_URL_FAIL'"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$PUSH_URL_SUCCESS" ]
|
||||||
|
then
|
||||||
|
push_notification=" && (grep -q 'backup finished' /tmp/backup.log && curl -s '$PUSH_URL_SUCCESS' $push_fail_notification)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$cron_schedule $push_start_notification backup --machine-logs create 2>&1 | tee /tmp/backup.log $push_notification" | crontab -
|
||||||
crontab -l
|
crontab -l
|
||||||
|
|
||||||
crond -f -d8 -L /dev/stdout
|
crond -f -d8 -L /dev/stdout
|
||||||
|
Reference in New Issue
Block a user