Home

Awesome

Borgmatic Container with S6

Docker Pulls Docker Image Size (tag) Build Status GitHub last commit User Guide

"Buy Me A Coffee"

Description

Multiarch fork of borgmatic-collective/borgmatic with latest software versions and leveraging S6 so you can run Docker commands.

There are also tags with Docker-CLI installed. Useful for container stop/start scripts.

Dockerhub

modem7/borgmatic-docker

Github

modem7/borgmatic-docker

Readme

A little container based on b3vis's work to automate my Borgbackups using the excellent Borgmatic.

It uses cron to run the backups at a time you can configure in data/borgmatic.d/crontab.txt.

Tags

TagDescription
LatestLatest version of Borgmatic
1.8.xx-1.2.xSpecific versions of Borgmatic

Environment Variables

VariableDescriptionPossible ValuesDefault
DOCKERCLIInstall DockerCLI and ComposeCLItrueEmpty
EXTRA_PKGSInstall additional packagesrclone coreutils jq other_packagesEmpty
CRONCron timescron time, false0 1 * * *
CRON_COMMANDCommand cron will runborgmatic --stats -v 0 2>&1borgmatic --stats -v 0 2>&1
EXTRA_CRONExtra cron lines0 5 2 * * command1Empty

Customising the container

Simply mount a volume located at /custom-cont-init.d and add any scripts you want. These scripts can contain logic for installing packages, copying over custom files to other locations, or installing plugins.

For example:

services:
  borgmatic:
    volumes:
      - /home/foo/my-custom-files:/custom-cont-init.d:ro

Usage

Please look at this guide to help you run this container.

To set your backup timing and configuration, you will need to create crontab.txt and your borgmatic config.yaml and mount these files into the /etc/borgmatic.d/ directory. When the container starts it creates the crontab from crontab.txt and starts crond. By cloning this repo in /opt/docker/, you will have a working setup to get started.

If using remote repositories mount your .ssh to /root/.ssh within the container.

Starting and stopping containers from hooks

In case you are using the container to backup docker volumes used by other containers, you might want to make sure that the data is consistent and doesn't change while the backup is running. The easiest way to ensure this is to stop the affected containers before the backup and restart them afterwards. You can use the appropriate borgmatic hooks and control the docker engine through the API using the hosts docker socket.

Please note that you might want to prefer the *_everything hooks to the *_backup hooks, as after_backup will not run if the backup fails for any reason (missing disk space, etc.) and therefore the containers stay stopped.

First mount the docker socket from the host by adding -v /var/run/docker.sock:/var/run/docker.sock to your run command or in the volume list of your docker-compose.yml.

Then use the following example to create the start/stop hooks in the config.yml for the containers that you want to control.

hooks:
    before_everything:
        - echo "Stopping containers..."
        - 'echo -ne "POST /v1.41/containers/<container1-name>/stop HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc local:/var/run/docker.sock 80 > /dev/null && echo "Stopped Container 1" || echo "Failed to stop Container 1"'
        - 'echo -ne "POST /v1.41/containers/<container2-name>/stop HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc local:/var/run/docker.sock 80 > /dev/null && echo "Stopped Container 2" || echo "Failed to stop Container 2"'
        - echo "Containers stopped."
        - echo "Starting a backup."

    after_everything:
        - echo "Finished a backup."
        - echo "Restarting containers..."
        - 'echo -ne "POST /v1.41/containers/<container1-name>/start HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc local:/var/run/docker.sock 80 > /dev/null && echo "Started Container 1" || echo "Failed to start Container 1"'
        - 'echo -ne "POST /v1.41/containers/<container2-name>/start HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc local:/var/run/docker.sock 80 > /dev/null && echo "Started Container 2" || echo "Failed to start Container 2"'
        - echo "Containers restarted."

Example run command

docker run \
  --detach --name borgmatic \
  -v /home:/mnt/source:ro \
  -v /opt/docker/docker-borgmatic/data/repository:/mnt/borg-repository \
  -v /opt/docker/docker-borgmatic/data/borgmatic.d:/etc/borgmatic.d/ \
  -v /opt/docker/docker-borgmatic/data/.config/borg:/root/.config/borg \
  -v /opt/docker/docker-borgmatic/data/.ssh:/root/.ssh \
  -v /opt/docker/docker-borgmatic/data/.cache/borg:/root/.cache/borg \
  -e TZ=Europe/London \
  modem7/borgmatic-docker

While the parameters above are sufficient for regular backups, following additional privileges will be needed to mount an archive as FUSE filesystem:

--cap-add SYS_ADMIN \
--device /dev/fuse \
--security-opt label:disable \
--security-opt apparmor:unconfined

Depending on your security system, --security-opt parameters may not be necessary. label:disable is needed for SELinux, while apparmor:unconfined is needed for AppArmor.

To init the repo with encryption, run:

docker exec borgmatic \
sh -c "borgmatic --init --encryption repokey-blake2"

Layout

/mnt/source

Your data you wish to backup. For some safety you may want to mount read-only. Borgmatic is running as root so all files can be backed up.

/mnt/borg-repository

Mount your borg backup repository here.

/etc/borgmatic.d

Where you need to create crontab.txt and your borgmatic config.yml

docker exec borgmatic \
sh -c "cd && borgmatic config generate -d /etc/borgmatic.d/config.yaml"
0 1 * * * PATH=$PATH:/usr/bin /usr/bin/borgmatic --stats -v 0 2>&1

/root/.config/borg

Here the borg config and keys for keyfile encryption modes are stored. Make sure to backup your keyfiles! Also needed when encryption is set to none.

/root/.ssh

Mount either your own .ssh here or create a new one with ssh keys in for your remote repo locations.

/root/.cache/borg

A non-volatile place to store the borg chunk cache.

Environment

Docker Compose