0

My application has an uninstall script which is written in VBScript. This script deletes all the registries and is supposed to uninstall sql server as well as delete the database that has been created during install. I want to take a backup of the database instead of deleting it.

So I have written a function to rename the mdf and ldf files with VBS, but I get a permission denied error for this method.

Dim file

DBfile = "C:\Program Files\Microsoft SQL 
         Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DB.mdf"

Sub backupDBFiles(file)
    Dim filesys

    Set filesys = WScript.CreateObject("Scripting.FileSystemObject")

    If filesys.FileExists(file) Then
        filesys.MoveFile file, file & "backup"
    End If
End Sub

backupDBFiles(DBfile)

The backupDBFiles method works for any other file, so I think that this method should ideally rename the mdf file, but gives me a permission denied error. Is there a way to fix this error?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
s_om
  • 661
  • 2
  • 9
  • 24
  • add your service account as local administrator – Ed Bangga Aug 29 '19 at 02:14
  • I tried the same function running the vbs from cmd as administrator but I get the same error – s_om Aug 29 '19 at 02:16
  • so its not about renaming the files, something to do with granting your service account as sysadmin – Ed Bangga Aug 29 '19 at 02:18
  • SQL Server configures the file system permissions to protect database files. You may need to change file ACLs, ACL inheritance or file ownership. – David Browne - Microsoft Aug 29 '19 at 02:19
  • Thank you David, could you please suggest how we can change the file ACLs? – s_om Aug 29 '19 at 03:01
  • The code you posted is invalid. VBScript doesn't support multiline strings. Please do not wrap code in arbitrary places. As for modifying ACLs, you can do that via [VBScript/WMI](https://stackoverflow.com/a/13341479/1630171) (the code there modifies share permissions, for file permissions you need the `Win32_LogicalFileSecuritySetting` class), but it's rather complicated. In general it's far simpler to [shell out to `icacls`](https://stackoverflow.com/a/15709424/1630171). – Ansgar Wiechers Aug 29 '19 at 08:12

0 Answers0