How to recover accidentally deleted database data to the state just before it was deleted
This is Oniumi (@fat47) from the Service Reliability Group (SRG) of the Technology Headquarters.
#SRG(Service Reliability Group) is a group that mainly provides cross-sectional support for the infrastructure of our media services, improving existing services, launching new ones, and contributing to OSS.
This article is located in SRGDBWGWe will publish materials regarding the database provided by the DB Working Group for the entire company.
I hope this helps in some way.
overviewOverall flowprocedureCheck the location of the full backup data and backup binlogCheck which queries were executed by mistake and for how longCheck the binlog position at the time of the full backup taken before the query was executedCheck the binlog position immediately before the mistakenly executed queryGenerate recovery queries from binlog filesDeploy full backup data to all databases currently in serviceRun the recovery query with SET sql_log_bin = 0 and then apply it to all DBsFinally, rebuild the replicationConclusion
overview
This is a summary of how to recover MySQL to the point just before an operation such as a table drop due to an operational error or a DELETE where condition is not met due to an application bug, causing all records to be deleted.
For the MySQL service we manage, in addition to daily full backups using Percona XtraBackup, we also back up the binlog file every five minutes.
We will show you how to recover using these backup files.
This article mainly covers those who are running MySQL in an on-premise environment.
The story is a little different for Amazon Aurora, so please refer to the article on the CyberAgent Developers Blog below.
Overall flow
- Check the location of the full backup data and backup binlog
- Check which queries were executed by mistake and for how long
- Check the binlog position at the time of the full backup taken before the query was executed
- Check the binlog position immediately before the mistakenly executed query
- Generate recovery queries from binlog files
- Deploy full backup data to all databases currently in service
- Apply recovery queries to all DBs after SET sql_log_bin = 0
- Finally, rebuild the replication
procedure
Check the location of the full backup data and backup binlog
Check where you store your backup data.
You should always be aware of whether you are uploading to S3 or to a physical server somewhere.
Check which queries were executed by mistake and for how long
Determines the previous execution of the query to which data is returned.
Check with the service provider or developer.
Check the binlog position at the time of the full backup taken before the query was executed
xtrabackup_binlog_info
Check the binlog position immediately before the mistakenly executed query
If you want to specify a point immediately before a specific query, you must identify that query from the backed up binlog file.
Since binlog cannot be read as it is, you need to convert it back to SQL format using the mysqlbinlog command and then search for the relevant query.
end_log_pos 286332780
Generate recovery queries from binlog files
Now that we have confirmed the "position at which the backup was taken" and the "position at the time we want to return to," we will generate a query to apply the full backup to the restored DB.
This generates SQL that applies up to just before the DELETE statement.
If you want to go back to a certain time, rather than just going back to a specific query, you can write it like this:
In this case, it is an example of specifying from the full backup data to 13:05 on March 4th.
Deploy full backup data to all databases currently in service
The full backup data from XtraBackup will be deployed to the DB server.
If you have a backup on an external network such as S3, the following method is recommended.
- Download the full backup data from S3 to one of the slave servers
- Extract the extrabackup data to that slave server
- Once the deployment is complete, copy the DB data in /var/lib/mysql on the slave server to all DBs.
Run the recovery query with SET sql_log_bin = 0 and then apply it to all DBs
Once the full backup data has been deployed to all DBs, the SQL extracted from the binary logs will be applied to all DBs.
If you simply run the recovery query on the DB master, it will take even longer for the replication delay to resolve, so we will set sql_log_bin = 0 and then run the query on all DBs at the same time.
Finally, rebuild the replication
Once all DB data has been copied, set up replication between the master and slave.
The database has now been restored to the state it was in just before the specified query was executed.
Conclusion
I think restoring from backup data will be an operation that occurs once every few years, if at all.
We recommend that you document and practice recovery methods on a regular basis so that you can operate calmly in the event of an emergency.
SRG is looking for people to work with us.
If you're interested, please contact us here.