How to recover accidentally deleted DB 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) mainly provides cross-sectional support for the infrastructure of our media services, improving existing services, launching new ones, contributing to OSS, etc.
This article is located in SRGDBWGWe will publish materials regarding the database that the (DB Working Group) provides to the entire company.
I hope this helps in some way.
overviewOverall flowprocedureCheck the location of full backup data and backup binlogSee which queries were executed by mistake and whenCheck 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 in serviceRun the recovery query with SET sql_log_bin = 0 and then apply it to all DBsFinally, re-establish 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 mistake, or a DELETE WHERE condition being invalidated due to an application bug, causing all records to be deleted.
For the MySQL services we manage, in addition to daily full backups using Percona XtraBackup, we also back up the binlog files every 5 minutes.
We will show you how to recover using those backup files.
The content of this article mainly applies to those who are running MySQL in an on-premise environment.
The story is a little different for Amazon Aurora, so please refer to the CyberAgent Developers Blog article below.
Overall flow
- Check the location of full backup data and backup binlog
- See which queries were executed by mistake and when
- 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 in service
- Apply recovery queries to all DBs after SET sql_log_bin = 0
- Finally, re-establish the replication
procedure
Check the location of full backup data and backup binlog
Check where you have your backup data stored.
You should always be aware of whether you are uploading to S3 or to a physical server somewhere.
See which queries were executed by mistake and when
Determines the previous execution of any 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 the binlog cannot be read as is, you need to convert it back to SQL format using the mysqlbinlog command before searching for the relevant query.
end_log_pos 286332780
Generate recovery queries from binlog files
Now that we have confirmed the "position from which the backed up was taken" and the "position to which we want to return", we will generate a query to apply the full backup to the restored DB.
This generates the SQL that applies up to just before the DELETE statement mentioned earlier.
If you want to go back to a certain time, rather than going back to a specific query as above, 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 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 xtrabackup data on that slave server
- Once the deployment is complete, copy the DB data in /var/lib/mysql on that 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 normally run the recovery query only on the DB master, it will take even more time for the replication delay to converge, so we will set sql_log_bin = 0 and then run the query on all DBs at the same time.
Finally, re-establish the replication
Once all DB data has been copied, configure replication between the master and slave.
The DB has now been restored to the state it was in just before the specified query was executed.
Conclusion
I think restoring backup data will be an operation that occurs once every few years, if that.
We recommend that you document your recovery methods and practice implementing them on a regular basis so that you can remain calm when an emergency occurs.
SRG is looking for people to work with us. If you are interested, please contact us here.