docker-borgmatic

docker-borgmatic

Docker容器备份的自动化解决方案

docker-borgmatic是一个基于Borgbackup的Docker镜像,为容器化环境提供自动化备份解决方案。该项目集成borgmatic工具,实现灵活配置、定时任务和通知功能。它支持备份Docker卷和主机数据,并提供FUSE文件系统挂载、容器停止/启动钩子等高级特性。通过详细的使用说明,docker-borgmatic使容器数据备份变得简单高效,适合各类备份需求。

borgmaticDocker备份容器化数据安全Github开源项目

borgmatic Container

Description

This repository provides a Docker image for borgmatic, a simple and efficient backup tool based on Borgbackup. The image is designed to make it easy to set up and run borgmatic (with Borg and optionally Cron daemon) within a Docker container, enabling you to streamline your backup process and ensure the safety of your data.

Warning As of 2022-01-29 this image has switched to use Supercronic instead of cron from alpine

Warning As of 2023-06-23 msmtp and ntfy flavors have been discontinued. This image has now switched to apprise.

Usage

Prerequisites

Before proceeding, ensure that you have Docker installed and properly configured on your system. Refer to the Docker documentation for installation instructions specific to your operating system. If you want to use docker-compose, you may also need to install it seperately. Alternatively, you can also use podman to run this image.

Getting Started

Run this command to create data directories required by this image under your prefered directory.

mkdir data/{borgmatic.d,repository,.config,.ssh,.cache}

Configure a copy of borgmatic's config.yaml in data/borgmatic.d and run the container. You can modify any of the host mount point to fit your backup configuration.

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

See Other usage methods below for more options.

Volumes

The following volumes are available for mounting:

VolumeDescription
/mnt/sourceYour 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-repositoryMount your borg backup repository here.
/etc/borgmatic.dWhere you need to create crontab.txt and your borgmatic config.yml
/root/.borgmaticNote this is now redundant and has been deprecated, please remove this from your configs
/root/.config/borgHere 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/.sshMount either your own .ssh here or create a new one with ssh keys in for your remote repo locations.
/root/.cache/borgA non-volatile place to store the borg chunk cache.

To generate an example borgmatic configuration, run:

docker exec borgmatic \
bash -c "cd && borgmatic config generate -d /etc/borgmatic.d/config.yaml"

Environment

You can set the following environment variables:

VariableDescription
TZTime zone, e.g. TZ="Europe/Berlin"'.
BORG_RSHSSH parameters, e.g. BORG_RSH="ssh -i /root/.ssh/id_ed25519 -p 50221"
BORG_PASSPHRASERepository passphrase, e.g. BORG_PASSPHRASE="DonNotMissToChangeYourPassphrase"
BACKUP_CRONCron schedule to run borgmatic. Default:0 1 * * *
RUN_ON_STARTUPRun borgmatic on startup. e.g.: RUN_ON_STARTUP=true

You can also provide your own crontab file. If data/borgmatic.d/crontab.txt exists, BACKUP_CRON will be ignored in preference to it. In here you can add any other tasks you want ran

0 1 * * * PATH=$PATH:/usr/local/bin /usr/local/bin/borgmatic --stats -v 0 2>&1

Beside that, you can also pass any environment variable that is supported by borgmatic. See documentation for borgmatic and Borg and for a list of supported variables.

Using Secrets (Optional)

You also have the option of using Docker Secrets for more sensitive information. This is not mandatory, but provides an additional layer of security. Note that this function is only applicable to environment variables that start with BORG or YOUR.

For each environment variable such as BORG_PASSPHRASE, you can create a corresponding secret file called BORG_PASSPHRASE_FILE. Store the contents of the secret file in this file. The start script automatically searches for corresponding _FILE secrets if the environment variables are not set and loads them.

It is important to know that this environment variable is not available via docker compose exec borgmatic sh. Only for the automated call via the defined cron.

Using Apprise for Notifications

To enhance your experience with Borgmatic, we'll show you a quick example of how to use Apprise for notifications. Apprise is a versatile tool that integrates with a variety of services and is built into Borgmatic. With the upcoming version 1.8.4 also natively. Here's a quick example of how you can use Apprise.

Basic Setup

Cronjob Configuration

In an unmodified Borgmatic installation, your cronjob.txt might look something like this:

0 1 * * * /usr/local/bin/borgmatic --stats -v 0 2>&1

To incorporate Apprise notifications, you can modify it like this:

*/5 * * * * PATH=$PATH:/usr/local/bin /usr/local/bin/borgmatic --stats -v 0 > /tmp/backup_run.log

Borgmatic Configuration

Add the following lines to your Borgmatic configuration file (config.yaml):

before_backup: - echo "Starting a backup job." after_backup: - echo "Backup created." - apprise -vv -t "✅ SUCCESS" -b "$(cat /tmp/backup_run.log)" "mailtos://smtp.example.com:587?user=server@example.com&pass=YourSecurePassword&from=server@example.com&to=receiver@example.com" on_error: - echo "Error while creating a backup." - apprise -vv -t "❌ FAILED" -b "$(cat /tmp/backup_run.log)" "mailtos://smtp.example.com:587?user=server@example.com&pass=YourSecurePassword&from=server@example.com&to=receiver@example.com"
Note:

