Full Backup and Restore with MariaDB Backup
When using MariaDB Backup, you have the option of performing a full or incremental backup. Full backups create a complete copy in an empty directory while incremental backups update a previous backup with new data. This page documents full backups.
Backing up Database
In order to back up the database, you need to run MariaDB Backup with the --backup
command option. Use the --target-dir
option to define were it places the backup files. When taking a full backup, the target directory must be empty or not exist.
To take a backup, run the following command:
$ mariabackup --backup --target-dir /var/mariadb/backup/ \ --user backup_user --password backup_passwd
The time the backup takes depends on the size of the databases or tables you're backing up. You can cancel the backup if you need to, as it does not modified the database.
MariaDB Backup adds a series of files to the target directory. If the target directory doesn't exist it creates it. If the target directory exists and contains files, the backup errors out.
$ ls /var/mariadb/backup/ aria_log.0000001 mysql xtrabackup_checkpoints aria_log_control performance_schema xtrabackup_info backup-my.cnf test xtrabackup_logfile ibdata1 xtrabackup_binlog_info
Restoring from Backups
The data files that MariaDB Backup creates in the target directory are not point-in-time consistent, given that the data is taken at different times during the backup operation. If you try to restore from these files, InnoDB notices the discrepancies and crashes to protect you from corrupted data.
Before you can restore from a backup, you first you to prepare it to normalize these files. You can do so with the --prepare
command option.
$ mariabackup --prepare --target-dir /var/mariadb/backup/ \ --user backup_user --password backup_passwd
Once this process is complete, you can restore data from the backup using either the --copy-back
or the --move-back
command options. Copy it when you want to keep the backup, move it when you don't want to keep it. Stop the MariaDB Server, then run the command with one of these command.
$ mariabackup --copy-back --target-dir /var/mariadb/backup/ \ --user backup_user --password backup_passwd
Bear in mind, the data directory must be empty before restoring the backup.
Finally, you may have to fix file privileges
$ chown -R mysql:mysql /var/lib/mysql/
Restoring with Other Tools
In the event that you don't want to use the tool to restore you database, you can manage the same using either cp
or rysnc
to copy the data from the target directory to the data directory. For instance,
$ rsync -avrP /var/mariadb/backup /var/lib/mysql/ $ chown -R mysql:mysql /var/lib/mysql/ $ rm /var/lib/mysql/ib_logfile*
The last step is needed with Mariabackup 10.2 for ensuring that the server will not attempt crash recovery with an old redo log. Mariabackup 10.1 would create redo log files after --prepare, but Mariabackup 10.2 will not create them, so that the LSN will not unnecessarily move forward. Instead, starting with Mariabackup 10.2.10, it creates a dummy (empty) ib_logfile0 to prevent an accidental startup with old redo log files.