Lesson 6: Backup and Restore

Backup and Restore

Learning Objectives

Backing Up Data

Backup Principles

Basic Types of Backup Methods

Physical or Binary Backup

MariaDB Backup, manual data directory copy (after stopping the MariaDB daemon), or VM/Cloud/LVM/ZFS snapshots

Logical Backup

mariadb-dump, mydumper, SELECT INTO OUTFILE

MariaDB Backup

MariaDB’s own backup package (mariabackup) is available from MariaDB repositories

Backing Up Data - MariaDB Backup

Full Backups

To use MariaDB Backup you need to create a user on your MariaDB Server with RELOAD, LOCK TABLES and REPLICATION CLIENT privileges.

MariaDB [(none)]> CREATE USER '<backupuser>'@'localhost' IDENTIFIED BY '<password>';
MariaDB [(none)]> GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO '<backupuser>'@'localhost';

To take a full backup at the OS command-line use:

# mariabackup --backup --target-dir <backupdir> --user <backupuser> --password <password>
# mariabackup --prepare --target-dir <backupdir>

Backing Up Data - MariaDB-dump

The standard MariaDB logical dump tool copies schema and data to a SQL text file

To use mariadb-dump you need to create a user on your MariaDB Server with SELECT, RELOAD, LOCK TABLES, REPLICATION CLIENT, SHOW VIEW, EVENT, and TRIGGER privileges

CREATE USER 'backupuser'@'localhost' IDENTIFIED BY 'mariadb';

GRANT SELECT, RELOAD, LOCK TABLES, REPLICATION CLIENT, SHOW VIEW, EVENT, TRIGGER ON *.* TO 'backupuser'@'localhost';

To take a full backup at the OS command line use

# mariadb-dump -u backupuser -p --all-databases --single-transaction --flush-logs -r /path/to/full-backup-YYYYMMDD.sql

Planning and Scheduling Backups

Take inventory of databases

Write a backup schedule

Write a verification schedule

Verify and practice restoring from backups

Naming standard for backup files

Frequency — days and times

Location — security and off-site

Restoring Data

Restoring Data - MariaDB Backup

Full Backups

Working with a full backup taken with MariaDB Backup, you restore the backup into an empty data directory

# mariabackup --copy-back --target-dir <backupdir> --datadir <datadir>

Afterwards it might be necessary to set the ownership of the data directory contents

# chown -R mysql:mysql <datadir>

Restoring Data - MariaDB-dump

Restoring from a logical backup

# mariadb < /path/to/full-backup-YYYYMMDD.sql

Or to avoid interpretation by the shell

# mariadb
SOURCE /path/to/full-backup-YYYYMMDD.sql

You can also use standard linux tools such as vi or grep to edit or extract the backup

# grep city worldbackup.sql
CREATE TABLE `world`.`city` ( ...

Verifying Backups

Lesson Summary


For academic and non-commercial usage, licensed under CC BY-NC-SA 4.0 by MariaDB plc.