If you don't want to send the log file, you can replace -b "$(cat /tmp/backup_run.log)" with a custom message like -b "My message".

Advanced Options

Apprise Capabilities

Apprise offers a variety of services to send notifications to, such as Telegram, Slack, Discord, and many more. For a complete list, visit the Apprise GitHub page.

Example for Multiple Services

Apprise allows you to notify multiple services at the same time:

after_backup: - echo "Backup created." - apprise -vv -t "✅ SUCCESS" -b "$(cat /tmp/backup_run.log)" "mailtos://smtp.example.com:587?user=server@example.com&pass=YourSecurePassword&from=server@example.com&to=receiver@example.com,slack://token@Txxxx/Bxxxx/Cxxxx"

Native Apprise Configuration in Borgmatic 1.8.4+

Starting from version 1.8.4, Borgmatic has native support for Apprise within its configuration. This makes it even easier to set up notifications. Below is how you can add Apprise directly to your Borgmatic config.yaml.

apprise: states: - start - finish - fail services: - url: mailtos://smtp.example.com:587?user=server@example.com&pass=YourSecurePassword&from=server@example.com&to=receiver@example.com label: mail - url: slack://token@Txxxx/Bxxxx/Cxxxx label: slack start: title: ⚙️ Started body: Starting backup process. finish: title: ✅ SUCCESS body: Backups successfully made. fail: title: ❌ FAILED body: Your backups have failed.

And as of borgmatic 1.8.9+, borgmatic's logs are automatically appended to the body for each notification.

Conclusion

Apprise provides a flexible and powerful way to handle notifications in Borgmatic. Be sure to check out the official Apprise documentation for a full range of options and capabilities.

Other usage methods

Run borgmatic like a binary through a container

This image can be used to run borgmatic like a binary by passing the borgmatic command while running the container. It allows you to isolate your system and execute borgmatic commands without directly installing borgmatic on your host system and only keeping persistent data.

To execute borgmatic commands, you can run your container by passing borgmatic subcommands:

docker run --rm -it \
MOUNT_FLAGS_HERE \
ghcr.io/borgmatic-collective/borgmatic \
list

NOTE Replace MOUNT_FLAGS_HERE placeholder with appropriate mount flags and optionally environment flags. See above for more clues.

This will execute borgmatic list in your container. The idea is to create symlink to a script which executes this. Now create a new file borgmatic-docker.sh somewhere like your workspace or home directory.

#!/bin/sh

docker run --rm -it \
MOUNT_FLAGS_HERE \
ghcr.io/borgmatic-collective/borgmatic \
"$@"

Modify the above script as per your needs and copy it's path. Now you can either create a symbolic link to this script or add it as alias.

  1. Create a symlink to a directory that exists in your PATH variable e.g.:
chmod +x /path/to/script/borgmatic-docker.sh
sudo ln [-s] /path/to/script/borgmatic-docker.sh /usr/local/bin/borgmatic
  1. Or, to create an alias add this to your ~/.bashrc or similar file for other shells.
alias borgmatic="sh /path/to/script/borgmatic-docker.sh"

Tip You can view list of available command line options in borgmatic's docs

Running as daemon

To keep the container always running for continous backup, you can run it in detached mode. If you do not pass the command, by default it'll start the cron daemon which will run borgmatic at interval set in crontab.txt file.

docker run -d --restart=always \
MOUNT_FLAGS_HERE \
ghcr.io/borgmatic-collective/borgmatic \

If you ever need to run borgmatic manually, for instance to view or recover files, run:

docker exec -it container_id_or_name bash

Then you can run borgmatic directly within that shell.

Structure deployment with docker-compose

Use docker compose for easily management of your borgmatic container. You can also use this image with your existing docker-compose configuration to immediate setup backups for your deployed containers and/or the host.

<!-- Configure .env -->
  1. Copy .env.template to .env and edit it to your needs.
cp .env.template .env

You will need to configure environment variables for volumes. You can also directly configure docker-compose.yml file.

Beside these, you can also set other configuration variables in your .env file. See Environment section for more details.

  1. Start the container
docker-compose up -d
  1. To view logs
docker-compose logs -f

Miscelaneous

If you want to run borgmatic commands using this configuration instead of starting the container as daemon, you can run:

<!-- TODO: entry.sh is not working with docker-compose, having to pass full command -->
docker-compose run --rm borgmatic borgmatic list

If a container is already running, you can execute borgmatic commands in it by running:

docker-compose exec borgmatic ls
# or to run a shell
docker-compose exec borgmatic bash

Restoring backups

  1. Stop the backup container: docker-compose down
  2. Modify volume /host/mount/location in docker-compose.restore.yml file to point to the location where you want to restore your backup.
  3. Run an interactive shell: docker-compose -f docker-compose.yml -f docker-compose.restore.yml run borgmatic
  4. Fuse-mount the backup: borg mount /mnt/borg-repository <mount_point>
  5. Restore your files
  6. Finally unmount and exit: borg umount <mount_point> && exit.

Tip In case Borg fails to create/acquire a lock: borg break-lock /mnt/repository

Advanced

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."

Mount an archive as FUSE filesystem

While the parameters defined in above examples 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 \
bash -c "borgmatic --init --encryption repokey-blake2"

Additional Reading

[Backup Docker using

编辑推荐精选

蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多