Restoring a Backup Stored in S3 to an EC2 Instance Using XtraBackup

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.

1. Set up a new Instance/Server

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.

2. Download Backup Files from S3

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.

3. Decompress the Backup Files (If Needed)

If the downloaded backup files are compressed, decompress them using XtraBackup’s --decompress option. Command: 

xtrabackup --decompress --target-dir=/file/path

4. Prepare the Backup for Restoration

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.

5. Stop the MySQL Service

To prevent conflicts during restoration, stop the MySQL service.

sudo systemctl stop mysql

6. Restore the Backup

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.

7. Set Correct Ownership

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

8. Start the MySQL Service

Restart the MySQL service to apply the restored database.

sudo systemctl start mysql

 

9. Verify the Restoration

Log into MySQL and verify that the restored data is intact and operational.
mysql -u root -p
SHOW DATABASES;

References

  1. Percona XtraBackup: Prepare Compressed Backup
  2. Percona XtraBackup: Restore a Backup
  3. YouTube Guide: Database Backup and Restore Using XtraBackup

Conclusion

Restoring a database backup from S3 to an EC2 instance with Percona XtraBackup is a reliable and efficient process. By following the steps above, you can ensure a smooth restoration and maintain data consistency in production or testing environments. Blog Pundit: Sandeep Rawat Opstree is an End to End DevOps solution provider

Connect Us

Leave a Reply