如何使用PowerShell为SQL Server备份指定备份目录

时间:2014-08-13 08:40:34

标签: sql sql-server powershell

我正在尝试使用以下脚本创建备份,但此脚本会将备份创建到默认备份文件夹,如何指定用户定义的文件夹,例如c:\backup

$dbToBackup = "inventory"

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[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

#display default backup directory
"Default Backup Directory: " + $backupDirectory
read-host "stop"
#i put stop here to see the default directory that is C:\Program Files\Microsoft SQL    
Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup 
$db = $server.Databases[$dbToBackup]
$dbName = $db.Name

我修改了脚本,但仍未将新位置显示在屏幕

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral,    
PublicKeyToken=89845dcd8080cc91"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($env:ComputerName)
$server.Properties["BackupDirectory"].Value = "K:\Backup"
$server.Alter()

$backupDirectory = $server.Settings.BackupDirectory

#display default backup directory
"Default Backup Directory: " + $backupDirectory
read-host "Stop"

2 个答案:

答案 0 :(得分:1)

Rafal建议的问题是它将修改实例的所有备份的默认路径。这不是重定向单个备份的安全方法。下面的脚本显示了如何在指定服务器默认路径之外的目标时使用Backup对象创建数据库备份。

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[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

$dbToBackup = "SomeDatabase"
$dbBackupFile = "Drive:\Path\to\database\backup\file.bak"

# no server name will connect to the default instance on the local computer
$server = New-Object Microsoft.SqlServer.Management.Smo.Server

# this object performs the database backup
$backup = New-Object Microsoft.SqlServer.Management.Smo.Backup

# set properties
$backup.Action = "Database"
$backup.BackupSetName = "$dbToBackup Backup"
$backup.BackupSetDescription = "Full backup of $dbToBackup"
$backup.Database = $dbToBackup
$backup.Incremental = $false

# define the file to use for writing backups
$backupFile = New-Object Microsoft.SqlServer.Management.Smo.BackupDeviceItem $dbBackupFile, "Disk"

# add it to the backup job
$backup.Devices.Add($backupFile) | Out-Null

# execute the backup
$backup.SqlBackup($server)

答案 1 :(得分:0)

我想我找到了解决问题的方法。

如果要覆盖备份目录,请将此代码添加到脚本中:

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($env:ComputerName)
$server.Properties["BackupDirectory"].Value = "K:\Backup"
$server.Alter()

请注意,上面的Add-Type命令中指定的程序集版本是SQL Server 2008 R2版本。如果要将此代码段用于SQL Server 2012,则必须找到正确的SMO版本。

代码示例和文本来自网站: http://www.powershellmagazine.com/2013/03/14/pstip-change-sql-server-default-backup-folder-location/