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

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>ā€™;

Related

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

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.

Restoring database from script

I have created a small scrip to restore databases from backups. Once the script is run it displays
RESTORE DATABASE successfully processed 28818 pages in 1.568 seconds
(143.584 MB/sec).
I have more code to alter the database, alter a few views and sp too but I am getting the following error; User does not have permission to alter database 'GreyGoo', the database does not exist, or the database is not in a state that allows access checks.
I have noticed too that I cannot see the database in the object explorer
this is what I use to restore the DB from a backup
if DB exists set to a single user
if DB exist drop database
Ran the below script
RESTORE DATABASE GreyGoo FROM DISK = 'C:\Bkp\GreyGoo_backup_2020_03_02_180002_5403592.bak'
WITH
MOVE 'GreyGoo' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.mdf',
MOVE 'GreyGoo_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.ldf',
REPLACE;
set DB to multi-users and other properties
So what is the problem and how can I solve it, I am currently testing my code on SQL Server 2008
Thanks
During your restore process, you will need to ensure your
login / permissions are correct.
if another previous database with same name is not there, if so delete it. Perhaps from a previous attempt
You can also turn off recovery option like below, recovery is default
RESTORE FILELISTONLY
Sample -- MSDN
USE master;
GO
-- First determine the number and names of the files in the backup.
-- AdventureWorks2012_Backup is the name of the backup device.
RESTORE FILELISTONLY
FROM AdventureWorks2012_Backup;
-- Restore the files for MyAdvWorks.
RESTORE DATABASE MyAdvWorks
FROM AdventureWorks2012_Backup
WITH RECOVERY,
MOVE 'AdventureWorks2012_Data' TO 'D:\MyData\MyAdvWorks_Data.mdf',
MOVE 'AdventureWorks2012_Log' TO 'F:\MyLog\MyAdvWorks_Log.ldf';
GO
EXEC msdb.dbo.sp_delete_database_backuphistory #database_name = N'MYDB'
GO
USE [master]
GO
/* Database [MYDB] */
DROP DATABASE [MYDB]
GO
USE [master]
RESTORE DATABASE [MYDB] FROM DISK = N'E:\FTP\local_MYDB_01.bak' WITH FILE = 1, MOVE N'blank' TO N'E:\DBs\2019\MYDB.mdf', MOVE N'blank_log' TO N'E:\DBs\2019\MYDB.ldf', NOUNLOAD, STATS = 5
GO

Need to Backup and Restore a database

I need to create a script that when I execute it. It will backup a production database (keep in mind this is a live database) and restore it as development database(if exists overwrite)
I have tried this query but getting error message. I need to find a way to do this without taking the database offline or single user mode
USE [master]
RESTORE DATABASE [Development] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\Production.bak'
WITH FILE = 6, MOVE N'Producation' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Development.mdf',
MOVE N'Production_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Development_log.ldf', NOUNLOAD, REPLACE, STATS = 5
Msg 3101, Level 16, State 1, Line 67
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 67
RESTORE DATABASE is terminating abnormally.
I realized I do not need to keep the development online and can kill connections.
so I have used the ALTER DATABASE [ALI] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO to resolve this error

Cannot Backup and Restore using SQLCMD

I'm trying to restore a database from a backup.
The BACKUP sqlcmd I'm using is the following
sqlcmd -e -s [SERVERNAME] -q "backup database [DATABASE_NAME] to disk ='[BackupLocation]'"
For the RESTORE, I'm using the following sqlcmd for restore.
sqlcmd -e -s [SERVERNAME] -q "restore database [DATABASENAME] from disk ='[BackUpLocation]"
When i try to restore the database I get this error.
Msg 3159, Level 16, State 1. Server [SERVERNAME], Line 1
The Tail of the log for the database "[DATABASE_NAME]" has note been backed up. Use BACKUP
LOG WITH NO RECOVERY or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.
Msg 3013, Level 16, State 1, Server [SERVERNAME], Line 1
RESTORE DATABASE is terminating abnormally.
I'm trying to incorporate the tail log backup but I can't seem to find a way, I keep getting errors like the one below. I already added WITH NORECOVERY at the end of the backup sqlcmd, but had no luck.
Msg 3032, Level 16, State 2, Server [SERVERNAME], Line 1
One or more of the options are not supported for this statement. Review the documentation for supported options.
Does anyone know how can I accomplish this task? Backing up and restoring successful
You have to backup the log with norecovery to get a tail log backup. So, backup log [yourDatabase] to disk='your log file' with norecovery. Alternatively, if you're just playing around (or are legitimately replacing your database, just add with replace to the restore command.

Warm SQL Backup

We have a warm sql backup. full backup nightly, txn logs shipped every so often during the day and restored. I need to move the data files to another disk. These DB's are in a "warm backup" state (such that I can't unmark them as read-only - "Error 5063: Database '<dbname>' is in warm standby. A warm-standby database is read-only.
") and am worried about detaching and re-attaching.
How do we obtain the "warm backup" status after detach/attach operations are complete?
The only solution I know is to create a complete backup of your active database and restore this backup to a copy of the database in a 'warm backup' state. First create a backup from the active db:
backup database activedb to disk='somefile'
Then restore the backup on another sql server. If needed you can use the WITH REPLACE option to change the default storage directory
restore database warmbackup from disk='somefile'
with norecovery, replace ....
Now you can create backups of the logs and restore them to the warmbackup with the restore log statement.
It looks like you didn't complete the restore task , just do the restore task only for the TRANSACTOINAL LOG .Then it will be fine immediately when you finish that.

Resources