Recently we came across a situation where one of the customer was trying to restore a Database backup using SSMS and while going to Options tab UI failed with the error:
"Length cannot be less than zero. Parameter name: length"
We later found that RESTORE FILELISTONLY of the backup file showed incorrect path to the Database file.
LogicalName PhysicalName
--------------------------------------------------
Test D:\Test.mdf
Test_log D:Test_log.ldf
So as you see, someone has altered the path of the LDF file to D:Test_log.ldf instead of D:\Test_log.ldf. Customer was not aware that who altered the Database file path and how it got reflected in the backup . But then customer was able to restore the same backup file using T-SQL(which is explained later in this section)
Here I wanted to provide the repro steps which will explain on what circumstances we receive this error.
The issue occurs when you have the below conditions true.
1. You have a database with database file on the Root Drive (i.e. C:\ or D:\)
2. You alter the path of database files to another root drive and miss \ character.
3. You altered the Database as READ-ONLY.
4. You took a backup of the Database.
5. You are trying to Restore this backup using SSMS.
Repro Steps:
=============
1. Create a Database . Here I am using the DB name as ‘RestoreCheck’.
--Creating Database CREATE DATABASE [RestoreCheck] ON PRIMARY ( NAME = N'RestoreCheck', FILENAME = N'D:\RestoreCheck.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB ) LOG ON ( NAME = N'RestoreCheck_log', FILENAME = N'D:\RestoreCheck_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%) GO
--Creating Database
CREATE DATABASE [RestoreCheck] ON PRIMARY
( NAME = N'RestoreCheck', FILENAME = N'D:\RestoreCheck.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'RestoreCheck_log', FILENAME = N'D:\RestoreCheck_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%)
GO
2. Alter the Database LDF file path(Notice that the path which I am giving here is incorrect. I am missing a ‘\’ after the drive letter D: )
USE master; GO ALTER DATABASE [restorecheck] MODIFY FILE ( NAME = 'RestoreCheck_log',FILENAME = N'D:restorecheck_log.ldf' ) GO
3. Mark the Database as ‘Read Only’ USE [master] GO ALTER DATABASE [restorecheck] SET READ_ONLY WITH NO_WAIT GO
4. Backup the Database and here I am taking a FULL backup.
backup database [restorecheck] to disk ='D:\restorecheckfull.bak'
5. Using SQL Server Management Studio, Try to now Restore the backup taken on same server with a different name. Here I am trying to restore the database with name ‘copy_restorecheck’. When you choose the backup set and click on “Options”, you will encounter the error:
6. Below is the screen shot of the error:
7. We can use the T-SQL Command and restore the backup to resolve the problem, as shown below:
RESTORE DATABASE [copy_restorecheck] FROM DISK = N'D:\restorecheckfull.bak' with recovery, MOVE N'restorecheck' TO N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\copy_restorecheck.mdf', MOVE N'restorecheck_log' TO N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\copy_restorecheck.ldf', NOUNLOAD, STATS = 10 GO
RESTORE DATABASE [copy_restorecheck] FROM
DISK = N'D:\restorecheckfull.bak'
with recovery,
MOVE N'restorecheck' TO
N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\copy_restorecheck.mdf',
MOVE N'restorecheck_log' TO
N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\copy_restorecheck.ldf',
NOUNLOAD, STATS = 10
Key Points:
=========
Regards, Durai Murugan Support Engineer, Microsoft SQL Server Support
Reviewed by: Balmukund Lakhani Technical Lead, Microsoft SQL Server Support
Pranab Mazumdar Support Escalation Engineer, Microsoft SQL Server Support