In the first article of this series, I presented a few simple scripts for automating the most basic SQL Server administrative tasks, such as starting and stopping a SQL Server service and login management (changing passwords and determining mappings between server logins and database users).
The second article covered methods dealing mostly with SQL Server and database maintenance.
In this article, I will continue coverage of WMI-based methods of managing SQL Server by presenting sample code for performing SQL database backup and restore.
SQL Backup can be performed using the SQLBackup method of the MSSQL_SQLServer class. The method uses as its parameter an instance of another class, MSSQL_BackupSettings. The values of its properties control various backup parameters, such as the type of backup (full, differential, file, or log), media and backup set names and descriptions, formatting and initializing options, etc. A description of each parameter is included in the script comments.
sComputer = "SWYNKSRV01"
sSQLServer = "SWYNKSRV01"
Dim sPassword
Dim sMediaPassword
Dim sBackupDescription
Dim sBackupSetName
Dim sBlockSize
Dim sDatabase
Dim aDatabaseFileGroups
Dim aDatabaseFiles
Dim aDevice
Dim bFormatMedia
Dim bInitialize
Dim sMediaDescription
Dim sMediaName
Dim bRestart
Dim iRetainDays
Dim bSkipTapeHeader
Dim sSQLServerName
Dim iTargetType
Dim iTruncateLog
Dim bUnloadTapeAfter
Set oInstance = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/MicrosoftSQLServer:MSSQL_SQLServer.Name=" & _
Chr(34) & sSQLServer & Chr(34))
Set oBackSettings = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/MicrosoftSQLServer").Get("MSSQL_BackupSetting").SpawnInstance_()
sBackupDescription = "Full Backup of SwynkDB on 12/06/02"
sBackupSetname = "Full SwynkDB 12/06/02"
sDatabase = "SwynkDB"
aDevice = Array("C01DiskBkp")
bInitialize = "True"
sMediaDescription = "SwynkDB Backup Media"
sMediaName = "SwynkDB Media"
bRestart = "True"
iRetainDays = 7
iTargetType = 0
iTruncateLog = 0
oBackSettings.BackupSetDescription = sBackupDescription
oBackSettings.BackupSetName = sBackupSetName
oBackSettings.Database = sDatabase
oBackSettings.Device = aDevice
oBackSettings.Initialize = bInitialize
oBackSettings.MediaDescription = sMediaDescription
oBackSettings.MediaName = sMediaName
oBackSettings.Restart = bRestart
oBackSettings.RetainDays = iRetainDays
oBackSettings.SQLServerName = sSQLServer
oBackSettings.TargetType = iTargetType
oBackSettings.TruncateLog = iTruncateLog
sPassword = "BackupPa$$"
sMediaPassword = "MediaPa$$"
Set oOutParam = oInstance.SQLBackup(oBackSettings, sPassword, sMediaPassword)
If oOutParam.ReturnValue = 0 Then
WScript.Echo "SQL Server backup completed successfully"
Else
WScript.Echo "SQL Server backup failed with the error " & oOutParam.Description
End If
You would need to change the provided sample values depending on your SQL Server configuration or type of backup performed. For example, if you wanted to back up only specific files instead of a complete database, you would assign the value to aDatabaseFiles parameter, e.g.:
'-- aDatabaseFiles = Array("SwynkDB_Data","SwynkDB_DataSec")
(where SwynkDB_Data and SwynkDB_DataSec are logical names of database files) and change the value of iTargetType variable to 2 (backup of specified files only). I commented out (by placing an apostrophe at the beginning of the line) parameters that are not relevant in this example. If you want to use them, simply change the value of the appropriate variable and remove the leading apostrophe.
Restoring the backup requires use of the SQLRestore method of the MSSQL_SQLServer class, which, similiar to the Backup method, uses as its parameter instance of another class (MSSQL_RestoreSettings). The following sample code contains descriptions of the parameters used for the backup:
sComputer = "SWYNKSRV01"
sSQLServer = "SWYNKSRV01"
Dim aBackupDevice
Dim sPassword
Dim sMediaPassword
Dim aDatabaseFileGroups
Dim aDatabaseFiles
Dim sDatabaseName
Dim iFileNumber
Dim bLoadHistory
Dim sMediaName
Dim aRelocateFile
Dim bReplaceDatabase
Dim bRestart
Dim dtRestoreTillTime
Dim sSQLServerName
Dim sStandbyFile
Dim iTargetType
Dim bUnloadTapeAfter
Set oInstance = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/MicrosoftSQLServer:MSSQL_SQLServer.Name=" & _
Chr(34) & sSQLServer & Chr(34))
Set oRestSettings = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/MicrosoftSQLServer").Get("MSSQL_RestoreSetting").SpawnInstance_()
aBackupDevice = Array("C01DiskBkp")
sDatabaseName = "SwynkDB"
iFileNumber = 1
bLoadHistory = "True"
sMediaName = "SwynkDB Media"
bReplaceDatabase = "True"
bRestart = "False"
iTargetType = 0
oRestSettings.BackupDevice = aBackupDevice
oRestSettings.DatabaseName = sDatabaseName
oRestSettings.FileNumber = iFileNumber
oRestSettings.MediaName = sMediaName
oRestSettings.ReplaceDatabase = bReplaceDatabase
oRestSettings.Restart = bRestart
oRestSettings.SQLServerName = sSQLServer
oRestSettings.TargetType = iTargetType
sPassword = "BackupPa$$"
sMediaPassword = "MediaPa$$"
Set oOutParam = oInstance.SQLRestore(oRestSettings, sPassword, sMediaPassword)
If oOutParam.ReturnValue = 0 Then
WScript.Echo "SQL Server backup completed successfully"
Else
WScript.Echo "SQL Server backup failed with the error " & oOutParam.Description
End If
As before, you will need to adjust the values of the parameters (and remove comment marks) to match your environment and requirements.
In the next article of this series, I'll present WMI-based methods for performing common maintenance tasks, such as truncating transaction logs or rebuilding indexes and updating statistics on database tables.
See All Articles by Columnist Marcin Policht