This script is a simple utility to allow users to restore SQL Server databases. It’s easier to use for non-DBAs and it provides a method of restoring databases when the management tools are not installed. It’s a good demonstration of a HTA application, but it’s not a intended as a replacement for SQL Server Management Studio. You can only use this utility for simple restore scenarios.A simple utility that allows you to restore SQL Server databases from a backup file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
<html><head> <STYLE TYPE="text/css"> BODY {font-family: "Verdana, Arial, Helvetica, sans-serif"; font-size:x-small; background-color:#dfe0e5; } table {font-family: "Verdana, Arial, Helvetica, sans-serif"; font-size:x-small;} #RestoreCompleted { font-weight:bold; color:green; } #RestoreInProgress { font-weight:bold; color:blue; } #RestoreError { font-weight:bold; color:red; } #Footer { font-weight:bold; font-size:10px; } </STYLE> <script language="vbscript" OnLoad="Main()"> option explicit Const HKEY_LOCAL_MACHINE = &H80000002 Const adVarChar = 200 Const adParamInput = 1 Const intAppWidth = 600 Const intAppHeight = 280 ' 255 ' Set app size and centre screen on load Sub Window_Onload() Dim objWMIService,objItem,intHorizontal,intVertical Dim intLeft, intTop Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") For Each objItem in objWMIService.ExecQuery("Select * From Win32_DesktopMonitor") intHorizontal = objItem.ScreenWidth intVertical = objItem.ScreenHeight Next If IsNull(intHorizontal) = False And IsNull(intVertical) = False Then intLeft = (intHorizontal - intAppWidth) / 2 intTop = (intVertical - intAppHeight) / 2 window.moveTo intLeft, intTop End If window.resizeTo intAppWidth,intAppHeight LoadSQLServerInstances() End Sub ' Populate list box with local SQL Server instances Sub LoadSQLServerInstances() Dim strComputer,strKeyPath,strValueName,strValue Dim oReg,arrValues, d strComputer = "." Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\default:StdRegProv") strKeyPath = "SOFTWARE\Microsoft\Microsoft SQL Server\" strValueName = "InstalledInstances" oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, _ strValueName,arrValues If ISArray(arrValues) = false then msgbox "No local SQL Server instances found",vbCritical Close() end If set d = createobject("Scripting.Dictionary") For Each strValue In arrValues Dim objOption Set objOption = Document.createElement("OPTION") if strValue = "LOCAL" OR strValue="MSSQLSERVER" then strValue = "(local)" else strValue = ".\" & strValue end if objOption.Text = strValue objOption.Value = strValue if d.Exists(strValue) = false then if selInstances.Contains(objOption) = false then selInstances.Add(objOption) end if d.Add strValue,strValue end if Next End Sub ' Prompt user to select backup file Sub SelectBackupFile() Dim oDLG Set oDLG=CreateObject("MSComDlg.CommonDialog") With oDLG .DialogTitle="Open" .Filter="Backup Files|*.bak|All files|*.*" .MaxFileSize=255 .Flags=.Flags Or &H1000 'FileMustExist (OFN_FILEMUSTEXIST) .ShowOpen If .FileName<>"" Then txtFileName.Value =.FileName End If End With Set oDLG=Nothing End Sub ' Prompt user for location of database restore Sub SelectRestoreFolder() Const MY_COMPUTER = &H11& Const WINDOW_HANDLE = 0 Const OPTIONS = 0 Dim objShell,objFolder,objFolderItem,strPath Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(MY_COMPUTER) Set objFolderItem = objFolder.Self strPath = objFolderItem.Path Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.BrowseForFolder _ (WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath) If not objFolder Is Nothing Then Set objFolderItem = objFolder.Self txtRestoreFolder.Value = objFolderItem.Path End If End Sub ' Show/Hide username/password textbox depending on integrated security option Sub ShowAuth() if chkIntegrated.Checked = true then trUserName.Style.Display = "none" trPassword.Style.Display = "none" window.resizeTo intAppWidth,intAppHeight else trUserName.Style.Display = "block" trPassword.Style.Display = "block" window.resizeTo intAppWidth,intAppHeight + 50 end if End Sub ' Generate connection string for the local database Private Function GetConnectionString Dim strInstance,strUserName,strPassword strInstance = selInstances.Value strUserName = txtUserName.Value strPassword = txtPassword.Value if chkIntegrated.Checked = true then GetConnectionString = "Provider=SQLOLEDB.1;Data Source=" & strInstance & ";Integrated Security=SSPI;" else GetConnectionString = "Provider=SQLOLEDB.1;Data Source=" & strInstance & ";UID=" & strUserName & ";PWD=" & strPassword end if End Function ' Check if the specified database name already exists Function DatabaseExists(byval dbName) Dim strSQL,objCn,objCmd,strCn, objRs,pName strCn = GetConnectionString() strSQL = "SELECT COUNT(*) FROM sysdatabases where name = ?" set objCn = CREATEOBJECT("ADODB.Connection") set objCmd = CREATEOBJECT("ADODB.Command") set pName = objCmd.CreateParameter("@name",advarchar,adParamInput,128,dbname) objCmd.Parameters.Append pName objCn.Open strCn objCmd.ActiveConnection = objCn objCmd.CommandText = strSQL set objRs = objCmd.Execute if objRs(0) = 0 then DatabaseExists = false else DatabaseExists = true end if objRs.Close objCn.Close end function ' Perform validations before the restore Private Function Validate() dim objFSO, strCn, objCn,dbName set objFSO=createobject("Scripting.FileSystemObject") ' Check backup file exists if NOT objFSO.FileExists(txtFileName.Value) then RestoreError.InnerHTML = "File specified does not exist" Validate = false exit function end if ' Check restore folder exists if not objFSO.FolderExists(txtRestoreFolder.Value) then RestoreError.InnerHTML = "Folder specified does not exist" Validate = false exit function end if err.clear on error resume next ' Check connection to database strCn = GetConnectionString set objCn = CREATEOBJECT("ADODB.Connection") objCn.Open strCn objCn.Close if err.number <> 0 then RestoreError.InnerHTML= "Connection error:" & err.number Validate = false exit function end if ' Get database name from backup file. ' Just a quick check that backup file is readable GetDatabaseNameFromBackupFile() if err.number <> 0 then RestoreError.InnerHTML = "Error:" & err.number & " - " & err.description Validate = false exit function end if on error goto 0 ' Get database name. dbName = GetDatabaseName() ' Check if database with same name already exists if DatabaseExists(dbName) then RestoreError.InnerHTML = "Error: Database '" & dbname & "' already exists" Validate = false exit function end if Validate = True End Function ' Return database name for restore (either from backup file or user specified name) Private Function GetDatabaseName if chkDBNameDefault.Checked = True then GetDatabaseName = GetDatabaseNameFromBackupFile() Else GetDatabaseName = txtDBName.Value end if End Function ' Retreive database name from backup file Private Function GetDatabaseNameFromBackupFile Dim strSQL,objCn,objCmd,strCn, objRs,pRestoreFile strCn = GetConnectionString() strSQL = "RESTORE HEADERONLY FROM DISK = ?" set objCn = CREATEOBJECT("ADODB.Connection") set objCmd = CREATEOBJECT("ADODB.Command") set pRestoreFile = objCmd.CreateParameter("@RestoreFile",advarchar,adParamInput,255,txtFileName.Value) objCmd.Parameters.Append pRestoreFile objCn.Open strCn objCmd.ActiveConnection = objCn objCmd.CommandText = strSQL set objRs = objCmd.Execute GetDatabaseNameFromBackupFile = objRs("DatabaseName") objCn.Close end function ' Similar to SQL Server QUOTENAME function ' Wraps quotes around string and performs escapes Private Function QuoteName(byval Value, quoteChar) if quotechar = "[" or quotechar = "]" then QuoteName = "[" & REPLACE(Value,"]","]]") & "]" elseif quoteChar = "'" then QuoteName = "'" & REPLACE(Value,"'","''") & "'" else QuoteName = "" end if End Function ' Function to get the SQL statement used to perform the DB restore Private Function GetRestoreSQL Dim strSQL,objCn,objCmd,strCn, objRs,pRestoreFile Dim dbName dbName = GetDatabaseName() strCn = GetConnectionString() strSQL = "RESTORE FILELISTONLY FROM DISK = ?" set objCn = CREATEOBJECT("ADODB.Connection") set objCmd = CREATEOBJECT("ADODB.Command") set pRestoreFile = objCmd.CreateParameter("@RestoreFile",advarchar,adParamInput,255,txtFileName.Value) objCmd.Parameters.Append pRestoreFile objCn.Open strCn objCmd.ActiveConnection = objCn objCmd.CommandText = strSQL on error resume next set objRs = objCmd.Execute if err.number <> 0 then msgbox(err.description & err.number) exit function end if Dim strFolder strFolder = txtRestoreFolder.Value if not RIGHT(strFolder,1) = "\" then strFolder = strFolder & "\" end if Dim strRestore,strPath while objRs.EOF <> True AND objRs.BOF <> True strPath = strFolder + dbname & "_" & objRs("LogicalName") if objRs("Type") = "L" then 'LOG strPath = strPath & ".ldf" elseif objRs("Type") = "S" then 'FILESTREAM ' Folder don't add extension else 'DATA if objRs("FileID") = "1" then 'Primary File strPath = strPath & ".mdf" else strPath = strPath & ".ndf" end if end if if LEN(strRestore)> 0 then strRestore = strRestore & ", " end if strRestore = strRestore & "MOVE N" & QuoteName(objRs("LogicalName"),"'") & " TO " & QuoteName(strPath,"'") objRs.MoveNext wend objCn.Close strRestore = "RESTORE DATABASE " & QuoteName(dbName,"]") & " FROM DISK=N" & QuoteName(txtFileName.Value,"'") & " WITH FILE=1," & strRestore GetRestoreSQL = strRestore End Function ' Perform database restore Sub RestoreDatabase() Dim strSQL,strCn, objCn, objCmd ' Clear any existing error/completed notifications RestoreError.InnerHTML = "" RestoreCompleted.Style.Display = "none" ' Perform validation checks prior to restore if Validate() = false then exit sub end if ' Set restore notification RestoreInProgress.Style.Display = "block" UpdateScreen ' Get SQL for Restore strSQL = GetRestoreSQL() strCn = GetConnectionString() set objCn = CREATEOBJECT("ADODB.Connection") set objCmd = CREATEOBJECT("ADODB.Command") objCn.Open strCn objCmd.ActiveConnection = objCn objCmd.CommandText = strSQL objCmd.CommandTimeout = 0 on error resume next objCmd.Execute objCn.Close if err.number <> 0 then RestoreError.InnerHTML = "Error: " & err.Number & " - " & err.description else RestoreCompleted.Style.Display = "block" end if RestoreInProgress.Style.Display = "none" End Sub ' Causes a refresh Sub UpdateScreen() ' this is used to allow screen updates dim CMD, WSHShell Set WSHShell = CreateObject("WScript.Shell") CMD = WSHShell.ExpandEnvironmentStrings("%comspec%") WSHShell.run CMD + " /c ", 0, true Set WSHShell = Nothing End Sub ' Enables/Disables database name textbox Sub DefaultOptionChanged() if chkDBNameDefault.Checked = True then txtDBName.Disabled = "Disabled" txtDBName.style.backgroundcolor = "#dfe0e5" else txtDBName.Disabled = "" txtDBName.style.backgroundcolor = "#ffffff" end if End Sub </script> </head> <HTA:APPLICATION APPLICATIONNAME="Restore Database Utility" ID="RestoreDBHTA" INNERBORDER="no" > <body leftmargin=30 topmargin=20 rightmargin=30> <table> <tr> <td>Backup File:</td><td><input id="txtFileName" style="width=300px;" type="Text" Value=""/><input type="submit" value="Open" OnClick="SelectBackupFile()" /></td> </tr> <tr> <td>Restore Folder:</td><td><input id="txtRestoreFolder" style="width=300px;" type="Text" Value="" /><input type="submit" value="Open" OnClick="SelectRestoreFolder()" /></td> </tr> <tr> <td>SQL Server Instance:</td> <td> <select name="selInstances"> </select> <input id="chkIntegrated" type="checkbox" checked="true" OnClick="ShowAuth()">Integrated Security</input> </td> <tr id="trUserName" style="display:none"> <td>UserName:</td><td><input id="txtUserName" style="width=200px;" type="Text" /></td> </tr> <tr id="trPassword" style="display:none"> <td>Password</td><td><input id="txtPassword" style="width=200px;" type="password" /></td> </tr> <tr> <td>Database Name:</td><td><input id="txtDBName" style="width=200px;background-color:#dfe0e5" disabled="disabled" type="Text" /><input id="chkDBNameDefault" type="checkbox" checked="true" OnClick="DefaultOptionChanged">Use Default</input></td> </tr> </table> <br/> <input type="submit" value="Restore Database" OnClick="RestoreDatabase()" /> <input type="submit" value="Close" OnClick="Close()" /> <br/> <span id="RestoreInProgress" style="display:none">Database is been restored - this might take a while depending on the size of the database. Please wait...</span> <span id="RestoreCompleted" style="display:none">Restore Completed.</span> <span id="RestoreError"></span> <div id="Footer"> <hr/> <div style="float:left;">Version 1.0</div> <div style="float:right;">By David Wiseman<br /> </div> <div style="clear:both;"></div> <div style="text-align:center;font-size:12px;font-weight:bold"><a href="http://www.wisesoft.co.uk">www.wisesoft.co.uk</a></div> </div> </body></html> |