Container Backup and Restoration
MariaDB databases in containers need backup and restore like their non-container equivalents.
Logicial Backups
Backup
mariadb-dump is in the Docker Official Image and can be used as follows:
$ docker exec some-%%REPO%% mariadb-dump --all-databases -uroot -p"$MARIADB_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
Restoring Data from Dump Files
For restoring data, you can use the `docker exec` command with the `-i` flag, similar to the following:
$ docker exec -i some-%%REPO%% sh -c 'exec mariadb -uroot -p"$MARIADB_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
Physical Backups
mariadb-backup is in the Docker Official Image.
Backup
MariaDB Backup can create a backup as follows:
To perform a backup using Mariabackup, a second container is started that shares the original container's data directory. An additional volume for the backup needs to be included in the second backup instance. Authentication against the MariaDB database instance is required to successfully complete the backup. In the example below, a `mysql@localhost` user is used with the MariaDB server's Unix socket shared with the backup container.
$ docker volume create some-%%REPO%%-socket $ docker run --name some-%%REPO%% -v /my/own/datadir:/var/lib/mysql -v some-%%REPO%%-socket:/var/run/mysqld -e MARIADB_MYSQL_LOCALHOST_USER=1 -e MARIADB_MYSQL_LOCALHOST_GRANTS="RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR" -e MARIADB_ROOT_PASSWORD=my-secret-pw -d %%IMAGE%%:latest
Note: Privileges listed here are for 10.5+. For an exact list, see Mariabackup: Authentication and Privileges.
Mariabackup will run as the `mysql` user in the container, so the permissions on `/backup` will need to ensure that it can be written to by this user:
$ docker volume create some-%%REPO%%-backup $ docker run --rm some-%%REPO%%-backup -v some-%%REPO%%-backup:/backup %%IMAGE%%:latest chown mysql: /backup
Restore
These steps restore the backup made with Mariabackup.
At some point before doing the restore, the backup needs to be prepared. Perform the prepare like this:
$ docker run --user mysql --rm -v some-%%REPO%%-backup:/backup %%IMAGE%%:latest mariabackup --prepare --target-dir=/backup
Now that the image is prepared, start the container with both the data and the backup volumes and restore the backup:
$ docker run --user mysql --rm -v /my/new/datadir:/var/lib/mysql -v some-%%REPO%%-backup:/backup %%IMAGE%%:latest mariabackup --copy-back --target-dir=/backup
With `/my/new/datadir` containing the restored backup, start normally as this is an initialized data directory:
$ docker run --name some-%%REPO%% -v /my/new/datadir:/var/lib/mysql -d %%IMAGE%%:latest
For further information on Mariabackup, see Mariabackup Overview.