I have created a Stored Procedure that restores my backup files dynamically - sql-server

I am getting one issue though with my log files. I restore my full and any diffs if I have any successfully. However with my Logs I get the following error message. Is this because I would need to take backups of my copy version of my DB.
SET #LogBackupSQL = 'RESTORE LOG '+#DatabaseNameCopy+' FROM DISK = '''+#LogbackupFile+''' WITH REPLACE, FILE = 1, NORECOVERY, NOUNLOAD, STATS = 5,'
error message
The tail of the log for the database "ns_lots_of_vlfs_Copy" has not been backed up. Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.

Related

SSMS - Restore database failed because .mdf cannot be overwritten

I am trying to restore a DB from the backup. Suddenly, it is giving me the following error everytime while trying to restore.
Restore of database 'Something_Secret' failed.
The file 'C:\Some\Path\Something_Secret.mdf' cannot be overwritten. It is being used by database 'Something_Secreter'.
How to get rid this of error?
Don't use the UI for this, since this won't be the last thing it gets wrong for you. Learn the RESTORE DATABASE command.
RESTORE DATABASE Something_Secret_NewName
FROM DISK = 'D:\Baseline\Path\To\Something_Secret.bak'
WITH REPLACE, RECOVERY,
MOVE 'Something_Something_Logical_Primary'
TO 'C:\Program Files\...\Something_Secret_NewName.mdf',
MOVE 'Something_Something_Logical_Log'
TO 'C:\Program Files\...\Something_Secret_NewName.ldf';
If you want more specific guidance, show the output of:
RESTORE FILELISTONLY FROM DISK = 'D:\Baseline\Path\To\Something_Secret.bak';

Script to restore SQL Server database with multiple BAK files

I am trying to come up with a script to dynamically do 'Restore Database' in SQL Server 2019 with multiple BAK files that are located in one folder on a daily basis.
I will do the manual full backup first, and this task will be scheduled to run after on a daily basis.
The plan is go thru each BAK files (located in the same folder) sequentially (for example here: 04 --> 12).
There will be another Python script which will wipe out old files, so I am only focusing on execution of Restore database with BAK files that are located in this folder.
What should be the best approach?
I have attached a screenshot that has all BAK files.
I have some T-SQL code here (that I copied from other thread), but I am not sure how to loop over each BAK file sequentially.
And I am not sure whether I have to deal with mdf and ldf files as listed here.
RESTORE DATABASE [Test] FROM DISK ='\\Folder\LOG...04.bak'
WITH
--what ever options... but likely a file move
MOVE 'data_file_1' TO 'E:\somefolder\data.mdf',
MOVE 'db_log' TO 'E:\somefolder\log.ldf',
REPLACE, --overwrites the database
RECOVERY --sets the DB to READ/WRITE. Use NORECOVERY if you need to restore
logs / differentials
GO
RESTORE LOG Test
FROM '\\Folder\Log...04.bak' --assuming this is a log
WITH FILE = 1, --this is the first log
WITH NORECOVERY; --keep in norecovery to restore other logs...
GO

Duplicate a Database in SQL Server 2014

I have a live database. I need to create a test database with all the data from the live one, but I've encoutneered an error. I've tried to create this new one by restoring from a "live database .bak", but an error message appears saying that could not do the process because the database is in use. Also, I don't know why but after that, the live database starts restoring and keep on it until getting stuck. It does not finish restoring and I have to force stop, deleting the live database and restoring from scratch. Checking the activity log it seems that it goes to restoring mode after failing to create a backup of the database log and that's what happens right after I try to create the new test database using the .bak.
What am I doing wrong? Is there another way to duplicate the live database to use it as a test one?
Note: I've done the process when nobody is using the DB or connected to it but no success.
You need to relocate/rename the Files. Just changing the Database name that you are restoring to may not be changing the Database File and Log File that SQL is trying to actually create when you perform the restore. Go to the Option page of the Restore Database and ensure the files are pointing to Valid locations and not trying to utilize the same files as the Live database.
--At First take ypur database backup and restore it as DatabaseName_Test and also change Datafiles As database _test.mdf and DatabaseName_test_log.ldf
--try to follow as below script
USE master
GO
BACKUP DATABASE [DatabaseName]
TO DISK = N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\Backup\DatabaseName.bak'
WITH COPY_ONLY, NOFORMAT, NOINIT,
NAME = N'DatabaseName-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
USE [master]
RESTORE DATABASE [DatabaseName_Test] FROM
DISK = N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\Backup\DatabaseName.bak'
WITH FILE = 2,
MOVE N'DatabaseName' TO N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\DATA\DatabaseName_test.mdf',
MOVE N'DatabaseName_log' TO N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\DATA\DatabaseName_test_log.ldf', NOUNLOAD, STATS = 5
GO

Differential Backups in SQL Server 2008; Able to restore through SSMS but unable to restore through Transact SQL

