Backing up and Restoring your MariaDB Database

You can use the tool Mariabackup to backup and restore your MariaDB database. For more information, see https://mariadb.com/kb/en/mariabackup-overview.

  1. Backup your database by running the following command:

    $ mariabackup --backup \

    --target-dir=/var/mariadb/backup/ \

    --encrypted-backup --user=mariabackup --password=mypassword\

  2. After the backup is complete, prepare the backup for restoration by running the following command: 

    $ mariabackup --prepare \

    --use-memory=124M --target-dir=/var/mariadb/backup/

    Note: The --use-memory command defines the buffer pool size that is used during the preparation stage. If you see errors while preparing a backup, try increasing the buffer pool size.

  3. Use the --moveback or --copyback commands to restore the database as follows: 

    Note: The --moveback option physically moves the backup files to the new directory, so the original backup files are lost. The --copyback option keeps the original backup files.

    1. Stop the MariaDB service using the following command:  systemctl stop mariadb

    2. Ensure that the directory where you plan to restore the backup is empty.

    3. Run the Mariabackup with your preferred option. For example:

      $ mariabackup --copy-back \

      --target-dir=/var/mariadb/backup/

      Note: This command restores the backup to the same location. If you want to restore the backup on a different VM, you need to share the backup folder to another VM using the SCP command. For example:  scp /var/mariadb/backup user@destination_host:/var/mariadb/backup

    4. If necessary, update the file permissions using the chown -R mysql:mysql /var/lib/mysql/ command.

    5. Start the MariaDB service using the start mariadb command.