我正在尝试将 LAN 上的远程数据库备份到网络共享驱动器。我使用 .NET 使用相同的方法进行了测试,它确实有效,但我计划在没有 Jenkins 持续集成工具的情况下使用这个 PowerShell 脚本。
$dbserver = "dbserver"
$location = "\\otherserver\Temp\"
$user = "user"
$pwd = "password"
$timestamp=((get-date).toString("yyyy_MM_dd_hh_mm"))
$file = $location + "jira_" + $timestamp + ".bak"
[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
$connection = New-Object "Microsoft.SqlServer.Management.Common.ServerConnection"
$connection.ServerInstance =$dbserver
$connection.LoginSecure = $false
$connection.Login = $user
$connection.Password = $pwd
$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $connection
$backup = New-Object "Microsoft.SqlServer.Management.Smo.backup"
$backup.Action = 'Database'
$device = New-Object ('Microsoft.SqlServer.Management.Smo.BackupDeviceItem') ($file, 'File')
#$device.DeviceType = 'File'
#$device.Name = $file
$backup.MediaDescription = "Disk"
$backup.Database= "jira"
$backup.Devices.Add($device)
$backup.SqlBackup
我没有收到任何异常,但也没有任何信息可以帮助我对代码进行故障排除。使用所有可用的 throw, catch, try 我只能得到。
MemberType : Method
OverloadDefinitions : {System.Void SqlBackup(Microsoft.SqlServer.Management.Smo.Server srv)}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : System.Void SqlBackup(Microsoft.SqlServer.Management.Smo.Server srv)
Name : SqlBackup
IsInstance : True