Quantcast
Channel: SQL Backup Archives - SQL Authority with Pinal Dave
Viewing all 110 articles
Browse latest View live

How to Add Date to Database Backup Filename? – Interview Question of the Week #109

$
0
0

How to Add Date to Database Backup Filename? - Interview Question of the Week #109 backupicon Question: How to Add Date to Database Backup Filename?

Answer: Many times, there are comments on my earlier blogs which are a source of new blogs. This blog is a side effect of one of such interaction.

Here are a few blogs which I wrote about backup to Azure storage using Backup to URL command.

SQL SERVER – Steps to Backup to Windows Azure storage

SQL SERVER – Backup to Azure Blob using SQL Server 2014 Management Studio

Here is the comment which inspired me for this blog

<BEGIN – Comment>

I am trying to take backup to a URL but how can I generate a new file name? Getting below error

Msg 3271, Level 16, State 1, Line 2
A nonrecoverable I/O error occurred on file “https://mycblobstorage.blob.core.windows.net/sqlbackup/master.bak:” The file https://mycblobstorage.blob.core.windows.net/sqlbackup/master.bak exists on the remote endpoint, and WITH FORMAT was not specified. Backup cannot proceed..
Msg 3013, Level 16, State 1, Line 2
BACKUP DATABASE is terminating abnormally.

<END – Comment>

If we use WITH FORMAT then it would overwrite the backup. So, here is the solution I have provided via email to him.

