Recover deleted record in SQL Server - sql-server

I accidentally deleted a row in a table and want to restore it. I've found a solution here: How to recover deleted records in MS SQL server
I've tried to restore database from backup taken after delete. But I can't restore database with STOPAT option:
RESTORE LOG database FROM DISK = N'X:\database.BAK' WITH
STOPAT = N'2011-02-12T00:00:00', RECOVERY
I have following error:
Msg 3117, Level 16, State 4, Line 1
The log or differential backup cannot be restored because no files are
ready to rollforward.
Msg 3013, Level 16, State 1, Line 1
RESTORE LOG is terminating abnormally.

I've tried to restore database from
backup taken after delete.
You cannot recover deleted records from a backup taken after the delete. You need:
the latest full backup taken before the delete
all the log backups taken between the last full backup until the first log backup taken one after the delete
the database must be in full recovery mode
You may have an differential backup thrown in to reduce the log backup chain length, but this is optional.
Only if all the conditions above are satisfied, then you can go ahead and follow the procedure. If you are missing any of the above, then that record is lost. As a rule of thumb, don't follow blog articles or forum answers, including this one, follow instead the product documentation: How to: Restore to a Point in Time (Transact-SQL)

If your database was in full recovery mode then you can try to read transaction log and recover data from there using commercial tool such as ApexSQL Log or sql server commands such as DBCC LOG or fn_log.
Also you can try looking at these posts for more details:
How to undo a delete operation in SQL Server 2005?
How to view transaction logs in SQL Server 2008

Taken from this link -
A differential backup, though only containing changed data pages since the last full database backup, can only be restored in conjuntion with a full backup similar to using log backups. You don't have the ability to restore only single tables or objects, but you can restore at the database, filegroup, file, and page level. You can also restore up to a particular point in time from any of the backups (assuming you've restored the required preceding full, diff, logs).
Possibly the best solution for you in this case would be to restore the database using a different database name, then moving only the data you want into your existing database? Not sure if that's exactly what you're trying to achieve though.

Yes, it's possible to recover deleted records from SQL Server database using various methods, which, on the other hand, requires different prerequisites. Some of them involve native SQL Server features, others 3rd party tools.
Anyway, in some cases the deleted data is not lost even your database is not using the Full recovery model. For more information, check the Recover deleted SQL data from a backup or from online database files online article.
Disclaimer: I work as a Product Support Engineer at ApexSQL

Related

How to restore SQL Server backup to earlier time than latest available?

Is it possible to restore a SQL Server .bak file to any time earlier than the time of last backup taken?
I can successfully restore a .bak file to the last backup taken. That looks like this. The backup set automatically appears.
However, I want to restore to a point in time before the latest available (to a time before an accidental deletion of some item in that database). So I select Timeline and set as follows. The inverted triangle is the only one in the entire timeline.
But then, I can't restore, I get an error
No backupset selected to be restored
I'm running SSMS as Administrator, using SQL Server 2017 Developer edition here. The original database was on SQL Server 2012.
Below the response from documentation:
A specified time is always restored from a log backup. In every
RESTORE LOG statement of the restore sequence, you must specify your
target time or transaction in an identical STOPAT clause. As a
prerequisite to a point-in-time restore, you must first restore a full
database backup whose end point is earlier than your target restore
time. That full database backup can be older than the most recent full
database backup as long as you then restore every subsequent log
backup, up to and including the log backup that contains your target
point in time.
For more details look here

Transaction Log Restore: System objects could not be updated in database because it is read-only

