Question

I am trying to restore a database from .bak and trn files. I am not able to see .bak and .trn files through the SQL Server Management Studio. But when I go to the folder I see them. I used T-sql but it says access is denied. I am a sysadmin on the server. Can someone please help me with it. Script:

RESTORE DATABASE [XYZ] FROM DISK = N'R:\MSSQL10_50.MSSQLSERVER\MSSQL\Restore\XYZ_Full.bak' WITH FILE = 1

GO

Error: Msg 3201, Level 16, State 2, Line 3 Cannot open backup device 'R:..."Operating system error 5(Access is denied.).

Was it helpful?

Solution

Make sure your account (windows or sql server) in SSMS has the right to backup/restore, sysadmin, db_backoperator, etc.

The backup and restore processes runs under the SQL Server (Engine) Service account since you might be running SSMS on your laptop but working with files on the server.

It doesn't matter who you are logged in as, it is the service account that needs access to the directory and files.

Is the service account a domain account or a local service? I use a domain account so that I can work with files on a UNC path.

Also, there are two system stored procedures that get executed during the browse dialog: master.dbo.xp_dirtree, master.dbo.xp_fileexist.

If they return empty results from a query window, it is a permission issue with the SQL Server Service account.

Profiler Trace Browse operation (Adventure Works).

declare @Path nvarchar(255)
declare @Name nvarchar(255)    

select @Path = N'C:\mssql\save me\backup\AdventureWorks2012'
select @Name = N'AdventureWorks2012_backup_2012_11_30_160723_2147507.bak'

create table #filetmpfin (Name nvarchar(255) NOT NULL, IsFile bit NULL) 

if(@Name is null) 
begin 
  create table #filetmp (Name nvarchar(255) NOT NULL, depth int NOT NULL, IsFile bit NULL ) 
  insert #filetmp EXECUTE master.dbo.xp_dirtree @Path, 1, 1 
  insert #filetmpfin select Name, IsFile from #filetmp f 
  drop table #filetmp 
end 

if(NOT @Name is null) 
begin 
  declare @FullName nvarchar(300) 
  if(@Path is null) 
    select @FullName = @Name 
  else
    select @FullName = @Path    + '\' + @Name 

  create table #filetmp2 ( Exist bit NOT NULL, IsDir bit NOT NULL, DirExist bit NULL ) 
  insert #filetmp2 EXECUTE master.dbo.xp_fileexist @FullName 
  insert #filetmpfin select @Name, 1-IsDir from #filetmp2 where Exist = 1 or IsDir = 1  
  drop table #filetmp2 
end

SELECT
 Name AS [Name],
 IsFile AS [IsFile]
FROM 
  #filetmpfin
ORDER BY
  [IsFile] ASC,[Name] ASC

drop table #filetmpfin
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top