Skip to content

Commit

Permalink
Allow multiple schedules in the same container (#78)
Browse files Browse the repository at this point in the history
* Allow mounting of config directory for multiple schedules

* Add docs for conf.d feature

* Fix behavior on multiple files

* Define default case first in entrypoint script
  • Loading branch information
m90 authored Mar 4, 2022
1 parent 6e1b855 commit c9ebb9e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 4 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ It handles __recurring or one-off backups of Docker volumes__ to a __local direc
- [Manually triggering a backup](#manually-triggering-a-backup)
- [Update deprecated email configuration](#update-deprecated-email-configuration)
- [Using a custom Docker host](#using-a-custom-docker-host)
- [Run multiple backup schedules in the same container](#run-multiple-backup-schedules-in-the-same-container)
- [Recipes](#recipes)
- [Backing up to AWS S3](#backing-up-to-aws-s3)
- [Backing up to Filebase](#backing-up-to-filebase)
Expand Down Expand Up @@ -653,6 +654,32 @@ DOCKER_HOST=tcp://docker_socket_proxy:2375

In case you are using a socket proxy, it must support `GET` and `POST` requests to the `/containers` endpoint. If you are using Docker Swarm, it must also support the `/services` endpoint. If you are using pre/post backup commands, it must also support the `/exec` endpoint.

### Run multiple backup schedules in the same container

Multiple backup schedules with different configuration can be configured by mounting an arbitrary number of configuration files (using the `.env` format) into `/etc/dockervolumebackup/conf.d`:

```
version: '3'
services:
# ... define other services using the `data` volume here
backup:
image: offen/docker-volume-backup:latest
volumes:
- data:/backup/my-app-backup:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./configuration:/etc/dockervolumebackup/conf.d

volumes:
data:
```
A separate cronjob will be created for each config file.
If a configuration value is set both in the global environment as well as in the config file, the config file will take precedence.
The `backup` command expects to run on an exclusive lock, so it is your responsibility to make sure the invocations do not overlap.
In case you need your schedules to overlap, you need to create a dedicated container for each schedule instead.
When changing the configuration, you currently need to manually restart the container for the changes to take effect.

## Recipes

This section lists configuration for some real-world use cases that you can mix and match according to your needs.
Expand Down
17 changes: 14 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@

set -e

BACKUP_CRON_EXPRESSION="${BACKUP_CRON_EXPRESSION:-@daily}"
if [ ! -d "/etc/dockervolumebackup/conf.d" ]; then
BACKUP_CRON_EXPRESSION="${BACKUP_CRON_EXPRESSION:-@daily}"

echo "Installing cron.d entry with expression $BACKUP_CRON_EXPRESSION."
echo "$BACKUP_CRON_EXPRESSION backup 2>&1" | crontab -
echo "Installing cron.d entry with expression $BACKUP_CRON_EXPRESSION."
echo "$BACKUP_CRON_EXPRESSION backup 2>&1" | crontab -
else
echo "/etc/dockervolumebackup/conf.d was found, using configuration files from this directory."

for file in /etc/dockervolumebackup/conf.d/*; do
source $file
BACKUP_CRON_EXPRESSION="${BACKUP_CRON_EXPRESSION:-@daily}"
echo "Appending cron.d entry with expression $BACKUP_CRON_EXPRESSION and configuration file $file"
(crontab -l; echo "$BACKUP_CRON_EXPRESSION /bin/sh -c 'set -a; source $file; set +a && backup' 2>&1") | crontab -
done
fi

echo "Starting cron in foreground."
crond -f -l 8
2 changes: 1 addition & 1 deletion test/compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
volumes:
- webdav_backup_data:/var/lib/dav

backup: &default_backup_service
backup:
image: offen/docker-volume-backup:${TEST_VERSION:-canary}
hostname: hostnametoken
depends_on:
Expand Down
1 change: 1 addition & 0 deletions test/confd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local
2 changes: 2 additions & 0 deletions test/confd/backup.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BACKUP_FILENAME="conf.tar.gz"
BACKUP_CRON_EXPRESSION="*/1 * * * *"
22 changes: 22 additions & 0 deletions test/confd/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'

services:
backup:
image: offen/docker-volume-backup:${TEST_VERSION:-canary}
restart: always
volumes:
- ./local:/archive
- app_data:/backup/app_data:ro
- ./backup.env:/etc/dockervolumebackup/conf.d/00backup.env
- ./never.env:/etc/dockervolumebackup/conf.d/10never.env
- /var/run/docker.sock:/var/run/docker.sock

offen:
image: offen/offen:latest
labels:
- docker-volume-backup.stop-during-backup=true
volumes:
- app_data:/var/opt/offen

volumes:
app_data:
2 changes: 2 additions & 0 deletions test/confd/never.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BACKUP_FILENAME="never.tar.gz"
BACKUP_CRON_EXPRESSION="0 0 5 31 2 ?"
26 changes: 26 additions & 0 deletions test/confd/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

set -e

cd $(dirname $0)

mkdir -p local

docker-compose up -d

# sleep until a backup is guaranteed to have happened on the 1 minute schedule
sleep 100

docker-compose down --volumes

if [ ! -f ./local/conf.tar.gz ]; then
echo "[TEST:FAIL] Config from file was not used."
exit 1
fi
echo "[TEST:PASS] Config from file was used."

if [ -f ./local/never.tar.gz ]; then
echo "[TEST:FAIL] Unexpected file was found."
exit 1
fi
echo "[TEST:PASS] Unexpected cron did not run."

0 comments on commit c9ebb9e

Please sign in to comment.