DECLARE @BackupName varchar(100)
DECLARE @BackupFile varchar(100)
DECLARE @dateTime NVARCHAR(20)
DECLARE @backupLocation nvarchar(200)
DECLARE @DBNAME VARCHAR(200)
DECLARE @URL VARCHAR(MAX)
DECLARE @sqlCommand NVARCHAR(1000)
SET @DBNAME = 'MASTER'
SET @dateTime = REPLACE(CONVERT(VARCHAR, GETDATE(),101),'/','') + '_' +  REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','')
SET @BackupFile = +REPLACE(REPLACE(@DBNAME, '[',''),']','')+ '_FULL_'+ @dateTime+ '.BAK'
SET @URL =  'https://SQLAuthority.blob.core.windows.net/sqlbackup/' + @BackupFile
SET @sqlCommand = 'BACKUP DATABASE ' +@DBNAME+  ' TO URL = '''+@URL+ ''' WITH COMPRESSION, NOSKIP, NOFORMAT,CREDENTIAL = ' + '''SQLAuthCred'''
EXEC(@sqlCommand)

Every time we would run the backup, it would append the date and time to the backup file so that new have new file name every time. He scheduled this in a SQL Agent job which was running every day and taking backup successfully.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on How to Add Date to Database Backup Filename? – Interview Question of the Week #109


SQL SERVER – Powershell Script – Remove Old SQL Database Backup Files from Azure Storage

$
0
0

SQL SERVER - Powershell Script - Remove Old SQL Database Backup Files from Azure Storage backupicon Few days back I wrote blog to help my blog reader and this is follow up on the same post How to Add Date to Database Backup Filename? – Interview Question of the Week #109. In this blog post we will see a powershell script for Removing Old SQL Database Backup Files from Azure Storage.

So, I added SQL Agent job in my SQL Server and forgot it. Later I realized that I am consuming space on my Azure storage and I need to clean up my old backups. If they were on disk, I would have created maintenance plan to clean them up, but since they are in Azure storage now, I need to write a PowerShell script to clean them up.

SOLUTION/WORKAROUND

Here is what I came up with.

$HowOldFile = [DateTime]::UtcNow.AddHours(-72)
$StorageAccountName = "<StorageAccountName>"
$StorageAccountKey = "StorageAccountKey>"
$Containername = "<ContainerName>"
$extension = "*.bak"
$Storagecontext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzureStorageBlob -Container $Containername -Context $context | Where-Object { $_.LastModified.UtcDateTime -lt $HowOldFile -and $_.BlobType -eq "PageBlob" -and $_.Name -like $extension} | Remove-AzureStorageBlob

You need to fill the parameters based on your configuration.

Notes

  1. To run above, you need to have Azure PowerShell cmdlets installed.
  2. It can be run from anywhere, not necessarily SQL Server machine because it has nothing to do with SQL Server.
  3. In the above script, I am checking the LastModified date for each blob with extension with *. back and deleting those which are 72 hours older. You need to change the extension and time.
  4. When you copy paste from a blog, it might not parse correctly, so take extra care for special characters.

Do you have any better script which can do the same? Please share via comments to help others.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Powershell Script – Remove Old SQL Database Backup Files from Azure Storage

SQL SERVER – Cannot open backup device. Operating system error 1326 (Logon failure: unknown user name or bad password.)

$
0
0

There might be various reasons for above error, but this case is very specific to a situation where SQL Server was installed on a machine which was in Workgroup whereas the backup destination was the server located in the domain.  Here is the error message seen in SQL Agent job history. Let us learn about how to fix Operating system error 1326.

Executed as user: NT Service\SQLSERVERAGENT. Microsoft (R) SQL Server Execute Package Utility Version 11.0.2100.60 for 64-bit Copyright (C) Microsoft Corporation. All rights reserved.
Started: 12:23:24 PM Progress: 2016-12-27 12:23:25.00 Source: {229AB1E3-E7E5-4185-B305-BF0F7657122A}
Executing query “DECLARE @Guid UNIQUEIDENTIFIER EXECUTE msdb..sp…”.: 100% complete End Progress Error: 2016-12-27 12:23:25.35 Code: 0xC002F210 Source: Back Up Database Task Execute SQL Task
Description: Executing the query “BACKUP DATABASE [master] TO DISK = N’\\192.168.3….” failed with the following error: “Cannot open backup device ‘\\192.168.3.55\backupShare\master_backup_2016_12_27_122325_2407734.bak’. Operating system error 1326(Logon failure: unknown user name or bad password.). BACKUP DATABASE is terminating abnormally.”. Possible failure reasons: Problems with the query, “ResultSet” property not set correctly, parameters not set correctly, or connection not established correctly. End Error

Generally, when the backup is taken SQL Agent Service account is used to take backup. In this case since SQL was on Workgroup, it was NT Service\SQLSERVERAGENT. This account is not a valid account to validate for a domain.

SQL SERVER - Cannot open backup device. Operating system error 1326 (Logon failure: unknown user name or bad password.) bad-password-800x185

VERIFICAITON

First, let’s verify that we are hitting the same issue which I experienced. From Workgroup machine, we would try to query remote share using xp_cmdshell. Here is the command.

xp_cmdshell 'dir \\192.168.3.55\backupShare'

This command should fail with below error.

Logon failure: unknown user name or bad password.

SOLUTION

Since we are talking about authentication issue. So, we need to make sure that we map a drive to SQL Server (not windows). As per SQL Server Books-On-Line:

“For a network share to be visible to SQL Server, the share must be mapped as a network drive in the session in which SQL Server is running”

Here is the command to map the drive.

EXEC XP_CMDSHELL 'net use Z: \\192.168.3.55\backupShare password@123 /User:domain\user'

Once the above is completely successful, we should be able to see content on the Z drive by running below command.

EXEC XP_CMDSHELL 'dir Z:\'

After this we should be able to take backup on the Z drive as its mapped within SQL Server as a drive.

Backup database master to disk = 'Z:\master.bak'

The above command should work and create the backup file in \\192.168.3.55\backupShare

Have you encountered this error earlier? Did you find this solution interesting? Please comment and let me know.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Cannot open backup device. Operating system error 1326 (Logon failure: unknown user name or bad password.)

SQL SERVER – FIX : Msg 35295, Level 16 – Log Backup For Database ‘DBName’ on a Secondary Replica Failed

$
0
0

Once, while doing a load testing with my client, I was shown below error while taking log backups on secondary replica.

Msg 35295, Level 16, State 1, Line 1
Log backup for database “MyAppDB” on a secondary replica failed because the last backup LSN (0x0016ff11:00032258:0001) from the primary database is greater than the current local redo LSN (0x0016ffec:0006ee28:0001). No log records need to be backed up at this time. Retry the log-backup operation later.
Msg 3013, Level 16, State 1, Line 1
BACKUP LOG is terminating abnormally.

This error was interesting because as per documentation we can take backup on the secondary replica in AlwaysOn configuration. I quickly checked AlwaysOn Dashboard from Management studio, all were looking green and databases were synchronized. Then I checked transaction log file (LDF) size and it was very big which made me think in different directions. Went to dashboard again and added Redo queue size. We found that REDO thread was blocked by a user query. As per Sys.dm_exec_requests we found that SPID 28 (which was showing DB STARTUP) was blocked by a user SPID 255. Then we used conventional way and found that SPID 255 was running SELECT INTO query for many hours. This was from a job which had a complex join with huge tables.

WORKAROUND/SOLUTION

If you find the same error while taking log backups on secondary replica, then check REDO THREAD and check if its blocked. Based on criticality you need to take a decision to KILL the SPID which is blocking system SPID.

As soon as we killed user query, the “Recovery Queue” for that database started coming down. Once it came down to zero, we could take a transaction log backup on the secondary.

SQL SERVER - FIX : Msg 35295, Level 16 - Log Backup For Database 'DBName' on a Secondary Replica Failed bos-fail-01-800x379

Have you seen REDO blocked in production environment? Do you monitor this by monitoring tool?

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – FIX : Msg 35295, Level 16 – Log Backup For Database ‘DBName’ on a Secondary Replica Failed

How to Get Status of Running Backup and Restore in SQL Server? – Interview Question of the Week #113

$
0
0

Question: How to Get Status of Running Backup and Restore in SQL Server?

How to Get Status of Running Backup and Restore in SQL Server? - Interview Question of the Week #113 runningbackup-800x362

Answer: One of the reasons I started my blog long ago was to have a repository of scripts which can be very useful by DBA during daily troubleshooting. There are many times when DBA wants to check the progress of a backup or a restore activity which is happening on the server. By combining various commands from the internet, I have come up with below script which can give us a summary of current backups and restores which are happening on the server.

SELECT r.session_id AS [Session_Id]
	,r.command AS [command]
	,CONVERT(NUMERIC(6, 2), r.percent_complete) AS [% Complete]
	,GETDATE() AS [Current Time]
	,CONVERT(VARCHAR(20), DATEADD(ms, r.estimated_completion_time, GetDate()), 20) AS [Estimated Completion Time]
	,CONVERT(NUMERIC(32, 2), r.total_elapsed_time / 1000.0 / 60.0) AS [Elapsed Min]
	,CONVERT(NUMERIC(32, 2), r.estimated_completion_time / 1000.0 / 60.0) AS [Estimated Min]
	,CONVERT(NUMERIC(32, 2), r.estimated_completion_time / 1000.0 / 60.0 / 60.0) AS [Estimated Hours]
	,CONVERT(VARCHAR(1000), (
			SELECT SUBSTRING(TEXT, r.statement_start_offset / 2, CASE
						WHEN r.statement_end_offset = - 1
							THEN 1000
						ELSE (r.statement_end_offset - r.statement_start_offset) / 2
						END) 'Statement text'
			FROM sys.dm_exec_sql_text(sql_handle)
			))
FROM sys.dm_exec_requests r
WHERE command like 'RESTORE%'
or	command like 'BACKUP%'

Recently I found this script very handy while I was doing performance tuning exercise for a customer. Suddenly we had observed that one of their drives had more I/O and when we ran above script we figured out that there is a backup operation going on the system.

Do you have any similar interesting script which you can share via comments?

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on How to Get Status of Running Backup and Restore in SQL Server? – Interview Question of the Week #113

SQL SERVER – Msg 3101, Level 16 – Exclusive Access Could not be Obtained Because the Database is in Use

$
0
0

This error might have seen by DBAs and they know how to fix it. This blog goes in little details about what are the various options available. Here is the simple reproduction of the error message related to exclusive access.

SQL SERVER - Msg 3101, Level 16 - Exclusive Access Could not be Obtained Because the Database is in Use restore-in-use-01-800x324

USE master
GO
CREATE DATABASE SQLAuthority
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth.bak'
GO
-- at this point open another query window and connect to SQLAuthority
RESTORE DATABASE SQLAuthority FROM DISK = 'SQLAuth.bak'
GO

Here are the few things which we can check.

  1. Use sys.dm_exec_sessions to identify who is using the database which we are restoring.
SELECT session_id
	,host_name
	,program_name
	,client_interface_name
	,login_name
	,STATUS
	,is_user_process
FROM sys.dm_exec_sessions
WHERE database_id = db_id('SQLAuthority')
  1. The database that we are trying to restore should not be set as the default database for the login that we are using. We can verify the default database for a login by running the query-
SELECT loginname
	,dbname
FROM sys.syslogins
WHERE dbname = 'SQLAuthority'
  1. If you want, you can take the database offline, which would drop all the connection. Then you can perform restore.
  2. We can also detach the database. By detaching, the data and transaction log files of the database will be available on the same location. If we have to restore the same database at same files when we need to delete the files.
  3. Another option would be to drop the database. Once dropped, its files and data are deleted from the disk on the server. When a database is dropped, it is permanently removed and cannot be retrieved without using a previous backup.

Which is the most common option you use to refresh development/QA server from production server?

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – Msg 3101, Level 16 – Exclusive Access Could not be Obtained Because the Database is in Use

SQL SERVER – Msg 3136, Level 16 – This Differential Backup Cannot be Restored Because the Database has not Been Restored to the Correct Earlier State

$
0
0

I was trying to teach copy-only backups to my students and encountered below error related to differential backup.

Msg 3136, Level 16, State 1, Line 28
This differential backup cannot be restored because the database has not been restored to correct the earlier state.
Msg 3013, Level 16, State 1, Line 28
RESTORE DATABASE is terminating abnormally.

I have written a blog earlier in the same error message.

SQL SERVER – FIX: ERROR : Msg 3136, Level 16, State 1 – This differential backup cannot be restored because the database has not been restored to correct the earlier state

Here are the steps to create the scenario

USE master
GO
CREATE DATABASE SQLAuthority
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Full_01.bak' WITH FORMAT
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Diff_01.bak' WITH DIFFERENTIAL, FORMAT
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Full_02_cp.bak' WITH COPY_ONLY, FORMAT
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Diff_02.bak' WITH DIFFERENTIAL, FORMAT
GO

SQL SERVER - Msg 3136, Level 16 - This Differential Backup Cannot be Restored Because the Database has not Been Restored to the Correct Earlier State restore-error-diff-01

The above image shows the series of backup I have taken. You can read more about COPY_ONLY backup in books online. The error would come if I restore Diff_02 on top of Full_02_cp. Below is the code to reproduce the error.

USE [master]
RESTORE DATABASE [SQLAuthority_1] FROM
DISK = N'SQLAuth_Full_02_cp.bak'
WITH  FILE = 1,
MOVE N'SQLAuthority' TO N'G:\SQL_FILES\SQLAuthority_1.mdf',
MOVE N'SQLAuthority_log' TO N'G:\SQL_FILES\SQLAuthority_1_log.ldf',
NOUNLOAD,  STATS = 5, NORECOVERY
GO
RESTORE DATABASE [SQLAuthority_1]
FROM  DISK = N'SQLAuth_Diff_02.bak' WITH  FILE = 1,
RECOVERY,  NOUNLOAD,  STATS = 10
GO

SOLUTION/WORKAROUND

We need to restore a differential backup on top of regular full backup, not copy_only full backup. Following command would work as we are restoring diff_02 after performing restore of Full_01.

DROP DATABASE [SQLAuthority_1]
GO
USE [master]
RESTORE DATABASE [SQLAuthority_1] FROM
DISK = N'SQLAuth_Full_01.bak'
WITH
MOVE N'SQLAuthority' TO N'G:\SQL_FILES\SQLAuthority_1.mdf',
MOVE N'SQLAuthority_log' TO N'G:\SQL_FILES\SQLAuthority_1_log.ldf',
NOUNLOAD,  STATS = 5, NORECOVERY
GO
RESTORE DATABASE [SQLAuthority_1]
FROM  DISK = N'G:\SQL_FILES\SQLAuth_Diff_02.bak'
WITH RECOVERY,  NOUNLOAD,  STATS = 10
GO

Have you ever encountered such issues during disaster recovery where chain is broken?

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – Msg 3136, Level 16 – This Differential Backup Cannot be Restored Because the Database has not Been Restored to the Correct Earlier State

SQL SERVER – Msg 3035, Level 16 – Cannot Perform a Differential Backup for Database “SQLAuthority”, Because a Current Database Backup Does not Exist

$
0
0

If we look at the error in the subject line, it is very clear that a full backup is needed so that the differential backup chain can be initiated. If this happens to a newly created database, then it makes sense and there is no need for me to write a blog. There was an interesting finding about the cause of this error, other than obvious one, so sharing it.  Let us see another error related to differential backup.

My client contacted me saying that they are having a database in production for which they have full and differential backups configured using maintenance plan. Sometimes they see below failure message for the differential backup plan under the history.
Failed:(-1073548784) Executing the query “BACKUP DATABASE [HRMS] TO DISK = N’O:\\Backup\\HRMS…” failed with the following error: “Cannot perform a differential backup for database “HRMS”, because a current database backup does not exist. Perform a full database backup by reissuing BACKUP DATABASE, omitting the WITH DIFFERENTIAL option. BACKUP DATABASE is terminating abnormally.”. Possible failure reasons: Problems with the query, “ResultSet” property not set correctly, parameters not set correctly, or connection not established correctly.

They also showed me successful run on the full backup plan and it was not having any errors. So, why is SQL confused and thinks there is not full backup. I asked them to try the same using T-SQL in management studio and it failed with the same error.

Msg 3035, Level 16, State 1, Line 1
Cannot perform a differential backup for database “HRMS”, because a current database backup does not exist. Perform a full database backup by reissuing BACKUP DATABASE, omitting the WITH DIFFERENTIAL option.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.

I looked into SQL Server ERRORLOG and found something interesting for this database.

2017-04-13 06:39:50.10 spid52 I/O is frozen on database HRMS. No user action is required. However, if I/O is not resumed promptly, you could cancel the backup.
2017-04-13 06:40:50.11 spid52 I/O was resumed on database HRMS. No user action is required.
2017-04-13 06:40:50.24 Backup Database backed up. Database: HRMS, creation date(time): 2016/09/29(00:37:12), pages dumped: 426, first LSN: 55:395:37, last LSN: 55:414:1, number of dump devices: 1, device information: (FILE=1, TYPE=VIRTUAL_DEVICE: {‘{1A8E69AB-E97B-4CCE-8B3C-7E62573B8FD4}4’}). This is an informational message only. No user action is required.

If we take SQL native backups, we would never see “I/O is frozen” and “I/O was resumed” messages. Moreover, the TYPE of backup would be FILE not VIRTUAL_DEVICE as shown in 3rd message. We used query from my blog to check the backups for this database. https://blog.sqlauthority.com/2010/11/10/sql-server-get-database-backup-history-for-a-single-database

SQL SERVER - Msg 3035, Level 16 - Cannot Perform a Differential Backup for Database "SQLAuthority", Because a Current Database Backup Does not Exist vss-fail-01-800x57

We also check the report https://blog.sqlauthority.com/2014/08/04/sql-server-ssms-backup-and-restore-events-report/

SQL SERVER - Msg 3035, Level 16 - Cannot Perform a Differential Backup for Database "SQLAuthority", Because a Current Database Backup Does not Exist vss-fail-02-800x394 SQL SERVER - Msg 3035, Level 16 - Cannot Perform a Differential Backup for Database "SQLAuthority", Because a Current Database Backup Does not Exist vss-fail-01-800x57

At this point, we were clear that these are backups happening from some 3rd party tool. These tools use VSS to take backup of the machine. This is not a normal full database backup carried directly by SQL Server but through the Virtual Device Interface (VDI). I search and found there’s a similar known issue below:

https://support.microsoft.com/en-us/help/903643/the-ntbackup-exe-operation-may-break-the-differential-backup-chain-of-a-sql-server-database-and-may-invalidate-the-differential-backups-when-you-perform-a-snapshot-backup-of-the-database

WORKAROUND/SOLUTION

In this case, we figured out that these were Azure Virtual Machines and recently they configured VM level backup from Azure Portal. I looked into Microsoft’s documentation and I did find a solution to fix this on Azure VMs. We can enable VSS copy using the registry:

https://azure.microsoft.com/en-in/documentation/articles/backup-azure-vms-introduction/#how-does-azure-back-up-virtual-machines

Azure Backup takes VSS Full back-up on the Windows VMs. To enable VSS Copy Backups the below registry key needs to be set on the VM.

[HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\BCDRAGENT] “USEVSSCOPYBACKUP”=”TRUE”

We created key and verified that Azure was taking copy_only backup and maintenance plan were able to run a maintenance plan successfully along with Azure VM backups.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – Msg 3035, Level 16 – Cannot Perform a Differential Backup for Database “SQLAuthority”, Because a Current Database Backup Does not Exist


SQL SERVER – FIX: Msg 3132, Level 16 – The Media Set has 2 Media Families but Only 1 are Provided. All Members Must be Provided

$
0
0

This blog is a response to a comment on our blog. Have you ever encountered below error related to media set?

Msg 3132, Level 16, State 1, Line 15
The media set has 2 media families but only 1 are provided. All members must be provided.
Msg 3013, Level 16, State 1, Line 15
RESTORE DATABASE is terminating abnormally.

Here are the steps to reproduce the error. First, I would take backup of database.

USE master
GO
IF (db_ID('SQLAuthority') is NOT NULL)
DROP DATABASE SQLAuthority
GO
CREATE DATABASE SQLAuthority
GO
BACKUP DATABASE SQLAuthority
TO DISK = 'SQLAuth_Full_Part01.bak',
   DISK = 'SQLAuth_Full_Part02.bak'
WITH FORMAT

And now, try to restore it.

RESTORE DATABASE [SQLAuthority_1] FROM
DISK = N'SQLAuth_Full_Part01.bak'
WITH
MOVE N'SQLAuthority' TO N'E:\DATA\SQLAuthority_1.mdf',
MOVE N'SQLAuthority_log' TO N'E:\DATA\SQLAuthority_1_log.ldf'
GO

As shown below, it fails with error.

SQL SERVER - FIX: Msg 3132, Level 16 - The Media Set has 2 Media Families but Only 1 are Provided. All Members Must be Provided restore-media-01

I hope you understood the reason of error. If you look at backup command, I have taken a striped backup on 2 files, but during restore I have given only one.

This error message means that the original backup was done as a striped backup where the backup stream was split into two destination files. When attempting to restore, you need to specify all of the same files which were used to take the backup. So, the simple version is: If you back up to 2 files, you must specify 2 files to restore from.  If you back up to 5 files, you must specify 5 files to restore from, etc. You cannot restore any data from less than the full set of files used for the backup.

WORKAROUND/SOLUTION

We need to find out where is part of this backup. Below is the query which can be used to find the missing part of the backup.

DECLARE @DatabaseName NVARCHAR(max)
SET @DatabaseName = N'SQLAuthority'
USE msdb;
SELECT DISTINCT t1.NAME
	,t3.[media_set_id]
	,t6.family_sequence_number
	,t6.physical_device_name
	,(datediff(ss, t3.backup_start_date, t3.backup_finish_date)) / 60.0 AS duration
	,t3.backup_start_date
	,t3.backup_finish_date
	,t3.type AS [type]
	,CASE
		WHEN (t3.backup_size / 1024.0) < 1024
			THEN (t3.backup_size / 1024.0)
		WHEN (t3.backup_size / 1048576.0) < 1024
			THEN (t3.backup_size / 1048576.0)
		ELSE (t3.backup_size / 1048576.0 / 1024.0)
		END AS backup_size
	,CASE
		WHEN (t3.backup_size / 1024.0) < 1024
			THEN 'KB'
		WHEN (t3.backup_size / 1048576.0) < 1024
			THEN 'MB'
		ELSE 'GB'
		END AS backup_size_unit
	,t3.first_lsn
	,t3.last_lsn
	,CASE
		WHEN t3.differential_base_lsn IS NULL
			THEN 'Not Applicable'
		ELSE convert(VARCHAR(100), t3.differential_base_lsn)
		END AS [differential_base_lsn]
	,t6.device_type AS [device_type]
	,t3.recovery_model
FROM sys.databases t1
INNER JOIN backupset t3 ON (t3.database_name = t1.NAME)
LEFT JOIN backupmediaset t5 ON (t3.media_set_id = t5.media_set_id)
LEFT JOIN backupmediafamily t6 ON (t6.media_set_id = t5.media_set_id)
WHERE (t1.NAME = @DatabaseName)
ORDER BY t6.physical_device_name;

SQL SERVER - FIX: Msg 3132, Level 16 - The Media Set has 2 Media Families but Only 1 are Provided. All Members Must be Provided restore-media-02

Media_set_id and family_sequence_number can tell if there is a split backup.

Do you use this feature?

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – FIX: Msg 3132, Level 16 – The Media Set has 2 Media Families but Only 1 are Provided. All Members Must be Provided

SQL SERVER – FIX : Msg 3178, Level 16 – File SQLAuthority is Not in the Correct State to have This Differential Backup Applied to it

$
0
0

Few days back I wrote a blog about error messages during differential backup restore. SQL SERVER – Msg 3136, Level 16 – This Differential Backup Cannot be Restored Because the Database has not Been Restored to the Correct Earlier State

This blog is a variation of the same, but instead of restoring complete backup, we were trying to restore a file from differential backup. Here is the error message

(Msg 3178, Level 16, State 1, Line 26
File SQLAuthority is not in the correct state to have this differential backup applied to it.
Msg 3119, Level 16, State 1, Line 26
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 26
RESTORE DATABASE is terminating abnormally.

Here are the steps to create the scenario

USE master
GO
CREATE DATABASE SQLAuthority
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Full_01.bak' WITH FORMAT
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Diff_01.bak' WITH DIFFERENTIAL, FORMAT
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Full_02_cp.bak' WITH COPY_ONLY, FORMAT
GO
BACKUP DATABASE SQLAuthority TO DISK = 'SQLAuth_Diff_02.bak' WITH DIFFERENTIAL, FORMAT
GO

SQL SERVER - FIX : Msg 3178, Level 16 - File SQLAuthority is Not in the Correct State to have This Differential Backup Applied to it restore-error-file-01

The above image shows the series of backup I have taken. You can read more about COPY_ONLY backup in books online. The error would come if I restore Diff_02 on top of Full_02_cp. Below is the code to reproduce the error.

USE [master]
RESTORE DATABASE [SQLAuthority_1] FROM
DISK = N'SQLAuth_Full_02_cp.bak'
WITH
MOVE N'SQLAuthority' TO N'E:\DATA\SQLAuthority_1.mdf',
MOVE N'SQLAuthority_log' TO N'E:\DATA\SQLAuthority_1_log.ldf',
NOUNLOAD,  STATS = 5, NORECOVERY
GO
RESTORE DATABASE [SQLAuthority_1]  FILE = 'SQLAuthority'
FROM  DISK = N'SQLAuth_Diff_02.bak'
WITH RECOVERY,  NOUNLOAD,  STATS = 10
GO

SOLUTION/WORKAROUND

We need to restore differential backup on top of regular full backup, not copy_only full backup. Below command would work as we are restoring diff_02 after performing restore of Full_01.

DROP DATABASE [SQLAuthority_1]
GO
USE [master]
RESTORE DATABASE [SQLAuthority_1] FROM
DISK = N'SQLAuth_Full_01.bak'
WITH
MOVE N'SQLAuthority' TO N'E:\DATA\SQLAuthority_1.mdf',
MOVE N'SQLAuthority_log' TO N'E:\DATA\SQLAuthority_1_log.ldf',
NOUNLOAD,  STATS = 5, NORECOVERY
GO
RESTORE DATABASE [SQLAuthority_1]   FILE = 'SQLAuthority'
FROM  DISK = N'SQLAuth_Diff_02.bak'
WITH RECOVERY,  NOUNLOAD,  STATS = 10
GO

Have you ever encountered such issues during disaster recovery where chain is broken?

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – FIX : Msg 3178, Level 16 – File SQLAuthority is Not in the Correct State to have This Differential Backup Applied to it

SQL SERVER – Maintenance Plan – Backup Files Not Getting Deleted

$
0
0

Sometimes it is important to read the documentation before assuming things based on the text. This blog is the result of one of my quick interaction with a client who had an issue with Maintenance Plan which they created for backup files.

Pinal,
We are having issues with a maintenance plan. We have created plans to take log backup every 15 mins and configured it in such a way that it deletes the backup. The maintenance plan is running fine, but backups are not getting deleted. Do you have any ideas?

Within a few minutes, I replied to my blog which I wrote on similar behavior. SQL SERVER – Maintenance Plan – Maintenance Cleanup Task not Deleting Files

Their response made me laugh.

Pinal,
We have gone through the blog, but we are not using clean task. We have selected maintenance plan with the option “backup set will expire” to delete the old backup files. Do you think it’s a bug?

I replied with a one liner.

Backup set will expire”, means an expiration date to indicate when the backup set can be overwritten by another backup not that the file/data is removed.

SQL SERVER - Maintenance Plan - Backup Files Not Getting Deleted mp-clean-01

Please use a cleanup task to achieve what you want.

SOLUTION/WORKAROUND

To remove old backup files, we need to use maintenance plan cleanup task (refer my earlier blog). We might need to create two tasks. One to clean up the backup files and other to cleanup maintenance plan reports.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – Maintenance Plan – Backup Files Not Getting Deleted

SQL SERVER – How to Protect Your Database from Ransomware?

$
0
0

In the last month, I have received calls from quite a few of my customers about Ransomware. Particularly this weekend it has been very busy with over 8 customers had issues with Ransomware. However, we were lucky enough to have proper database backups and pre-cautions in the place and hence we were able to recover quickly and without downtime. Today, I decided to write up small notes for everyone who wants to protect their database from Ransomware.

SQL SERVER - How to Protect Your Database from Ransomware? ransomware

What is Ransomeware?

Ransomware is a program that gets into your computer, either by clicking on the wrong thing or downloading the wrong thing, and then it holds something you need to ransom.

Please do check that if you are following all the steps to keep your database safe.

Backup Database

The best protection against Ransomware is to backup all of your database files to a completely separate system. For example, take your backup to external hard drive or remote FTP system. Please make sure that after taking the backup your hard drive is not connected to your network or even the internet. If your hard drive is still connected to your network or computer when an attack happens on your system, your hard drive with a proper backup may be locked out of your reach.

Remember: Take back up to the system which is disconnected from your network or internet after the backup operation is over.

Important Resources:

Be Vigilant

Do not open suspicious emails, websites and apps on your network. Make sure your firewall is blocking all the unwanted traffic and unauthorized traffic.

One thing which I still see in many of my customer places is that they have left the browser and web surfing access available on the server where they have kept SQL Server. I have been very much against it from the very first day. During my Comprehensive Database Performance Health Check and SQL Server Performance Tuning Practical Workshop, we always discuss this as I believe this is very important aspect everyone should know and follow.

Additionally, you should have your database server on a different network from your user network that way, if your user network is attacked by ransomware your database server network remains protected from it. This is one of the very important infrastructure change one should implement while they are planning a robust and secure IT infrastructure for your network.

Remember: Do not open suspicious links on your computer network system.

Use Antivirus Program

I have been always advocating that an antivirus program should not be installed on your database server because they slow down the performance of your system when it is under stress. That does not mean that I have been against anti-virus on your network computers. I believe we should use latest antivirus program to scan your entire network and connected system to make sure there is no malware or any entry point open for ransomware. It is extremely critical that your all system’s are updated with latest anti-virus definitions.

Don’t Ignore Windows Update

Just like you, I have even had a habit of differing the Windows updates. In the past, I had poor experienced issues with windows updates, since then I have decided that I will only update windows once a month with the updates which are tested by the community and are over 15 days old. This routine has been working out great with me. However, there are moments when we have to make exceptions. During the recent attacks of Ransomware, I am updating my windows more often and with smaller intervals.

I totally understand that when we are patching recent windows update, there is a small risk of problems with our system. However, it is better to take risks with Windows Update then Ransomware. Please remember, at the end Microsoft has always provided updates for free and took responsibilities on the issues.

Contact Details

If you have any questions related to this article, or want to discuss your backup strategies, you can for sure reach out to me. Every single day I keep two slots available for On Demand (50 minutes). Just send me email with your preferred time for discussion at pinal@sqlauthority.com. We will conduct this learning session via gotomeeting.

During this 50 minutes we will discuss following:

  • Your Backup Timeline
  • Restore Strategy
  • Disaster Recovery Strategy
  • Ideas to keep Ransomware away

Remember, this 50 minutes can save you lots of troubles in the future. It is better to be safe now than sorry later. 

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER – How to Protect Your Database from Ransomware?

SQL SERVER- Error: 18204, Severity: 16 – Backup Failure – Operating System Error 1240 (The Account is not Authorized to Log in From This Station)

$
0
0

SQL SERVER- Error: 18204, Severity: 16 - Backup Failure - Operating System Error 1240 (The Account is not Authorized to Log in From This Station) errorstop Whenever you are into a business of “fixing the issue” – you should always find in error message first. The whole approach of solving the issue changes if we go behind the wrong error message. Let us learn about the error Backup Failure.

Once of my client told that their backups are failing randomly. If you are a DBA, you would agree with me that there can be many different reasons of backup failure, mostly permission or space issue. So, I asked them to send me email with exact issue and the error message from a SQL Server ErrorLog file. So now, they reported that – Our backups are “randomly” failing to backup up a database to a file share will with below errors in ERRORLOG file.

2017-05-11 11:36:23.10 spid109 Error: 18204, Severity: 16, State: 1.
2017-05-11 11:36:23.10 spid109 BackupDiskFile::CreateMedia: Backup device ‘\\ozone-fs\Backups\DBName.bak’ failed to create. Operating system error 1240(The account is not authorized to log in from this station.).
2017-05-11 11:36:23.10 Backup Error: 3041, Severity: 16, State: 1.
2017-05-11 11:36:23.10 Backup BACKUP failed to complete the command BACKUP DATABASE DBName. Check the backup application log for detailed messages.

The first observation from error message is that it’s a share not a local drive. I was curious to know what kind of share it is? They informed that this is Scale-Out File Server (SOFS) configuration with CA (Continuously Available) shares. When I searched for the error message, it was talking about SMB and some registry level change, but had nothing to do with CA shares.

I had no idea about this feature so I did some research about this feature and found something interesting. Read the article here.

My thoughts:

Based on my research and learning, for the SQL backups, we should not use Scale-Out File Server (SOFS) configuration with CA (Continuously Available) shares, in favor of General Use File Server Cluster. Here is the reference.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

First appeared on SQL SERVER- Error: 18204, Severity: 16 – Backup Failure – Operating System Error 1240 (The Account is not Authorized to Log in From This Station)

SQL SERVER – Unable to Restore from URL – The Specified URL Points to a Block Blob. Backup and Restore Operations on Block Blobs are not Permitted

$
0
0

SQL SERVER - Unable to Restore from URL - The Specified URL Points to a Block Blob. Backup and Restore Operations on Block Blobs are not Permitted backup With Microsoft Azure pitching in the market, I am getting few clients who want me to assist them for errors related to Azure as well. As a part of growth, I do learn them and try to help the clients. Let us learn about Block Blobs in this blog post.

My client was migrating their on-premise SQL Severs to SQL running under Azure virtual machines. They have decided to move database via backup restore.

  • Source SQL Server (On-Premise): SQL 2012 Standard.
  • Destination SQL Server (SQL on Azure VM): SQL 2014 Enterprise

While restoring from the Azure storage using “Restore … from URL” command, we were seeing below error in ERRORLOG.

BackupVirtualDeviceFile::DetermineFileSize: SetPosition(0,EOF) failure on backup device ‘https://sqlauth storagehdd.blob.core.windows.net/sqlbackup/DBName.bak’. Operating system error The specified URL points to a Block Blob. Backup and Restore operations on Block Blobs are not permitted.

BackupVirtualDeviceFile::DetermineFileSize: SetPosition(0,EOF) failure on backup device ‘https://sqlauth storagehdd.blob.core.windows.net/sqlbackup/DBName.bak’. Operating system error The specified URL points to a Block Blob. Backup and Restore operations on Block Blobs are not permitted.

I asked the method to transfer the data from the source server to Azure. They told that they have used AzCopy to copy the data from the local machine to Azure storage. So essentially, database backup was taken locally on On-Prem machine and then AzCopy was used to copy the backup files. They have not used “BACKUP … to URL”

Transfer data with the AzCopy on Windows

I looked into documentation of SQL 2012 and SQL 2014 and found below.

SQL Server Backup and Restore with Windows Azure Blob Storage Service

Blob: A file of any type and size. There are two types of blobs that can be stored in the Windows Azure Blob storage service: block and page blobs. SQL Server backup uses page Blobs as the Blob type. Blobs are addressable using the following URL format: https://<storage account>.blob.core.windows.net/<container>/<blob>

Caution
If you choose to copy and upload a backup file to the Windows Azure Blob storage service, use page blob as your storage option. Restores from Block Blobs are not supported. RESTORE from a block blob type fails with an error.

Separately, in SQL Server 2014, we were able to see the same limitation listed, but under a different topic: SQL Server Backup to URL

Blob: A file of any type and size. There are two types of blobs that can be stored in the Windows Azure Blob storage service: block and page blobs. SQL Server backup uses page Blobs as the Blob type. Blobs are addressable using the following URL format: https://<storage account>.blob.core.windows.net/<container>/<blob>

Warning
If you choose to copy and upload a backup file to the Windows Azure Blob storage service, use page blob as your storage option. Restores from Block Blobs are not supported. RESTORE from a block blob type fails with an error.

SOLUTION / WORKAROUND

It looks like, the backup file was turned into a block blob, and that explains the cause of the error during restore. I think the above is implying that the transfer tool (AZCopy) may have been configured to use block blob to transfer the backup file by default. After reading further about the tool, I found there is a parameter called /BlobType:Page. Once we used that parameter, restore worked like a charm.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Unable to Restore from URL – The Specified URL Points to a Block Blob. Backup and Restore Operations on Block Blobs are not Permitted

SQL SERVER – Msg 3168, Level 16 – The Backup of the System Database on the Device Cannot be Restored Because it was Created by a Different Version of the Server

$
0
0

SQL SERVER - Msg 3168, Level 16 - The Backup of the System Database on the Device Cannot be Restored Because it was Created by a Different Version of the Server backupswitch-800x506 In the recent past, I have written a blog about Ransomware. Here is the link. SQL SERVER – How to Protect Your Database from Ransomware?. In this blog post we are going to discuss about system database.

One of my client was hit with Ransomware and they contacted me. Luckily, they had database backups in another location and all they wanted was to build a new server and go back to the old state. They have reinstalled same version of operating system and installed the same version of SQL Server. Now, to go back to a previous state, they wanted to restore all databases from backup. While restoring master, they were getting error related to SQL version.

Msg 3168, Level 16, State 1, Line 1
The backup of the system database on the device cannot be restored because it was created by a different version of the server (10.50.2550) than this server (10.50.2500).
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

The error message is about version mismatch. If you get the same error while restoring master (or msdb or models), you need to make a note of two things.

  1. The version on which backup was taken. (First build number in error message)
  2. Current version of SQL Server. (Second build number on error message.

WORKAROUND/SOLUTION

To restore any system database backup, #1 and #2 should match. If they don’t, then SQL would raise error message during restore. You should refer to http://sqlserverbuilds.blogspot.in and find the desired build number and download it.

  • If the current bid is lower and apply the desired patch.
  • If the current bid is higher than uninstall the patch (or patches) till both become same.

Hope this would help. If you have more ideas and thoughts, please share via comments.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Msg 3168, Level 16 – The Backup of the System Database on the Device Cannot be Restored Because it was Created by a Different Version of the Server


What is Copy Only Backup in SQL Server? – Interview Question of the Week #128

$
0
0

Question: What is Copy Only Backup in SQL Server?

Answer: This is another interesting question I received the other day. The question was in the image form and I am re-producing the image again here.

What is Copy Only Backup in SQL Server? - Interview Question of the Week #128 copyonly0

The answer of this question can be very long. However, to simplify the answer here is what I would have replied if I ever get asked – “Copy Only backup is independent backup and it does not break the chain of database backup LSN (Log Sequence Number). In another word, it does not disturb the original backup workflow, but just create a copy of the original database independently.”

Well, that was a short answer here is a bit more detailed answer. However, if you are not sure what is differential backup, I strongly suggest that you read this blog post first before you continue reading this blog post. Must Read: Backup Timeline and Understanding of Database Restore Process in Full Recovery Model.

When you want to restore your latest differential backup, you must first restore the last full backup before restoring this full backup. Let us assume that everyday you are taking full database backup at midnight and differential backup every hour.  Now when you want to restore your database to 2 PM in the afternoon, you will first have to restore your full database backup of midnight and restore the latest differential backup.

However, due to any reason, if you take another full backup at 10 AM in the morning, the differential backup at 2 PM, can be only restored after you have restored the 10 AM full database backup and not the one which you have taken at midnight. This can be very complicated if you have a backup workflow in your organization and certain sets of the policy how your backup should be set up.

To illustrate this scenario I have created following image. Where you can see how the regular backup in middle of your backup workflow works. If you have placed automated workflow of the backup sequence, taking an additional backup in the middle can just create disruption in the automatic process if it is dependent on sequence.

What is Copy Only Backup in SQL Server? - Interview Question of the Week #128 copyonly2

Now let us think of the scenario that you just want to take a backup but does not want to disturb the backup workflow. In that case you can use the copy_only backup option and that will not disturb the backup workflow. The optional copy_only will be a stand alone full backup.

What is Copy Only Backup in SQL Server? - Interview Question of the Week #128 copyonly1

Here is the T-SQL script for copy_only backup.

BACKUP DATABASE [DatabaseName] TO DISK = N':c\backup.bak' WITH COPY_ONLY

Let me know what you think of this blog post. I will be happy to know your opinion and if you believe I should add something more in the blog post, please leave a comment.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on What is Copy Only Backup in SQL Server? – Interview Question of the Week #128

SQL SERVER – Alternative of Log Shipping in Simple Recovery Model

$
0
0

The blog topic might seem controversial and doesn’t go well with definition of log shipping features. Read it again. The blog has an alternative way to set up something “similar” to log-shipping. If the database is in simple recovery model, then we can’t take transaction log backups and hence no log shipping possible.

SQL SERVER - Alternative of Log Shipping in Simple Recovery Model logshipping

In the script, I am taking last full backup and the last differential backup and restoring them on the destination server. To do this, we need to have a linked server created on the secondary server, which would point to the primary server. Here are the things to be replaced with the script.

  1. Shared Path on primary (@BackupPath) – This is the share location which has backup and can be accessed by SQL Server on secondary for restoring purpose.
  2. Local path on secondary (@RestorePath) – This is the folder on secondary where the backups would be restored and MDF/LDF would be kept there.
  3. Database Name (@database_name) – This is the database name for which we want to configure regular backups and restores.
  4. Linked server name – This is the linked server created on secondary server. In below script, it’s called as PrimaryLinkedServer.

You can download the entire script from this link

Please comment if you find it useful.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Alternative of Log Shipping in Simple Recovery Model

What does BACKUP WITH NAME Command do? – Interview Question of the Week #130

$
0
0

Questions: What does BACKUP WITH NAME Command do?

Answer: Nothing more than naming your database backup.

What does BACKUP WITH NAME Command do? - Interview Question of the Week #130 backupwithname-800x211

Honestly, I was recently asked this question by a user and I was a bit lost when I heard this one. To find the answer, I had to search the internet and read documents before I was able to answer it. I am never ashamed of accepting that there are quite a few things I do not know.

After reading MSDN documents, I figured out that we can name our backup and use that name to check its value in the msdn table backupset.

Let me show you a simple example:

— taking a backup with name

BACKUP DATABASE AdventureWorks2014
TO DISK = 'D:\AW.bak'
WITH NAME = 'AW-SQLAuthority'
GO

Now we will run a query on the msdb.dbo.backupset table with a where condition. Please note that this table contains details of successful backup operations.

SELECT name, *
FROM msdb.dbo.backupset
WHERE [name] = N'AW-SQLAuthority'
GO

When we run above queries it gives us record for all the backups with the name specified in the WHERE conditions.

I am not sure how many really uses this particular feature of SQL Server. Anyway, there are many other useful ways to use msdb.db.backupset database.

Well, that’s it. I think this is when it is about backup with name at this point of time. Do you have any other suggestions or idea? If yes, please share it here in the comments.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on What does BACKUP WITH NAME Command do? – Interview Question of the Week #130

SQL SERVER – Msg 1833 – File Cannot be Reused Until After the Next BACKUP LOG Operation

$
0
0

While preparing for a demo for an upcoming session, I came across an error which I didn’t see earlier. Here is the error in the UI and it is related to Backup Log Operation.

SQL SERVER - Msg 1833 - File Cannot be Reused Until After the Next BACKUP LOG Operation file-add-error

I am also trying to add a file to the database using below command.

USE [master]
GO
ALTER DATABASE [SQLAuth] ADD FILE ( NAME = N'SQLAuth_LogNew', FILENAME = N'F:\DATA\SQLAuth_LogNew.ndf' , SIZE = 8192KB , FILEGROWTH = 65536KB ) TO FILEGROUP [PRIMARY]
GO

And it also failed with following error.

If I change the file name, it works. As mentioned in error message I performed a log backup, and I was able to add the file again.

ROOT CAUSE

While working yesterday, I already added the file and dropped it. As per error message, if I need to reuse the same logical name of the database file, I must take a log backup. Here is the simple reproduction of the error.

CREATE DATABASE [DropFileTest]
GO
USE [master]
GO
-- take a full backup to make it real full else it would act as simple.
BACKUP DATABASE [DropFileTest] TO  DISK = N'DropFileTest.bak'
GO
-- add a new LDF file
USE [master]
GO
ALTER DATABASE [DropFileTest] ADD LOG FILE ( NAME = N'NewLDF',
FILENAME = N'F:\LOG\NewLDF.ldf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
GO
-- reomve the file.
USE [DropFileTest]
GO
ALTER DATABASE [DropFileTest]  REMOVE FILE [NewLDF]
GO
-- Adding same file again and it should fail.
USE [master]
GO
ALTER DATABASE [DropFileTest] ADD LOG FILE ( NAME = N'NewLDF',
FILENAME = N'F:\LOG\NewLDF.ldf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
GO

If you run above, you can notice the same error.

WORKAROUND/FIX

As mentioned in the error message, take a transaction log backup of the database. In the above demo script, we can do below before adding a file and it should work.

BACKUP LOG [DropFileTest] TO  DISK = N'DropFileTest.trn'
GO
-- Adding same file again and it should work this time.
USE [master]
GO
ALTER DATABASE [DropFileTest] ADD LOG FILE ( NAME = N'NewLDF', FILENAME = N'F:\LOG\NewLDF.ldf' )
GO

Have you seen such interesting error?

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – Msg 1833 – File Cannot be Reused Until After the Next BACKUP LOG Operation

What does Keyword STATS Indicates in Backup Scripts in SQL Server? – Interview Question of the Week #138

$
0
0

Question: What does Keyword STATS Indicates in Backup Scripts in SQL Server?

Answer: This is a very interesting question. I have seen many DBA using the backup scripts and using the keywords STATS but when I ask them if they really know why they are using the keyword STATS, most of the time they have no clue with that.

What does Keyword STATS Indicates in Backup Scripts in SQL Server? - Interview Question of the Week #138 stat0

Let us first see the backup scripts in SQL Server.

BACKUP DATABASE [AdventureWorks2014] TO
DISK = N'D:\AW1.bak' WITH FORMAT,
STATS = 10
GO

Now the question is what does the keyword STATS do and how it is used.

Well, here is the answer – Stats displays a message of percentage complete when the backup is going on. The default value of the stats is 10 and hence if we do not specify the stats, keywords, SQL Server displays the percentage completion at every 10 percentage.

To further illustrate what I mean by this, let us see a few screenshots where I have changed the STATS value to different values.

What does Keyword STATS Indicates in Backup Scripts in SQL Server? - Interview Question of the Week #138 stat1

What does Keyword STATS Indicates in Backup Scripts in SQL Server? - Interview Question of the Week #138 stat2

What does Keyword STATS Indicates in Backup Scripts in SQL Server? - Interview Question of the Week #138 stat3

What does Keyword STATS Indicates in Backup Scripts in SQL Server? - Interview Question of the Week #138 stat4

I hope from these examples it is very clear to you that STATS displays the progress of the backup complete in SQL Server. For large databases, I prefer to keep the value of the STATS to a much lower value so we can see much finer progress in the message window.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on What does Keyword STATS Indicates in Backup Scripts in SQL Server? – Interview Question of the Week #138

Viewing all 110 articles
Browse latest View live