1.7. Backing up and restoring MariaDB data with physical copies
A physical backup of MariaDB data contains files and directories that store the content. This method is typically faster and smaller in size.
You can create physical online backups of your MariaDB server by using the mariabackup utility to backup InnoDB, Aria, and MyISAM tables while the server is running. The utility supports full backup capability for MariaDB server, which includes encrypted and compressed data.
Prerequisites
-
The
mariadb-backuppackage is installed on the system: -
You must provide
mariabackupwith credentials for the user under which the backup will be run. You can provide the credentials either on the command line or by a configuration file. -
Users of
mariabackupmust have theRELOAD,LOCK TABLES, andREPLICATION CLIENTprivileges.
Procedure
Use one of the following options to create a backup:
To create a backup while providing credentials on the command line, enter:
$ mariabackup --backup --target-dir <backup_directory> --user <backup_user> --password <backup_passwd>The
target-diroption defines the directory where the backup files are stored. If you want to perform a full backup, the target directory must be empty or not exist.The
userandpasswordoptions allow you to configure the user name and the password.To create a backup with credentials set in a configuration file:
-
Create a configuration file in the
/etc/my.cnf.d/directory, for example,/etc/my.cnf.d/mariabackup.cnf. Add the following content to the file:
[mysqld] user=<backup_username> password=<password>Perform the backup:
$ mariabackup --backup --target-dir <backup_directory>
-
Create a configuration file in the
1.7.2. Restoring data by using mariabackup 复制链接链接已复制到粘贴板!
If you have a MariaDB backup created by the mariabackup utility, you can use the same utility to restore the data.
Prerequisites
-
The
mariadbservice is stopped. - The data directory is empty.
-
Users of
mariabackupmust have theRELOAD,LOCK TABLES, andREPLICATION CLIENTprivileges.
Procedure
Use one of the following options to restore the data:
To restore data from the backup in the
/var/mariadb/backup/directory and keep the original backup files, enter:$ mariabackup --copy-back --target-dir=/var/mariadb/backup/To restore data from the backup in the
/var/mariadb/backup/directory and remove the original backup files, enter:$ mariabackup --move-back --target-dir=/var/mariadb/backup/
Fix the file permissions. For example, to recursively change ownership of the files to the
mysqluser and group, enter:# chown -R mysql:mysql /var/lib/mysql/When restoring a database,
mariabackuppreserves the file and directory privileges of the backup. However,mariabackupwrites the files to disk as the user and group restoring the database. Therefore, after restoring a backup, you must adjust the owner of the data directory to match the user and group for the MariaDB server.Start the
mariadbservice:# systemctl start mariadb.service
A file system level backup is a fast way to back up a complete MariaDB instance. This method requires a shutdown of the mariadb service for data consistency.
A file system-level backup is specific to an architecture and MariaDB version. You cannot restore data backed up by this method on a different architecture or MariaDB version.
Procedure
Stop the
mariadbservice:# systemctl stop mariadb.serviceCreate a backup directory, for example:
# mkdir -p /root/mariadb-backup/{data,config}/Back up the data directory:
# cp -rp /var/lib/mysql/ /root/mariadb-backup/data/Back up the configuration files:
# cp -rp /etc/my.cnf /etc/my.cnf.d/ /root/mariadb-backup/config/Start the
mariadbservice:# systemctl start mariadb.service
If your MariaDB instance has been corrupted, and you previously performed a file system backup that includes the data directory and the configuration files, you can restore the instance from this backup.
Prerequisites
- You performed a file system backup on a MariaDB server.
The target server must meet the following conditions of the backup source:
- The MariaDB version must be identical or higher.
- The system architecture must be identical.
Procedure
Stop the
mariadbservice:# systemctl stop mariadb.serviceRemove the current
/var/lib/mysql/directory:# rm -rf /var/lib/mysql/Restore the data directory from your backup:
# cp -rp /root/mariadb-backup/data/mysql/ /var/lib/Ensure the correct ownership of the
/var/lib/mysql/directory:# chown -R mysql:mysql /var/lib/mysql/Restore the SELinux context of the
/var/lib/mysql/directory:# restorecon -Rv /var/lib/mysql/Remove the current configuration files:
# rm -rf /etc/my.cnf /etc/my.cnf.d/Restore the configuration files from your backup:
# cp -rp /root/mariadb-backup/config/my.cnf /root/mariadb-backup/config/my.cnf.d/ /etc/Ensure the correct ownership of the configuration files:
# chown -R root:root /etc/my.cnf /etc/my.cnf.d/Restore the SELinux context of the configuration files:
# restorecon -Rv /etc/my.cnf /etc/my.cnf.d/Start the
mariadbservice:# systemctl start mariadb.service
Verification
Connect to a MariaDB database and query data, for example:
# mariadb -u root -p <database> -e "*SELECT * FROM <table>;"