1.6. Backing up and restoring MariaDB data with logical dumps
A logical backup of MariaDB data consists of the SQL statements necessary to restore the data. The advantage of logical backup over physical backup is that the data can be restored on other hardware configurations and MariaDB versions.
1.6.1. Performing a logical backup by using mariadb-dump 复制链接链接已复制到粘贴板!
You can use the mariadb-dump utility to back up databases while the MariaDB server is running and store the exported data in an SQL file. Store the backup in a safe location to be able to recover it in data loss scenarios.
Frequent scenarios with mariadb-dump include:
- Backing up a single database
- Backing up multiple databases
- Backing up all databases
The mariadb-dump utility stores its output in a single file. If you want to back up multiple databases and require one file per database, back up each database individually.
The mariadb-dump utility can back up only databases. This also includes server settings stored in the mysql database. However, the utility does not back up configuration files, such as /etc/my.cnf.
Prerequisites
-
The
mariadbservice is running. -
You have credentials with permissions to back up a database, for example, the
rootaccount.
Procedure
Create a consistent and comprehensive logical backup of MariaDB databases:
# mariadb-dump -u <username> -p --routines --events --triggers --single-transaction --result-file=backup.sql --databases <database_1> <database_2>where:
-u <username>- Sets the username the utility uses to connect to the database server.
-p- Prompts for the password.
--routines- Includes stored procedures and functions in the backup.
--events- Includes scheduled events in the backup.
--triggers- Includes triggers in the backup.
--single-transactionInitiates a consistent snapshot of databases with a transactional storage engine, such as InnoDB. By using a single transaction, all read operations reflect the database state at the moment the dump begins.
If you still use a non-transactional storage engine, such as MyISAM, use the
--lock-tablesoption instead of--single-transactionto ensure a consistent backup.--result-file=<output_file>-
Defines in which file
mariadb-dumpstores the output. --databases <list_of_databases>Defines the databases to back up. Alternatively, to back up all databases at once, use the
--all-databasesoption.重要A backup of a database contains only the data from that database. It does not include MariaDB user accounts or other server settings. MariaDB stores this essential security and system information in a separate
mysqlsystem database. Therefore, you must back upmysqlas well if you need to preserve these settings.
Verification
- Restore the backup in a sandbox environment and ensure that the data is correct.
1.6.2. Restoring MariaDB data from a dump in SQL format 复制链接链接已复制到粘贴板!
If you backed up one or multiple databases to an SQL file, you can use this file to re-create the database structure and its data.
Prerequisites
-
The
mariadbservice is running. -
You have credentials with permissions to restore data, for example, the
rootaccount.
Procedure
If the database you want to restore already exists and your SQL file does not contain
DROP TABLE IF EXISTSstatements, you must manually remove the tables or the entire database:To remove a table, enter:
# mariadb -u root -p -e "DROP TABLE <database>.<table>;"Repeat this command for every table that the SQL file will re-create.
To remove a database, enter:
# mariadb -u root -p -e "DROP DATABASE <database>;"Repeat this command for every database that the SQL file will re-create.
Import the SQL file:
# mariadb -u root -p < backup.sql"
Verification
Connect to a MariaDB database and query data, for example:
# mariadb -u root -p <database> -e "*SELECT * FROM <table>;"