Summary: This blog talks about the possible solutions to perform maximum recovery of data after SQL database corruption. You can try to restore the database from backup or run DBCC CHECKDB command with repair options to fix database corruption. If nothing works, using a SQL recovery tool can help.
The Job role and responsibility of DBA is demanding and Challenging; he has to look after day to day critical performance of Database and, when it comes to performance issue then, it is the responsibility of the DBA to perform maximum recovery after SQL database corruption.
There are several reasons that can cause database corruption. “Osiris” for example is a Ransomware, used to attack systems including databases. It is hard to prevent all the possible attacks by hackers or/and viruses. Hardware failure is another reason for database corruption, and when business day to day transaction is totally dependent on your database then, it could hamper the complete business.
What needs to be done in such a cryptic situation?
My advice is to face the problem and try the following possible solutions:
Fix 1: Verify if you have a backup available
If you have a backup available, you can use it to restore the database. The problem with the backup is that you may lose some data according to the backup date. If the last backup was made 3 days ago, you would lose the data of the last 3 days. It is possible to recover data in a specific hour if you take a transaction log backup. However, if your database is corrupt, it will not be possible. That is why it is necessary to program regular backups. You can use a combination of transaction log backups, full backups and differential backups if necessary.
Try to test if your backup works. You have an option “Verify only “ to verify if the backup is working fine.
RESTORE VERIFYONLY FROM DISK = C:\AdventureWorks.BAK GO
Backup is your preparation against data loss, and it should be protected and taken at the highest priority, Every DBA is aware of some prerequisite while dealing with database backup plan. Keeping Backup at a different location is good practice also, in many cases we have experienced that virus attack on database and backup as well. So we have to be extra cautious in such scenarios.
Fix 2: Repair a database using inbuilt options
If you do not have a current database backup or if your backup is damaged, the second alternative would be to repair the database. Restoring the database may take longer if your database has several TB in size or it will perform fast if it is just a database with few MB.
You can use the DBCC command in SSMS or any other SQL Server tool of your preference. This command will show you the error messages. If your database is big, you can use the TABLOCK option or NO_INFOMSGS to run the command faster. However, the access to the database will be slower because a lock will be produced by the command.
If you suspect that just some database is corrupt, you can use the DBCC CHECKTABLE to repair specific tables.
DBCC CHECKDB command uses a lot of space in the TEMPDB database, so it is advisable to use the option ESTIMATEONLY to estimate the space required on the TEMPDB especially when the database is big.
DBCC can be used to repair your database using different options. One of the options is REPAIR_BUILD and, if it gets fail then, you can use the REPAIR_ALLOW_DATA_LOSS option, that will repair but lose some data if necessary.
DBCC CHECKDB ('databasename', REPAIR_ALLOW_DATA_LOSS)
Fix 3: Repair SQL database using SQL recovery tool
If you do not have a backup or the DBCC CHECKDB cannot repair your database, you can use the Stellar Repair for MS SQL. This software will repair the corrupt SQL database.
The software recommends stopping the SQL Server service and copies the database to a different location.
You will need to know the location of your MDF file to repair. The Find button allows searching for the MDF file.
Once the data file is selected, you need to press the Repair button to start the repairing process. This software repairs the complete database and provides the facility to save in four different formats.
Video Guide: How to Use Stellar Repair for MS SQL
Repairing a Database is always a challenging task for any DBA, But it can be possible if we can more explore in this concern area. In this article, we show different options to restore or repair a database in cases of disasters. We mentioned the backup and restored strategy as the first option, use the DBCC commands as a second option and finally we have mentioned the Stellar Repair for MS SQL. This software can easily repair your database in an intuitive way and to which you can rely on!!