利用 PowerShell 做 SQL 數據庫備份

分成三個部份,首先是準備 PowerShell 的 backup script,接著是利用一個 batch file (.BAT) 啟動這個 script 檔案,最後就是在 Task Scheduler 創建一個 task 按時執行這個 batch file 就完成了。

PowerShell script file 稱為 SqlBackup.ps1,內容如下:

#============================================================
# Backup a Database using PowerShell and SQL Server SMO
# Script below creates a full backup
# Donabel Santos
# 2010.10.16 paulus:modified
#============================================================

#specify database to backup
#ideally this will be an argument you pass in when you run
#this script, but let's simplify for now
$dbToBackup = $args[0]

#clear screen
cls

#load assemblies
#note need to load SqlServer.SmoExtended to use SMO backup in SQL Server 2008
#otherwise may get this error
#Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: make sure
#the assembly containing this type is loaded.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for smo.backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

#create a new server object
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"
$backupDirectory = $server.Settings.BackupDirectory

#2010.10.16 paulus: display default backup directory
"Default Backup Directory: " + $backupDirectory

#display current backup database
"Current database: " + $dbToBackup

$db = $server.Databases[$dbToBackup]
$dbName = $db.Name

$timestamp = Get-Date -format yyyyMMddHHmmss
$smoBackup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")

#BackupActionType specifies the type of backup.
#Options are Database, Files, Log
#This belongs in Microsoft.SqlServer.SmoExtended assembly

$smoBackup.Action = "Database"
$smoBackup.BackupSetDescription = "Full Backup of " + $dbName
$smoBackup.BackupSetName = $dbName + " Backup"
$smoBackup.Database = $dbName
$smoBackup.MediaDescription = "Disk"
#2010.10.16 paulus: overwrite
$smoBackup.Initialize = $TRUE
#2010.10.16 paulus: no need for timestamp
#$smoBackup.Devices.AddDevice($backupDirectory + "" + $dbName + "_" + $timestamp + ".bak", "File")
$smoBackup.Devices.AddDevice($backupDirectory + "" + $dbName + ".bak", "File")
$smoBackup.SqlBackup($server)

#let's confirm, let's list list all backup files
$directory = Get-ChildItem $backupDirectory

#list only files that end in .bak, assuming this is your convention for all backup files
$backupFilesList = $directory | where {$_.extension -eq ".bak"}
$backupFilesList | Format-Table Name, LastWriteTime

batch file 稱為 BackupDb.bat,內容如下:

del C:MSSQLBackup*.* /F /Q
powershell -command "& {C:MSSQLSqlBackup.ps1 Club.Secretary}"
powershell -command "& {C:MSSQLSqlBackup.ps1 Club.Secretary.RTIA}"
robocopy C:MSSQLBackup \151-SQL2K8C$MSSQLBackup /Z /W:5 /MIR

第一行,把檔案夾內的舊 backup files 刪除。
第二行,啟動 Powershell script 備份數據庫 Club.Secretary。
第三行,再用 PowerShell script 備份數據庫 Club.Secretary.RTIA。
如此安排是容許選擇性地 backup SQL Server 內的 databases.
第四行,用 RoboCopy 把剛才備份的檔案抄至指定的 computer 151-SQL2K8 去,RoboCopy 用了一個 /MIR 意思是 Mirror, 確保 151-SQL2K8 收到的檔案跟本機完全相同,不多也不少。

最後的設定 Task Scheduler 就不用講了吧?