I am facing an issue while restoring the databases from differential backups. Here are the steps I performed
DROP DATABASE DBName_delta
GO
BACKUP DATABASE DBName TO DISK = 'E:\Fullbak1.bak'
GO
RESTORE FILELISTONLY FROM DISK = 'E:\Fullbak.bak'
GO
RESTORE DATABASE DBName_delta
FROM DISK='E:\Fullbak.bak'
WITH MOVE 'DBName_Data' TO 'E:\DBData\DBName_delta.mdf',
MOVE 'DBName_Image_Data' TO 'E:\DBData\DBName_delta_Image_Data.mdf',
MOVE 'DBName_Log' TO 'D:\DBLog\DBName_delta.ldf',
NORECOVERY
--Made Some changes in the database
BACKUP DATABASE DBName
TO DISK = 'E:\DiffBak1.TRN'
WITH DIFFERENTIAL
GO
--Made Some more changes in the database
BACKUP DATABASE DBName
TO DISK = 'E:\DiffBak2.TRN'
WITH DIFFERENTIAL
GO
RESTORE FILELISTONLY FROM DISK = 'E:\DiffBak1.TRN'
GO
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak1.TRN' WITH NORECOVERY
GO
Msg 4305, Level 16, State 1, Line 2
The log in this backup set begins at LSN 81125000000059600297, which is too recent to apply to the database. An earlier log backup that includes LSN 81121000000116200001 can be restored.
Msg 3013, Level 16, State 1, Line 2
RESTORE LOG is terminating abnormally.
RESTORE FILELISTONLY FROM DISK = 'E:\DiffBak2.TRN'
GO
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak2.TRN' WITH STANDBY = 'c:\undo.ldf'
GO
But when I tried to restore the same E:\DiffBak1.TRN through SSMS by using the WITH NORECOVERY option, it restored the database and again I was able to perform the restore of the same file using the Transact SQL. Am I missing something here? Is this something to do with the RESTORE DATABASE? I am sure that we are not missing any of the logs in between. Any help will be much appreciated.
I am able to figure this out. As I mentioned, I was able to restore the differential database through the SSMS wizard, I took the script from the wizard and found out the differences in the query.
I was using the below code
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak1.TRN' WITH NORECOVERY
GO
I have changed the code to
RESTORE DATABASE [DBrel02t_delta] FROM DISK = N'E:\DiffBak1.TRN' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10
GO
This resolved the issue. Use the below query to find out the lsn details of your backup.
RESTORE HEADERONLY FROM DISK = Nā€™<backup file>ā€™;

restore database from transaction log problem

I'm following instructions here to save database state and restore it from any state i previously saved. I cannot however get the RESTORE given in the example there working. I always get a message
This log file contains records logged
before the designated mark. The
database is being left in the
Restoring state so you can apply
another log file.
I think the problem is with the FILE version, but from what I see in the example, it should be N in DB and N-1 in Log, and that's how I always set it. Any help would be appreciated.
It sounds like you need to apply more transaction logs. You have to first restore the database with a full backup to some point in the past. You may then restore zero or more differential backups (in order) to skip forward as far as possible. Finally, you have to restore every transaction log that has log records beginning from the start of the most recently restored backup (full or differential) all the way up to the restore time in question.
RESTORE DATABASE Blah FROM DISK = 'blah 201105210000.bak' WITH NORECOVERY;
RESTORE LOG Blah_Log FROM DISK = 'blah 201105210100.trn' WITH NORECOVERY;
RESTORE LOG Blah_Log FROM DISK = 'blah 201105210200.trn' WITH NORECOVERY;
RESTORE LOG Blah_Log FROM DISK = 'blah 201105210300.trn' WITH NORECOVERY;
RESTORE LOG Blah_Log FROM DISK = 'blah 201105210400.trn' WITH NORECOVERY;
RESTORE LOG Blah_Log FROM DISK = 'blah 201105210500.trn' WITH STOPAT 'x', RECOVERY;
My syntax may not be perfect but the concept is solid, you have to restore all the transaction logs in order. If you pick a t-log that is too early you get a message about that, or if you pick one that is too late you'll get a different message. The message you posted indicates to me that the t-log you're trying to apply has log records entirely before the transaction id of the last restore applied. Find more t-logs from after that.
If t-logs shouldn't be missing but are, look in the SQL jobs or maintenance plans to see if you have two t-log backups going on different schedules. If this was happening, you must collect all the t-logs from both the backups as only all together can they be a proper and unbroken chain that will let you restore up to the point in time you want.
I couldn't get the example to work either unless I changed the last statement to the name of the transaction, rather than the description. (SQL Server Express 2017)
RESTORE LOG AdventureWorks2012
FROM AdventureWorksBackups
WITH FILE = 4,
RECOVERY,
STOPATMARK = 'ListPriceUpdate';
When the STOPAT argument is specified, SQL Server (2008 Express at least) appears to assume the NORECOVERY option even when RECOVERY is specified. I assume that's why it generates the message (not an actual error, correct?). To complete the operation, an additional step may be added to the process.
...
RESTORE LOG Blah_Log FROM DISK = 'blah 201105210500.trn' WITH STOPAT 'x' WITH NORECOVERY;
RESTORE DATABASE Blah_Log WITH RECOVERY;

Resources