Container Backup and Restoration

You are viewing an old version of this article. View the current version here.

MariaDB databases in containers need backup and restore like their non-container equivalents.

Logicial Backups

In this section, we will assume that the MariaDB container has been created as follows:

$ docker volume create mariadb_data
$ docker volume create mariadb_backup
$ docker run --rm \
  -v mariadb_data:/var/lib/mysql \
  -v mariadb_backup:/backup \
  mariadb \
  chown -R mysql:mysql /var/lib/mysql /backup
$ docker run -d --name mariadb \
  -v mariadb_data:/var/lib/mysql \
  -v mariadb_backup:/backup \
  -e MARIADB_ROOT_PASSWORD='MariaDB11!' \
  <mariadb-image>

Backup

mariadb-dump is in the Docker Official Image and can be used as follows:

$ docker exec mariadb sh -c 'mariadb-dump --all-databases -u root -p"$MARIADB_ROOT_PASSWORD" > backup/db.sql'

Restoring Data from Dump Files

For restoring data, you can use the following `docker exec` command:

$ docker exec mariadb sh -c 'mariadb -u root -p"$MARIADB_ROOT_PASSWORD" < backup/db.sql'

Physical Backups

mariadb-backup is in the Docker Official Image.

Backup

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

Note: Privileges listed here are for 10.5+. For an exact list, see Mariabackup: Authentication and Privileges.

$ docker volume create mariadb_data
$ docker volume create mariadb_backup
$ docker run --rm \
  -v mariadb_data:/var/lib/mysql \
  -v mariadb_backup:/backup \
  mariadb \
  chown -R mysql:mysql /var/lib/mysql /backup
$ docker run -d --name mariadb \
  -v mariadb_data:/var/lib/mysql \
  -v mariadb_backup:/backup \
  -e MARIADB_ROOT_PASSWORD='MariaDB11!' \
  -e MARIADB_MYSQL_LOCALHOST_USER=1 \
  -e MARIADB_MYSQL_LOCALHOST_GRANTS='RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR' \
  <mariadb-image>

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 exec --user mysql mariadb 'mariadb-backup --backup --target-dir=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 --rm \
  --name mariadb-restore \
  -v mariadb_backup:/backup \
  <mariadb-image> \
  mariadb-backup --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. The data directory must be empty to perform this action:

$ docker run --rm \
  --name mariadb-restore \
  -v /my/new/datadir:/var/lib/mysql \
  -v mariadb_backup:/backup \
  <mariadb-image> \
  mariadb-backup --copy-back --target-dir=backup

With `/my/new/datadir` containing the restored backup, start normally as this is an initialized data directory:

$ docker run -d --name mariadb \
  -v /my/new/datadir:/var/lib/mysql \
  -e MARIADB_ROOT_PASSWORD='MariaDB11!' \
  <mariadb-image>

For further information on Mariabackup, see Mariabackup Overview.

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.