使用powershell备份SQL数据库时出错

ar7v8xwq  于 2023-01-13  发布在  Shell
关注(0)|答案(3)|浏览(146)

我试图备份SQL数据库,但奇怪的事情是采取lpace,几个数据库,我可以用powershell备份,但很少我得到错误

Exception calling "SqlBackup" with "1" argument(s): "Backup failed for Server 
'Server1'. "
At C:\_Scripts\defaultbackup.ps1:40 char:1
+ $smoBackup.SqlBackup($server)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FailedOperationException

$dbToBackup = "test"


#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
[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
"Default Backup Directory: " + $backupDirectory
$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"

$smoBackup.Devices.AddDevice($backupDirectory + "\" + $dbName + "_" + $timestamp +   
".bak",   
"File")

$smoBackup.SqlBackup($server)

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

$smoBackup.设备.添加设备($备份目录+“\”+ $数据库名称+“_”+ $时间戳+“.巴克”,“文件”)
$smoBackup. sql备份($服务器)

确认,列出所有备份文件

$directory =获取子项$备份目录

仅列出以.巴克结尾的文件,假设这是所有备份文件的约定

$backupFilesList = $directory | where {$_.extension -eq ".bak"}
$backupFilesList | Format-Table Name, LastWriteTime
$backupFilesList = $directory | where {$_.extension -eq ".bak"}
$backupFilesList | Format-Table Name, LastWriteTime
vaj7vani

vaj7vani1#

替换

$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"

$srv = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"
$conContext = $srv.ConnectionContext
$conContext.LoginSecure = $True
$conContext.ConnectTimeout = 0
$server = new-object Microsoft.SqlServer.Management.Smo.Server($conContext)
uqcuzwp8

uqcuzwp82#

既然你可以备份很少的文件,而且很少有失败的,看起来脚本是不错的。为什么不添加异常处理来捕获确切的错误呢?你可能想修改你的代码如下

Try
{
    $smoBackup.SqlBackup($server
}
Catch
{
    Write-Output $_.Exception.InnerException
}
sr4lhrrt

sr4lhrrt3#

我知道这是一个旧的线程,但没有完全回答。虽然它帮助我解决了我的问题(这是在一个更高版本的SQL Server 14. 0. 1000. 169)。
我的数据库备份是通过任务调度程序上的Powershell脚本完成的。它会随机失败。同样,当在Powershell IDE中单步执行时,它失败了,并出现上述错误...
使用“1”个参数调用“SqlBackup”时发生异常:“服务器”ABC“的备份失败。”
当通过SMS(v18)中的“任务”手动备份时,它每次都能正常工作。
在阅读ServerConnection的属性时,StatementTimeout属性默认为600秒(10分钟)。我的备份脚本是,一旦DB备份完成,就压缩备份,然后将压缩文件FTP到另一台服务器。查看压缩文件上的时间戳,它们大约是从备份启动开始的10分钟。
因此,我向代码中添加了StatementTimeout行,如下所示,将超时时间加倍为20分钟:

$mySrvConn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$mySrvConn.ServerInstance = $serverName
$mySrvConn.LoginSecure = $false
$mySrvConn.Login = $userName
$mySrvConn.Password = $userPwd
$mySrvConn.StatementTimeout = 1200
$server = new-object Microsoft.SqlServer.Management.SMO.Server($mySrvConn)

这样就解决了问题。一些备份在10分钟内完成,其他备份肯定还没有完全完成。

相关问题