Full Backup and Restore with mariadb-backup
Learn how to perform and restore full physical backups of MariaDB databases using the mariadb-backup tool, ensuring consistent data recovery.
When using mariadb-backup, you have the option of performing a full or an incremental backup. Full backups create a complete backup of the database server in an empty directory while incremental backups update a previous backup with whatever changes to the data have occurred since the backup. This page documents how to perform full backups.
Backing up the Database Server
In order to back up the database, you need to run mariadb-backup with the --backup option to tell it to perform a backup and with the --target-dir option to tell it where to place the backup files. When taking a full backup, the target directory must be empty or it must not exist.
To take a backup, run the following command:
$ mariadb-backup --backup \
--target-dir=/var/mariadb/backup/ \
--user=mariadb-backup --password=mypasswordThe time the backup takes depends on the size of the databases or tables you're backing up. You can cancel the backup if you need to, as the backup process does not modify the database.
mariadb-backup writes the backup files the target directory. If the target directory doesn't exist, it creates it. If the target directory exists and contains files, it raises an error and aborts.
Here is an example backup directory:
$ ls /var/mariadb/backup/
aria_log.0000001 mysql xtrabackup_checkpoints
aria_log_control performance_schema xtrabackup_info
backup-my.cnf test xtrabackup_logfile
ibdata1 xtrabackup_binlog_infoUsing the Backup History Feature
You can optionally use the --history option to record metadata about your full backup in the database. This creates a centralized log and allows future incremental backups to reference this full backup by name instead of by directory path.
$ mariadb-backup --backup \
--target-dir=/var/mariadb/backup/ \
--user=mariadb-backup --password=mypassword \
--history=full_backup_weeklyPrivileges: The backup user requires
INSERT,CREATE, andALTERprivileges on the history table (mysql.mariadb_backup_historyin MariaDB 10.11+, orPERCONA_SCHEMA.xtrabackup_historyin older versions).Failure Case: If the user lacks privileges, the backup will complete the file copy process but will fail at the final step with an
INSERT command deniederror.
Preparing the Backup for Restoration
The data files that mariadb-backup creates in the target directory are not point-in-time consistent, given that the data files are copied at different times during the backup operation. If you try to restore from these files, InnoDB notices the inconsistencies and crashes to protect you from corruption
Before you can restore from a backup, you first need to prepare it to make the data files consistent. You can do so with the --prepare option.
$ mariadb-backup --prepare \
--target-dir=/var/mariadb/backup/Backup Preparation Steps
Run
mariadb-backup --backup. You must use a version ofmariadb-backupthat is compatible with the server version you are planning to upgrade from. For instance, when upgrading from MariaDB 10.4 to 10.5, you must use the 10.4 version ofmariadb-backup, Another example: When upgrading from MariaDB 10.6 to 10.11, you must use the 10.6 version ofmariadb-backup.Run
mariadb-backup --prepare, again using a compatible version ofmariadb-backup, as described in the previous step.
Restoring the Backup
Once the backup is complete and you have prepared the backup for restoration (previous step), you can restore the backup using either the --copy-back or the --move-back options. The --copy-back option allows you to keep the original backup files. The --move-back option actually moves the backup files to the datadir, so the original backup files are lost.
First, stop the MariaDB Server process.
Then, ensure that
datadiris empty.Then, run
mariadb-backupwith one of the options mentioned above:
$ mariadb-backup --copy-back \
--target-dir=/var/mariadb/backup/Then, you may need to fix the file permissions.
When mariadb-backup restores a database, it preserves the file and directory privileges of the backup. However, it writes the files to disk as the user and group restoring the database. As such, after restoring a backup, you may need to adjust the owner of the data directory to match the user and group for the MariaDB Server, typically mysql for both. For example, to recursively change ownership of the files to the mysql user and group, you could execute:
$ chown -R mysql:mysql /var/lib/mysql/Finally, start the MariaDB Server process.
Restoring with Other Tools
Once a full backup is prepared, it is a fully functional MariaDB data directory. Therefore, as long as the MariaDB Server process is stopped on the target server, you can technically restore the backup using any file copying tool, such as cp or rsync. For example, you could also execute the following to restore the backup:
$ rsync -avrP /var/mariadb/backup /var/lib/mysql/
$ chown -R mysql:mysql /var/lib/mysql/This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

