Restore full, incremental, and compressed backups¶
Warning
Backup needs to be prepared before it can be restored.
The restore backup procedure is the same for full, incremental, and compressed backups.
Ensure you have one of the following before proceeding:
-
Root access
-
sudoaccess — administrator privileges are required to restore files, change ownership, and manage the database service.
For convenience, xtrabackup binary has the --copy-back option to copy the backup to the datadir of the server:
xtrabackup --copy-back --target-dir=/data/backups/
If you don’t want to save your backup, you can use the --move-back option which will move the backed up data to the datadir.
If you don’t want to use any of the above options, you can also use rsync or cp to restore the files.
Note
The datadir must be empty before restoring the backup. Also, it’s important to note that MySQL server needs to be shut down before restore is performed. You cannot restore to a datadir of a running mysqld instance (except when importing a partial backup).
An example of the rsync command to restore the backup:
rsync -avrP /data/backup/ /var/lib/mysql/
You should check that the restored files have the correct ownership and permissions.
As files’ attributes will be preserved, in most cases you will need to change the files’ ownership to mysql before starting the database server, as they will be owned by the user who created the backup:
chown -R mysql:mysql /var/lib/mysql
Prepare grastate.dat for Galera cluster (Optional)¶
When restoring a backup of a Percona XtraDB Cluster, be sure the grastate.dat file is present and correctly populated before you start the node.
The example demonstrates how to restore a backup on a single node and then bootstrap a new Percona XtraDB Cluster. Once the first node is up, additional nodes can join later via IST (Incremental State Transfer) or SST (State Snapshot Transfer).
This grastate.dat file contains the following information:
-
UUID- identifies the specific Galera cluster the node last belonged to. -
seqno- indicates the global transaction number of the last committed write-set on that node. -
safe_to_bootstrapflag - indicates whether the node was the last to shut down gracefully. A value of1means the node was the last to shut down gracefully and it is safe to use this node to bootstrap a new cluster. A value of0means the node was not the last to shut down gracefully and it should not be used to start the cluster.
After preparing and restoring the backup, do the following steps:
-
Extract the Galera state information from the backup metadata file located in the restored datadir (for example,
/var/lib/mysql). The extracted information is represented in theuuid:seqnoformat.cat /var/lib/mysql/xtrabackup_galera_infoExpected output
3f9d1c8e-4a12-11ee-bc3c-8c8590c4abcd:145672 -
Create or update the
grastate.datfile in the datadir using the extractedUUIDandseqno. In this example, the node is used to bootstrap a new cluster, sosafe_to_bootstrapis set to1.Note
Set
safe_to_bootstrapto1only on the node that will be used to bootstrap a new cluster. All other nodes must havesafe_to_bootstrapset to0.Bootstrap node (
safe_to_bootstrap: 1):cat > /var/lib/mysql/grastate.dat <<EOF # GALERA saved state version: 2.1 uuid: 3f9d1c8e-4a12-11ee-bc3c-8c8590c4abcd seqno: 145672 safe_to_bootstrap: 1 EOFAll other nodes (
safe_to_bootstrap: 0):cat > /var/lib/mysql/grastate.dat <<EOF # GALERA saved state version: 2.1 uuid: 3f9d1c8e-4a12-11ee-bc3c-8c8590c4abcd seqno: 145672 safe_to_bootstrap: 0 EOF -
Ensure the file has the correct ownership:
chown mysql:mysql /var/lib/mysql/grastate.dat
If grastate.dat is missing or contains incorrect values, the node may fail to start or attempt an unsafe cluster bootstrap.
Data is now restored, and you can start the server.