使用电源shell无法恢复超过15GB的大型数据库

时间:2016-05-02 07:41:41

标签: database powershell sql-server-2012 smo

我正在使用以下脚本来恢复大小超过15 GB的数据库,并且mdf和ldf需要超过100GB的空间。但是我在驱动器上有足够的空间,我可以通过SSMS手动恢复它。 但是当我使用PowerShell脚本时,我收到了这个错误。 我在我的机器上使用sql server 2012。 此脚本能够以较小的大小恢复数据库 我正在使用SMO进行恢复。

请帮帮我。任何其他代码库也不胜感激。

  

使用“1”参数调用“SqlRestore”的异常:“服务器'localhost'的恢复失败。”   在D:\ CI \ RestoreDb.ps1:65 char:9   + $ smoRestore.SqlRestore($ server)   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:FailedOperationException

        function Invoke-DatabaseRestore {

    param ([String]$SQLServer="(local)",
                    $BackupPath,[String]$DataBaseName, 
                    [String]$BackupFileFilter = "")                 

                             $server = New-Object ( "Microsoft.SqlServer.Management.Smo.Server" ) $SQLServer 
                             if ($server.Databases[$DataBaseName] -ne $null)  
                            {  
                                $server.KillDatabase($DataBaseName)
                                #$server.databases[$DataBaseName].drop()                
                            }
                         # Get-ChildItem $BackupPath -Filter $BackupFileFilter | select fullname | % {            $backupFile = $_.FullName

                    #we will query the database name from the backup header later
                    #$server = New-Object ( "Microsoft.SqlServer.Management.Smo.Server" ) $SQLServer
                    $backupDevice = New-Object( "Microsoft.SqlServer.Management.Smo.BackupDeviceItem" ) ($BackupPath, "File")
                    $smoRestore = new-object( "Microsoft.SqlServer.Management.Smo.Restore" )
                    $backupDevice| FL *

                    #Get default log and data file locations
                    $DataPath ="D:\MSSQL\Data" 
                    $LogPath = "D:\MSSQL\Log"

            if(!(Test-Path($DataPath+"\"+$DataBaseName)))
                    {
                            New-Item -Name $DataBaseName -Path $DataPath -ItemType Directory -Force
                    }
                    if(!(Test-Path($LogPath+"\"+$DataBaseName)))
                    {
                            New-Item -Name $DataBaseName -Path $LogPath -ItemType Directory -Force
                    }
                    $DataPath = $DataPath+"\"+$DataBaseName
                    $LogPath = $LogPath+"\"+$DataBaseName

                    #restore settings
                    $smoRestore.NoRecovery = $false;
                    $smoRestore.ReplaceDatabase = $true;
                    $smoRestore.Action = "Database"
                    $smoRestore.PercentCompleteNotification = 10;
                    $smoRestore.Devices.Add($backupDevice)


                    #get database name from backup file
                    $smoRestoreDetails = $smoRestore.ReadBackupHeader($server)

                    #give a new database name
                    $smoRestore.Database = $DataBaseName # $smoRestoreDetails.Rows[0]["DatabaseName"]

                    #Relocate each file in the restore to the default directory
                    $smoRestoreFiles = $smoRestore.ReadFileList($server)

                    foreach ($File in $smoRestoreFiles) {
                            #Create relocate file object so that we can restore the database to a different path
                            $smoRestoreFile = New-Object( "Microsoft.SqlServer.Management.Smo.RelocateFile" )

                            #the logical file names should be the logical filename stored in the backup media
                            $smoRestoreFile.LogicalFileName = $File.LogicalName

                            $smoRestoreFile.PhysicalFileName = $( if($File.Type -eq "L") {$LogPath} else {$DataPath} ) + "\" + [System.IO.Path]::GetFileName($File.PhysicalName)
                            $smoRestore.RelocateFiles.Add($smoRestoreFile)
                    }
                    #restore database
                    $smoRestore.SqlRestore($server)

            }


        #load assemblies
                         [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
                         [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
                         [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
                         [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

            $SqlInstance = 'HYDHTC233832D'
            $DatabaseName = "xyz"
            $BackupPath = "D:\xyz.bak"



        Invoke-DatabaseRestore  -SQLServer  $SqlInstance  -BackupPath $BackupPath -DataBaseName $DatabaseName                   

1 个答案:

答案 0 :(得分:0)

这里的问题是由于大型数据库导致的连接超时。 添加 $ conn.StatementTimeout = 10000 解决了我的问题:) 谢谢