I want to set up a secundary copy of a large ERP production database, which would be used as read-only database for reporting purposes. Regular backup jobs on production DB are already configured: a full backup every night and regular transaction log backups every 15 minutes.
I'm restoring a full backup and then applying transaction log backups (with STANDBY option). The restore of transaction log is successful, but I always get a message at the end that the system objects could not be updated:
Processed 7 pages for database ..., file ... on file 1.
System objects could not be updated in database ... because it is read-only.
System objects could not be updated in database ... because it is read-only.
RESTORE LOG successfully processed 7 pages in 0.029 seconds (1.633 MB/sec).
It seems that the restore is successful. However, the warning (error?) regarding the system objects is confusing. Is it something I should pay attention to? What is the reason for the error? I couldn't find any similar problem on the Microsoft support forums...
Update: SQL Server version is 11.0.7469.6 (2012 with all service packs and hotfixes)
If you don't mind I'll suggest you a workaround(There might be a solution)
step 1. Restore the full backup to a new database and ensure the backup is successful(by accessing some objects).
step 2. restore the transaction backups 2(some count let say 3 or some number) at a time and make db to recovery state then query the same objects which were done at step 1.
step 3. repeat the process until all the backups were restored successfully.
Also check the following link, might work.

I have a SQL Server file backup file (.bak) i want to get detail of each transaction

I have a SQL Server backup (.bak) file, and I want use fn_dump_dblog (undocumented function) on it to get all transaction history from it.
I read this article https://www.mssqltips.com/sqlservertip/3555/read-sql-server-transaction-log-backups-to-find-when-transactions-occurred/
and do the same but fn_dump_dblog result only shows query of restoring of that database (most of the rows are NULL)
Please help me - what am I doing wrong? Or there is any alternative way to do that?
Note: I am taking backup in full mode
I already tried fn_dblog but that does not help me
I can not use fn_dblog or fn_dump_dblog on database directly so I am taking backup (.bak file) every day and then restore it to my local system.
can i get transaction history if i have .trn file ?
You can't. A database backup only contains a minimal amount of log records, see How much transaction log a backup includes. If there was no other activity, then the only transaction captured would be the BACKUP itself, as you see.
You did not specify if is a database backup or a log backup, I made an educated guess that you have a database backup. A log backup would contain only log records, but log backup implies a recovery plan and a log chain, you would have mentioned if they were in place.

Restore SQL Server DB without transaction log

Given a SQL Server 2008 .bak file, is there a way to restore the data file only from the .bak file, without the transaction log?
The reason I'm asking is that the transaction log file size of this database is huge - exceeding the disc space I have readily available.
I have no interest in the transaction log, and no interest in any uncompleted transactions. Normally I would simply shrink the log to zero, once I've restored the database. But that doesn't help when I have insufficient disc space to create the log in the first place.
What I need is a way to tell SQL Server to restore only the data from the .bak file, not the transaction log. Is there any way to do that?
Note that I have no control over the generation of the .bak file - it's from an external source. Shrinking the transaction log before generating the .bak file is not an option.
The transaction log is an integral part of the backup. You can't tell SQL Server to ignore the transaction log, because there is no way to let's say restore and shrink the transaction log file at the same time. However, you can take a look at DBA post to hack the process, although it is not recommended at all
Alternatively you could try some third party tools for restoring, particularly virtual restoring process that can save a lot of space and time. Check out ApexSQL Restore, RedGate Virtual Restore, Idera Virtual Database.
Disclaimer: I work for ApexSQL as support engineer
No, the transaction log is required.
Option 1:
An option may be to restore it to a machine that you DO have enough space on. Then on the restored copy change the logging to either bulk logged or simple, shrink the logs, do another backup operation on this new copy and then use that to restore to the target machine with the now much smaller transaction log.
Option 2:
Alternatively, perhaps the contact at the external source could shrink the transaction log before sending it to you (this may not work if the log is large due to a lot of big transactions).
Docs on the command to shrink the log file are available here.
This is really a question for the ServerFault or DBA sites, but the short answer is no, you can only restore the full .bak file (leaving aside 'exotic' scenarios such as filegroup or piecemeal restores). You don't say what "huge" means, but disk space is cheap; if adding more really isn't an option then you need to find an alternative way of getting the data from your external source.
This may not work since you have no control over the generation of the .bak file, but if you could convince your source to detach the database and then send you a copy of the .mdf file directly, you could then attach the .mdf and your server would automatically create a new empty transaction log file.
See sp_detach_db and sp_attach_db (or CREATE DATABASE database_name FOR ATTACH depending on your sql server version).
I know this is an old thread now, but i stumbled across it while I was having transactional log corruption issues, here is how I got around it without any data loss (I did have down time though!)
Here is what I did:--
Stop the sql server instance service
make a copy of the affected database .mdf file and .ldf file (if you have an .ndf file, copy that as well!) - Just to be sure, you can always put these back if it doesn't work for you.
restart the service.
Log into sql management studio and change the database mode to simple, then take a full backup.
Change the database type back again and once again take a full backup, then take a transactional log backup.
Detach the database.
Right click on databases and click on restore, select the database name from the drop down list, select the later full database backup created (not the one taken from the simple mode) and also select the transactional log backup.
Click restore and it should put it all back without any corruption in the log files.
This worked for me with no errors and my backups all worked correctly afterwards and there were no more transactional log errors.

