Restoring backups is a critical task for database administrators to ensure data consistency and disaster recovery. This guide provides a step-by-step process to restore a database backup stored in an Amazon S3 bucket to a new EC2 instance using Percona XtraBackup.
Start by provisioning a new instance that will serve as the restoration environment. Ensure the instance has the necessary permissions to access the S3 bucket (via an IAM role or AWS credentials) and sufficient storage for the backup files. Install required tools, such as AWS CLI and Percona XtraBackup.
Use the aws s3 sync command to download the backup files from your S3 bucket to the instance. Command:
aws s3 sync s3://path/to/backup /path/to/destination
This command synchronizes all files from the specified S3 path to the destination folder.
[ Good Read: Data Engineering vs. Data Science ]
If the downloaded backup files are compressed, decompress them using XtraBackup’s --decompress option. Command:
xtrabackup --decompress --target-dir=/file/path
Before restoration, prepare the backup to ensure it is consistent and ready to be restored. This step applies the redo log files. Command:
xtrabackup --prepare --target-dir=/path/to/backup
This ensures the backup is in a usable state.
To prevent conflicts during restoration, stop the MySQL service.
sudo systemctl stop mysql
Use the --copy-back or --move-back option to restore the backup to the MySQL data directory. Command:
xtrabackup --move-back --target-dir=/path/to/backup
OR
xtrabackup --copy-back --target-dir=/path/to/backup
Once restored, verify that the files are in the correct location, typically /var/lib/mysql.
Ensure the MySQL data directory has the correct ownership and permissions. Use the chown command to set the owner to mysql:mysql.
sudo chown -R mysql:mysql /var/lib/mysql
Restart the MySQL service to apply the restored database.
sudo systemctl start mysql
Log into MySQL and verify that the restored data is intact and operational.
mysql -u root -p
SHOW DATABASES;
You can check more info about: Backup Stored in S3 to an EC2 Instance.