Backup of Database in Docker Container fails when using backup tool within that container and following docs #906
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What I'm trying to do and why
I am trying to backup a Database (MariaDB, although I assume other DBs are affected too) within a Docker Container.
Borgmatic is not installed in a Container, but on the host instead.
I also do not have the database backup tool (
mariadb-dump
) installed on the host, so I am using the one provided inside the Container.So I followed the docs (specifically this part), to setup the backup.
It is failing however, because the command executed by borgmatic for the db backup includes the
--result-file
option. Since with this configuration themariadb-dump
tool gets executed inside the container, it tries to output to the result file inside the container, which does not exist.(
/home/USERNAME/.borgmatic/mariadb_databases/localhost/DATABASE_NAME
in my case)After playing around a bit and figuring out that it was because the file by borgmatic to pipe the db backup into was created on the host, but the actual mariadb backup tool tried to put the content into that file inside the container, I was able to solve that issue by mounting the .borgmatic directory inside the container with read and write permissions.
I am not sure however, if this has any security implications and if there is a better way to achieve the result without mounting.
For example using the mariadb-dump command with a shell pipe (
> OUTPUT file
) on the host system worked fine instead of the --result-file config.The --result-file option is hardcoded though.
Steps to reproduce
mariadb-dump
tool inside Container (docker exec NAME mariadb-dump [...]
)relevant part of docker compose file:
relevant part of borgmatic config file:
Actual behavior
Borgmatic fails to create database backup
Expected behavior
Since borgmatic creates the file on the host system, the db backup content should be put inside the same file.
Other notes / implementation ideas
As described earlier, if I include the following volume mount in the Docker compose file for the DB, it works.
If there are no security implications to mounting the .borgmatic folder inside the Docker Container, I would add this to the docs.
(Happy to create a PR for it myself, if this is the chosen solution)
borgmatic version
1.8.13
borgmatic installation method
pip install
Borg version
borg 1.2.4
Python version
Python 3.11.2
Database version (if applicable)
mariadb Ver 15.1 Distrib 10.6.19-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Operating system and version
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)" NAME="Debian GNU/Linux" VERSION_ID="12" VERSION="12 (bookworm)" VERSION_CODENAME=bookworm ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"
Good catch, and thank you for the detailed ticket! I think your analysis is completely correct here.
That makes sense, but it wouldn't work when running
mariadb-dump
inside of the container, right? Because then the shell pipe would still go to a file inside the container.I think the
~/.borgmatic
volume mount is probably the right solution here. There are some security implications, namely that a database container would have read-write access to: 1. database dump temporary files (which is what we want) and 2. records of when borgmatic check last ran (not a big deal). So I think the security risks are low and the worst an attacker could do is take control of the database container and prevent database backups from working and maybe prevent borgmatic checks from running as frequently. If you're okay with that, I'm okay with that.A PR would be welcome! Besides adding the "fix" in the docs, it might be good to at least mention it in the configuration schema.
True, didn't think about that.
I am okay with that.
Sorry to ask, but isn't that exactly what you do (redirection) in the Postgresql binding?
It works pretty well for me, instead of the volume solution described above.
+ (('>', shlex.quote(dump_filename)) if dump_format != 'directory' else ())
That's correct; the PostgreSQL hook does perform the same approach, and it would presumably have the same problem if run in a container without a directory format database dump.
Implemented in main—thank you for the PR!—and will be part of the next release. (I tweaked some of the wording hopefully for clarity.)