Recovery processing of accidentally deleted data in SQL Server 2014 database

1. Preamble

As a program developer or DBA, you often come into contact with the addition, deletion, modification and query operations of the database (taking the SQL Server 2014 database as an example). When executing the delete statement, the records of the data table were accidentally deleted, and the database did not have any backup before .

It is not difficult to retrieve the data in the database restore data processing of the SQL Server database itself, but there are two prerequisites for restoring the data:

  • There is a "full" backup process of the database before the delete data operation.
  • The database recovery mode (Recovery Mode) is the "full" mode.

Obviously, the current situation does not meet the above conditions, and database restore cannot be used to recover data. Need to use third-party tools to retrieve data. The following describes how to use the ApexSQL Log tool to restore data.

The following uses ApexSQL Log 2016 version for description.

2. Introduction to ApexSQL Log

ApexSQL Log is an excellent database recovery software, which basically supports mainstream versions of SQL Server ( click to view the latest release notes , and select the corresponding supported version of ApexSQL Log according to the currently installed version of SQL Server ), and can recover Delete/Update and other misoperations data and generate corresponding scripts.

Official website:

SQL transaction log reader | ApexSQL

download link:

https://www.apexsql.com/zips/ApexSQLLog.exe 

3. Installation tools

After downloading the file, the next step is to install the tool.

Double-click the ApexSQLLog.exe file to open the Setup window. For data recovery only, select the "Install ApexSQL Log" option, as shown below:

 Click "Next" to enter the installation processing state, and the Setup window displays " ApexSQL Log 2016 was successfully installed " to indicate that the installation has been successfully completed, as follows:

Click the "Close" button to directly close the window and open the ApexSQL Log tool.

4. Prepare data

To better implement the data recovery process, create a new database ( LocalTest ) for test processing.

1. Create the LocalTest database in SQL Server.

2. Create a Users table in the LocalTest database with 12 initial records.

Note: The initial data is prepared by yourself.

3. Simulate accidentally deleting data and execute the delete statement.

Next, use the ApexSQL Log 2016 tool to restore the 12 pieces of data that were accidentally deleted .

5. Data recovery

After installing the tool, proceed to data recovery.

1. There is a prerequisite for the tool to restore data: the recovery mode of the database (Recovery Mode) must be " full " mode, and it may not be recoverable in other modes.

2. Double-click to open the "ApexSQL Log" tool, prompting to set which database to connect to (similar to the setting for connecting to SQL Server), as shown in the figure below:

Authentication: There are two authentication methods,  " Windows Authentication " and " SQL Server Authentication ", which are the same as SQL Server login authentication methods, and different methods can be selected according to the actual situation.

3. Click the "Next" button, and Windows will prompt the user to perform authorization control processing. After the authorization is completed, it will enter the " Database connection " window again. This time, click "Next" to enter normally. (I don’t know whether it is the Windows 10 system security verification problem or the software itself needs to authorize the second entry in order to consider the data security problem. The specific reason is Baidu or Google, and I will not delve into it here).

 4. ApexSQL Log is a commercial software that provides an unlimited 14-day free trial of the evaluation version. "Next" When entering the main body of software functions, three options (Evaluate, Activate, Purchase) will be provided. If conditions permit, you can directly purchase the software (the official version will no longer display this window). Select "Evaluate" to enter the evaluation version.

5. Click "Evalute" to enter the "Data Sources" window, and automatically load the transaction log file of the currently connected database (the database LocalTest selected when connecting), or click the "Add file" button to manually add a log file (*.ldf), As shown below:

6. In the next step, provide the choice of the result output method, as shown in the figure below:

 " Open results in grid ": Display the operation log records in the log file that meet the specified conditions in the form of a table, and you can perform " Undo/Redo ", " Create Before-After report " and " Export results " operations on the log records.

" Undo/Redo ": Roll back or repeat the log operation.

" Create Before-After report ": Create a report of before-after operation records, save in SQL, SQL Bulk, XML, HTML or directly into the database.

" Export results ": Export log data to SQL, SQL Bulk, XML, HTML, CSV or directly to the database.

7. In order to view the contents of the log in detail, click " Open results in grid ", enter "Filter setup", and set the filter conditions for loading data, as follows:

Time range : Set the time range for filtering log operation records.

Operations : Set the filter to display the log of the type of operation (DML and DDL).

Tables : Select the data table to output the log content.

8. After setting the log record filter conditions, click the "Finish" button to automatically read the log resources, as shown in the figure below:

Note: There will be a warning statement on the table header: " Trial Version: Only details of every 10th row will be available ". The current version is an evaluation trial version, which limits the display of detailed content and the use of some functions only after every 10th row of records. If you want to check the details of each line of records and use complete functions, it is recommended to purchase the official version.

9. After reading the log file resources, you can see the log records of the previous initial data ( insert ) and data deletion ( delete ) operations in the table, as follows:

On the left side of the table, you can select conditions (Time, DML, Users, and Other) to filter log records according to the actual situation, as follows:

10. Use the delete statement to delete the data by mistake. If you want to restore the data, you can "Undo delete" the operation, and the data will be retrieved. According to this idea, find out the log records of all "delete" operations (set the DML condition to delete to filter out the log records of delete operations through the grid filter on the left). Select all records and click the "Undo" button to create a rollback processing script, as follows:

 11. After the processing is completed, the " Undo script " window is automatically opened to display the SQL script for rollback processing

You can directly click the " Excute " button to execute the script or " Save " to execute locally afterwards.

Execute the script

 Query the Users data table, and you can see that the 12 pieces of data have been restored.

The data recovery process is complete! ! ! !

Note: Execute the " Truncate Table " statement to clear the data, but there is no way to do a rollback operation. Data can only be recovered by redoing (Redo) the operation before the deletion.

6. Summary

Using the ApexSQL Log tool is only the last step to save data, and it is not easy to use tools to recover data of the magnitude of accidental deletion (more than one million). Usually, it is still necessary to do a good job of daily database backup. If conditions permit, you can do DB mirroring or AlwaysOn clustering, so as to avoid the trouble of restoring data after accidentally deleting data.

Note the following points:

  • The data to be recovered is not more than one order of magnitude.
  • The recovery mode of the database ( Recovery Mode ) must be " full " mode.
  • Some functions of the trial version of the tool are limited. If you need to perform complete data recovery processing, use the official version.

Guess you like

Origin blog.csdn.net/LZD_jay/article/details/128874464