42 Commits

Author SHA1 Message Date
e360c3d8f8 remove unused traefik labels 2023-06-13 14:34:53 +02:00
50b317c12d fix shellcheck prevent globbing 2023-06-13 11:57:42 +02:00
9cb3a469f3 mount volume ro 2023-06-13 11:48:25 +02:00
24d2c0e85b Backup volumes from host instead of copying paths
* Backupbot will now copy all volumes from a service with
  backupbot.enabled = 'true' label from the /var/lib/docker/volumes/
  path directly. This reduces the resource overhead of copying
  stuff from one volume to another.
  Recipes need to be adjustet that db-dumps are saved into a volume
  now!
* Remove the Dockerfile and move stuff into a entrypoint. This
  simplifies the whole versioning thing and makes this "just"
  a recipe

Co-authored-by: Moritz < moritz.m@local-it.org>
2023-06-05 11:15:54 +02:00
27e2e61d7f chore(deps): update docker docker tag to v24.0.2 2023-05-29 07:03:02 +00:00
1bb1917e18 Merge pull request 'chore(deps): update docker docker tag to v24 (main)' (#14) from renovate/main-docker-24.x into main
Reviewed-on: coop-cloud/backup-bot-two#14
2023-05-28 14:23:14 +00:00
7b8b3b1acd chore(deps): update docker docker tag to v24 2023-05-22 07:06:36 +00:00
9c5ba87232 chore(deps): update docker docker tag to v23.0.6 2023-05-10 07:02:21 +00:00
9064bebb56 chore(deps): update docker docker tag to v23.0.5 2023-04-27 07:06:03 +00:00
4fdb585825 Merge pull request 'chore(deps): update docker docker tag to v23 (main)' (#11) from renovate/main-docker-23.x into main
Reviewed-on: coop-cloud/backup-bot-two#11
2023-04-24 08:17:36 +00:00
bde63b3f6f chore(deps): update docker docker tag to v23 2023-04-18 07:02:30 +00:00
92dfd23b26 feat: backupvolume can be pruned after upload 2023-03-01 13:29:00 +01:00
bab224ab96 chore(deps): update docker docker tag to v19.03.15 2023-01-19 08:03:49 +00:00
36928c34ac Merge pull request 'Configure Renovate' (#8) from renovate/configure into main
Reviewed-on: coop-cloud/backup-bot-two#8
2023-01-18 17:37:22 +00:00
9b324476c2 chore(deps): add renovate.json 2023-01-18 17:24:09 +00:00
7aa464e271 chore: publish 0.1.0+latest release 2022-10-11 15:28:50 +02:00
59c071734a add labels to get around abra checks 2022-10-11 15:24:35 +02:00
940b6bde1a Merge pull request 'backup multiple paths' (#5) from multi_path into main
Reviewed-on: coop-cloud/backup-bot-two#5
2021-12-14 14:44:05 +00:00
d6faffcbbd move rm up, to keep the latest backup in the volume 2021-12-13 11:37:52 +01:00
5a20ef4349 Merge branch 'main' into multi_path 2021-11-24 10:29:27 +00:00
ce42fb06fd fix docker cp paths 2021-11-24 11:17:13 +01:00
f2472bd0d3 make backup.path comma separated list 2021-11-23 17:38:31 +01:00
3wc
f7cbbf04c0 Goodbye, emojis! 😢
[ci skip]
2021-11-23 12:19:04 +02:00
3wc
ab03d2d7cc Ignore testing folder 2021-11-22 14:25:56 +02:00
3wc
32ba0041d1 chore: fix README bullet formatting
[ci skip]
2021-11-22 13:42:03 +02:00
3wc
394cc4f47c Mass README update
[ci skip]
2021-11-21 21:31:24 +02:00
53c4f1956a fix push docker image to correct destination 2021-11-16 12:14:21 +01:00
c750d6402f fix s3 restic_repo 2021-11-16 11:44:10 +01:00
b2e2fc9d13 fix typo 2021-11-16 11:43:47 +01:00
3wc
9e818ed021 Revert to previous, probably-working cron set-up 2021-11-11 12:05:04 +02:00
3wc
f6d1da8899 Working cron again, d'oh 2021-11-11 02:10:17 +02:00
3wc
d3e9001597 Add badge, tidy docs 2021-11-11 00:18:50 +02:00
3wc
f7db376377 Appease shellcheck 2021-11-11 00:02:44 +02:00
3wc
d6e90e04ba SSH_HOST_KEY_DISABLE, add drone pipline 2021-11-11 00:00:39 +02:00
3wc
a990dc27c7 SSH host keys, split out swarm-cronjob 2021-11-10 22:02:13 +02:00
3wc
721c393d2d Allow overriding cron schedule, fix vars 2021-11-10 21:17:12 +02:00
3wc
c9de239e93 Bash command line handling showdown 2021-11-09 15:25:43 +02:00
3wc
489ef570dd Fix AWS S3 settings 2021-11-09 14:30:19 +02:00
3wc
23b092776f More progress towards S3/SSH 2021-11-09 14:20:11 +02:00
3wc
ed76e6164b Work-in-progress: split S3 & SSH storage 2021-11-09 12:37:56 +02:00
3wc
f5e87f396a Update TODO list and tidy up README 2021-11-09 12:27:54 +02:00
3wc
8317f50a8a Variables, Dockerfile, better syntax, etc. 2021-11-06 19:45:39 +02:00
13 changed files with 321 additions and 71 deletions

12
.drone.yml Normal file
View File

@ -0,0 +1,12 @@
---
kind: pipeline
name: linters
steps:
- name: run shellcheck
image: koalaman/shellcheck-alpine
commands:
- shellcheck backup.sh
trigger:
branch:
- main

24
.env.sample Normal file
View File

@ -0,0 +1,24 @@
TYPE=backup-bot-two
SECRET_RESTIC_PASSWORD_VERSION=v1
COMPOSE_FILE=compose.yml
SERVER_NAME=example.com
RESTIC_HOST=minio.example.com
CRON_SCHEDULE='*/5 * * * *'
REMOVE_BACKUP_VOLUME_AFTER_UPLOAD=1
# swarm-cronjob, instead of built-in cron
#COMPOSE_FILE="$COMPOSE_FILE:compose.swarm-cronjob.yml"
# SSH storage
#SECRET_SSH_KEY_VERSION=v1
#SSH_HOST_KEY="hostname ssh-rsa AAAAB3...
#COMPOSE_FILE="$COMPOSE_FILE:compose.ssh.yml"
# S3 storage
#SECRET_AWS_SECRET_ACCESS_KEY_VERSION=v1
#AWS_ACCESS_KEY_ID=something-secret
#COMPOSE_FILE="$COMPOSE_FILE:compose.s3.yml"

15
.envrc.sample Normal file
View File

@ -0,0 +1,15 @@
export RESTIC_HOST="user@domain.tld"
export RESTIC_PASSWORD_FILE=/run/secrets/restic-password
export BACKUP_DEST=/backups
export SERVER_NAME=domain.tld
export DOCKER_CONTEXT=$SERVER_NAME
# uncomment either this:
#export SSH_KEY_FILE=~/.ssh/id_rsa
# or this:
#export AWS_SECRET_ACCESS_KEY_FILE=s3
#export AWS_ACCESS_KEY_ID=easter-october-emphatic-tug-urgent-customer
# optionally limit subset of services for testing
#export SERVICES_OVERRIDE="ghost_domain_tld_app ghost_domain_tld_db"

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/testing

View File

@ -1,45 +1,70 @@
# Backupbot II: This Time It's Easily Configurable
# Backupbot II
Automatically backup files from running Docker Swarm services based on labels.
[![Build Status](https://build.coopcloud.tech/api/badges/coop-cloud/backup-bot-two/status.svg)](https://build.coopcloud.tech/coop-cloud/backup-bot-two)
## TODO
_This Time, It's Easily Configurable_
- [ ] Make a Docker image of this
- [ ] Rip out or improve Restic stuff
- [ ] Add secret handling for database backups
- [ ] Continuous linting with shellcheck
Automatically take backups from all volumes of running Docker Swarm services and runs pre- and post commands.
## Label format
## Background
(Haven't done secrets yet, here are two options)
There are lots of Docker volume backup systems; all of them have one or both of these limitations:
- You need to define all the volumes to back up in the configuration system
- Backups require services to be stopped to take consistent copies
Backupbot II tries to help, by
1. **letting you define backups using Docker labels**, so you can **easily collect your backups for use with another system** like docker-volume-backup.
2. **running pre- and post-commands** before and after backups, for example to use database tools to take a backup from a running service.
## Deployment
### With Co-op Cloud
1. Set up Docker Swarm and [`abra`][abra]
2. `abra app new backup-bot-two`
3. `abra app config <your-app-name>`, and set storage options. Either configure `CRON_SCHEDULE`, or set up `swarm-cronjob`
4. `abra app secret generate <your-app-name> restic-password v1`, optionally with `--pass` before `<your-app-name>` to save the generated secret in `pass`.
5. `abra app secret insert <your-app-name> ssh-key v1 ...` or similar, to load required secrets.
4. `abra app deploy <your-app-name>`
<!-- metadata -->
* **Category**: Utilities
* **Status**: 0, work-in-progress
* **Image**: [`thecoopcloud/backup-bot-two`](https://hub.docker.com/r/thecoopcloud/backup-bot-two), 4, upstream
* **Healthcheck**: No
* **Backups**: N/A
* **Email**: N/A
* **Tests**: No
* **SSO**: N/A
<!-- endmetadata -->
## Configuration
Like Traefik, or `swarm-cronjob`, Backupbot II uses access to the Docker socket to read labels from running Docker Swarm services:
v1:
```
services:
db:
deploy:
labels:
backupbot.backup: "true"
backupbot.backup.repos: "$some_thing"
backupbot.backup.at: "* * * * *"
backupbot.backup.pre-hook: 'mysqldump -u root -p"$(cat /run/secrets/db_root_password)" -f /tmp/dump/dump.db'
backupbot.backup.post-hook: "rm -rf /tmp/dump/dump.db"
backupbot.backup.path: "/tmp/dump/"
```
v2:
```
deploy:
labels:
backupbot.backup: "true"
backupbot.backup.repos: "$some_thing"
backupbot.backup.at: "* * * * *"
backupbot.backup.post-hook: "rm -rf /tmp/dump/dump.db"
backupbot.backup.secrets": "db_root_password",
backupbot.backup.pre-hook: 'mysqldump -u root -p"$DB_ROOT_PASSWORD" -f /tmp/dump/dump.db'
backupbot.backup: ${BACKUP:-"true"}
backupbot.backup.pre-hook: 'mysqldump -u root -p"$(cat /run/secrets/db_root_password)" -f /volume_path/dump.db'
backupbot.backup.post-hook: "rm -rf /volume_path/dump.db"
```
## Questions:
- `backupbot.backup` -- set to `true` to back up this service (REQUIRED)
- `backupbot.backup.pre-hook` -- command to run before copying files (optional), save all dumps into the volumes
- `backupbot.backup.post-hook` -- command to run after copying files (optional)
- Should frequency be configurable per service, centrally, or both?
As in the above example, you can reference Docker Secrets, e.g. for looking up database passwords, by reading the files in `/run/secrets` directly.
```
- "backupbot.backup.at: "* * * * *"
```
## Development
1. Install `direnv`
2. `cp .envrc.sample .envrc`
3. Edit `.envrc` as appropriate, including setting `DOCKER_CONTEXT` to a remote Docker context, if you're not running a swarm server locally.
4. Run `./backup.sh` -- you can add the `--skip-backup` or `--skip-upload` options if you just want to test one other step
[abra]: https://git.autonomic.zone/autonomic-cooperative/abra

2
abra.sh Normal file
View File

@ -0,0 +1,2 @@
export ENTRYPOINT_VERSION=v1
export BACKUP_VERSION=v1

147
backup.sh
View File

@ -1,50 +1,119 @@
#!/bin/bash
# FIXME: just for testing
backup_path=backups
set -e
# FIXME: just for testing
export DOCKER_CONTEXT=demo.coopcloud.tech
server_name="${SERVER_NAME:?SERVER_NAME not set}"
mapfile -t services < <(docker service ls --format '{{ .Name }}')
restic_password_file="${RESTIC_PASSWORD_FILE:?RESTIC_PASSWORD_FILE not set}"
# FIXME: just for testing
services=( "ghost_demo_app" "ghost_demo_db" )
restic_host="${RESTIC_HOST:?RESTIC_HOST not set}"
for service in "${services[@]}"; do
echo "service: $service"
details=$(docker service inspect "$service" --format "{{ json .Spec.Labels }}")
if echo "$details" | jq -r '.["backupbot.backup"]' | grep -q 'true'; then
pre=$(echo "$details" | jq -r '.["backupbot.backup.pre-hook"]')
post=$(echo "$details" | jq -r '.["backupbot.backup.post-hook"]')
path=$(echo "$details" | jq -r '.["backupbot.backup.path"]')
if [ "$path" = "null" ]; then
echo "ERROR: missing 'path' for $service"
continue # or maybe exit?
fi
backup_paths=()
container=$(docker container ls -f "name=$service" --format '{{ .ID }}')
echo "backing up $service"
test -d "$backup_path/$service" || mkdir "$backup_path/$service"
if [ "$pre" != "null" ]; then
# run the precommand
# shellcheck disable=SC2086
docker exec "$container" $pre
fi
# shellcheck disable=SC2153
ssh_key_file="${SSH_KEY_FILE}"
s3_key_file="${AWS_SECRET_ACCESS_KEY_FILE}"
# run the backup
docker cp "$container:$path" "$backup_path/$service"
restic_repo=
restic_extra_options=
if [ "$post" != "null" ]; then
# run the postcommand
# shellcheck disable=SC2086
docker exec "$container" $post
fi
if [ -n "$ssh_key_file" ] && [ -f "$ssh_key_file" ]; then
restic_repo="sftp:$restic_host:/$server_name"
# Only check server against provided SSH_HOST_KEY, if set
if [ -n "$SSH_HOST_KEY" ]; then
tmpfile=$(mktemp)
echo "$SSH_HOST_KEY" >>"$tmpfile"
echo "using host key $SSH_HOST_KEY"
ssh_options="-o 'UserKnownHostsFile $tmpfile'"
elif [ "$SSH_HOST_KEY_DISABLE" = "1" ]; then
echo "disabling SSH host key checking"
ssh_options="-o 'StrictHostKeyChecking=No'"
else
echo "neither SSH_HOST_KEY nor SSH_HOST_KEY_DISABLE set"
fi
restic -p restic-password \
backup --quiet -r sftp:u272979@u272979.your-storagebox.de:/demo.coopcloud.tech \
--tag coop-cloud "$backup_path"
restic_extra_options="sftp.command=ssh $ssh_options -i $ssh_key_file $restic_host -s sftp"
fi
if [ -n "$s3_key_file" ] && [ -f "$s3_key_file" ] && [ -n "$AWS_ACCESS_KEY_ID" ]; then
AWS_SECRET_ACCESS_KEY="$(cat "${s3_key_file}")"
export AWS_SECRET_ACCESS_KEY
restic_repo="s3:$restic_host:/$server_name"
fi
if [ -z "$restic_repo" ]; then
echo "you must configure either SFTP or S3 storage, see README"
exit 1
fi
echo "restic_repo: $restic_repo"
# Pre-bake-in some default restic options
_restic() {
if [ -z "$restic_extra_options" ]; then
# shellcheck disable=SC2068
restic -p "$restic_password_file" \
--quiet -r "$restic_repo" \
$@
else
# shellcheck disable=SC2068
restic -p "$restic_password_file" \
--quiet -r "$restic_repo" \
-o "$restic_extra_options" \
$@
fi
}
if [ -n "$SERVICES_OVERRIDE" ]; then
# this is fine because docker service names should never include spaces or
# glob characters
# shellcheck disable=SC2206
services=($SERVICES_OVERRIDE)
else
mapfile -t services < <(docker service ls --format '{{ .Name }}')
fi
post_commands=()
if [[ \ $*\ != *\ --skip-backup\ * ]]; then
for service in "${services[@]}"; do
echo "service: $service"
details=$(docker service inspect "$service" --format "{{ json .Spec.Labels }}")
if echo "$details" | jq -r '.["backupbot.backup"]' | grep -q 'true'; then
pre=$(echo "$details" | jq -r '.["backupbot.backup.pre-hook"]')
post=$(echo "$details" | jq -r '.["backupbot.backup.post-hook"]')
container=$(docker container ls -f "name=$service" --format '{{ .ID }}')
stack_name=$(echo "$details" | jq -r '.["com.docker.stack.namespace"]')
if [ "$pre" != "null" ]; then
# run the precommand
echo "executing precommand $pre in container $container"
docker exec "$container" sh -c "$pre"
fi
if [ "$post" != "null" ]; then
# append post command
post_commands+=("docker exec $container sh -c \"$post\"")
fi
# add volume paths to backup path
backup_paths+=(/var/lib/docker/volumes/"${stack_name}"_*)
fi
done
# check if restic repo exists, initialise if not
if [ -z "$(_restic cat config)" ] 2>/dev/null; then
echo "initializing restic repo"
_restic init
fi
fi
if [[ \ $*\ != *\ --skip-upload\ * ]]; then
echo "${backup_paths[@]}"
_restic backup --host "$server_name" --tag coop-cloud "${backup_paths[@]}"
fi
# run post commands
for post in "${post_commands[@]}"; do
echo "executing postcommand $post"
eval "$post"
done

14
compose.s3.yml Normal file
View File

@ -0,0 +1,14 @@
---
version: "3.8"
services:
app:
environment:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY_FILE=/run/secrets/aws_secret_access_key
secrets:
- aws_secret_access_key
secrets:
aws_secret_access_key:
external: true
name: ${STACK_NAME}_aws_secret_access_key_${SECRET_AWS_SECRET_ACCESS_KEY_VERSION}

16
compose.ssh.yml Normal file
View File

@ -0,0 +1,16 @@
---
version: "3.8"
services:
app:
environment:
- SSH_KEY_FILE=/run/secrets/ssh_key
- SSH_HOST_KEY
- SSH_HOST_KEY_DISABLE
secrets:
- source: ssh_key
mode: 0400
secrets:
ssh_key:
external: true
name: ${STACK_NAME}_ssh_key_${SECRET_SSH_KEY_VERSION}

15
compose.swarm-cronjob.yml Normal file
View File

@ -0,0 +1,15 @@
---
version: "3.8"
services:
app:
deploy:
mode: replicated
replicas: 0
labels:
- "swarm.cronjob.enable=true"
# Note(3wc): every 5m, testing
- "swarm.cronjob.schedule=*/5 * * * *"
# Note(3wc): blank label to be picked up by `abra recipe sync`
restart_policy:
condition: none
entrypoint: [ "/usr/bin/backup.sh" ]

42
compose.yml Normal file
View File

@ -0,0 +1,42 @@
---
version: "3.8"
services:
app:
image: docker:24.0.2-dind
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "/var/lib/docker/volumes/:/var/lib/docker/volumes/:ro"
environment:
- CRON_SCHEDULE
- RESTIC_REPO
- RESTIC_PASSWORD_FILE=/run/secrets/restic_password
- BACKUP_DEST=/backups
- RESTIC_HOST
- SERVER_NAME
- REMOVE_BACKUP_VOLUME_AFTER_UPLOAD=1
secrets:
- restic_password
deploy:
labels:
- coop-cloud.${STACK_NAME}.version=0.1.0+latest
configs:
- source: entrypoint
target: /entrypoint.sh
mode: 0555
- source: backup
target: /backup.sh
mode: 0555
entrypoint: ['/entrypoint.sh']
secrets:
restic_password:
external: true
name: ${STACK_NAME}_restic_password_${SECRET_RESTIC_PASSWORD_VERSION}
configs:
entrypoint:
name: ${STACK_NAME}_entrypoint_${ENTRYPOINT_VERSION}
file: entrypoint.sh
backup:
name: ${STACK_NAME}_backup_${BACKUP_VERSION}
file: backup.sh

12
entrypoint.sh Normal file
View File

@ -0,0 +1,12 @@
#!/bin/sh
set -e
apk add --upgrade --no-cache bash curl jq restic
cron_schedule="${CRON_SCHEDULE:?CRON_SCHEDULE not set}"
echo "$cron_schedule /backup.sh" | crontab -
crontab -l
crond -f -d8 -L /dev/stdout

3
renovate.json Normal file
View File

@ -0,0 +1,3 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
}