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-backup package is installed on the system:
  • You must provide mariabackup with 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 mariabackup must have the RELOAD, LOCK TABLES, and REPLICATION CLIENT privileges.

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-dir option 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 user and password options allow you to configure the user name and the password.

    • To create a backup with credentials set in a configuration file:

      1. Create a configuration file in the /etc/my.cnf.d/ directory, for example, /etc/my.cnf.d/mariabackup.cnf.
      2. Add the following content to the file:

        [mysqld]
        user=<backup_username>
        password=<password>
      3. Perform the backup:

        $ mariabackup --backup --target-dir <backup_directory>

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 mariadb service is stopped.
  • The data directory is empty.
  • Users of mariabackup must have the RELOAD, LOCK TABLES, and REPLICATION CLIENT privileges.

Procedure

  1. 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/
  2. Fix the file permissions. For example, to recursively change ownership of the files to the mysql user and group, enter:

    # chown -R mysql:mysql /var/lib/mysql/

    When restoring a database, mariabackup preserves the file and directory privileges of the backup. However, mariabackup writes 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.

  3. Start the mariadb service:

    # systemctl start mariadb.service

1.7.3. Performing a file system backup on a MariaDB server

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

  1. Stop the mariadb service:

    # systemctl stop mariadb.service
  2. Create a backup directory, for example:

    # mkdir -p /root/mariadb-backup/{data,config}/
  3. Back up the data directory:

    # cp -rp /var/lib/mysql/ /root/mariadb-backup/data/
  4. Back up the configuration files:

    # cp -rp /etc/my.cnf /etc/my.cnf.d/ /root/mariadb-backup/config/
  5. Start the mariadb service:

    # systemctl start mariadb.service

1.7.4. Restoring a file system backup on a MariaDB server

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

Procedure

  1. Stop the mariadb service:

    # systemctl stop mariadb.service
  2. Remove the current /var/lib/mysql/ directory:

    # rm -rf /var/lib/mysql/
  3. Restore the data directory from your backup:

    # cp -rp /root/mariadb-backup/data/mysql/ /var/lib/
  4. Ensure the correct ownership of the /var/lib/mysql/ directory:

    # chown -R mysql:mysql /var/lib/mysql/
  5. Restore the SELinux context of the /var/lib/mysql/ directory:

    # restorecon -Rv /var/lib/mysql/
  6. Remove the current configuration files:

    # rm -rf /etc/my.cnf /etc/my.cnf.d/
  7. Restore the configuration files from your backup:

    # cp -rp /root/mariadb-backup/config/my.cnf /root/mariadb-backup/config/my.cnf.d/ /etc/
  8. Ensure the correct ownership of the configuration files:

    # chown -R root:root /etc/my.cnf /etc/my.cnf.d/
  9. Restore the SELinux context of the configuration files:

    # restorecon -Rv /etc/my.cnf /etc/my.cnf.d/
  10. Start the mariadb service:

    # systemctl start mariadb.service

Verification

  • Connect to a MariaDB database and query data, for example:

    # mariadb -u root -p <database> -e "*SELECT * FROM <table>;"
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部