SQL Server LOG Data recovery

I was wondering how you can recover data from your logfile.
I'm using sql 2005 (full backup).
The problem is that a service cleared my data last night (which it shouldn't have).
and now I want to recover those rows that were deleted before.
can anyone tell me how I can do this?
As long as you have a backup of your database from before the delete and then all transaction log backups that have been made since the last database backup then you will be able to restore to a point in time.
The first thing to do is take a backup of the transaction log.
Then you restore your last database backup and all transaction log backups since then up to the point in time just before the delete.
See this MSDN Article on how to do it.
I would suggest that you leave your existing database in place as it is and restore the backups to a new database. Then you can write some scripts to transfer the required data back into your live database.
First of all, your database must be in the full recovery model and you must have a full chain of transaction log backups - a series of log records having an unbroken sequence of log sequence numbers (LSNs)
The log backup chain is started when a full database backup is made or the recovery model is switched from SIMPLE to FULL and a full backup is made. After that, transaction log backups are created on regular basis. The log backup chain can be broken only in two ways:
Overwriting the backup set
Switching from FULL to SIMPLE or BULK LOGGED recovery models
Breaking the log backup chain can lead to missing transaction information
You can restore to a point in time using:
SQL Server Management Studio, as shown in the link Robin provided
Using T-SQL and the STOPAT option
Syntax
RESTORE LOG database_name
FROM <backup_device>
WITH STOPAT = time, RECOVERY…
Use a third party tool, such as ApexSQL Log which can not only restore to a specific point in time, but selectively roll back only the transdactions you selected
You can find the steps for all listed options here: Restore a database to a point in time
Disclaimer: I work for ApexSQL as a Support engineer
Your data can be recovered only in case:
1) database uses full recovery model;
2) you have full backup that you make before accidental deleting;
3) you have have NOT recover or backup again this database yet.
If this is correct, you should:
1) make transaction log backup;
2) restore database from full backup WITH NORECOVERY option;
3) restore transaction log using STOPAT option.
To restore transaction log files to a point-in-time your database must run under full recovery model. So firstly you have to restore the latest full database backup:
RESTORE DATABASE *database* FROM DISK = 'D:/Full.bak' WITH NORECOVERY, REPLACE
The next step is to restore the last differential database backup:
RESTORE DATABASE *database* FROM DISK = 'D:/Diff.bak' WITH NORECOVERY
And then restore all transaction log backups that have made since last differential backup in correct sequence
RESTORE LOG *database* FROM DISK = 'D:/log1.bak' WITH NORECOVERY
RESTORE LOG *database* FROM DISK = 'D:/log2.bak' WITH NORECOVERY
RESTORE LOG *database* FROM DISK = 'D:/log3.bak' WITH NORECOVER
The last one transaction log backup that must be restored is the transaction log backup that have been made after the failure occurred with stopat option. After stopat option, you should set up the time to which you want to restore your database.
RESTORE LOG *database* FROM DISK = 'D:/log4.bak' WITH STOPAT = '2015-11-26 16:22:40.000', RECOVERY

Resources