2017-06-05 128 views
0

我打算使用Powershell自動執行SQL Server的恢復過程。我在執行下面的PowerShell腳本時沒有遇到任何錯誤。任何幫助將不勝感激。我無法成功執行下面的PowerShell腳本。PowerShell恢復數據庫(SQL Server)

[string] $SourceServer= ".\LOCAL" 
[string] $DestinationServer= ".\LOCAL2" 
[string] $SourceDatabase = "msdb" 
[string] $DestinationDatabase = "master" 
[string] $RestoreDatabase="test1" 
[string] $DataFolder='C:\Program Files\Microsoft SQL Server\MSSQL11.LOCAL2\MSSQL\DATA' 
[string] $LogFolder='C:\Program Files\Microsoft SQL Server\MSSQL11.LOCAL2\MSSQL\DATA' 

[string] $FullBackupSourceQuery= $("SELECT REPLACE (c.physical_device_name,'C:\SQL Server\Backup\','\\servername\Backup\') 
FROM msdb.dbo.backupset a 
INNER JOIN (SELECT database_name , backupdate = MAX(backup_finish_date) 
FROM msdb.dbo.backupset 
WHERE type = 'D' AND 
backup_finish_date >= DATEADD(MONTH , -1 , GETDATE()) 
AND database_name =$RestoreDatabase 
GROUP BY database_name)b 
ON a.database_name=b.database_name 
AND a.backup_finish_date=b.backupdate 
INNER JOIN msdb.dbo.backupmediafamily c 
ON c.media_set_id = a.media_set_id") 

[string] $DiffBackupSourceQuery= $("SELECT REPLACE (c.physical_device_name,'C:\SQL Server\Backup\','\\servername\Backup\') 
FROM msdb.dbo.backupset a 
INNER JOIN (SELECT database_name , backupdate = MAX(backup_finish_date) 
FROM msdb.dbo.backupset 
WHERE type = 'I' AND 
backup_finish_date >= DATEADD(MONTH , -1 , GETDATE()) 
AND database_name =$RestoreDatabase 
GROUP BY database_name)b 
ON a.database_name=b.database_name 
AND a.backup_finish_date=b.backupdate 
INNER JOIN msdb.dbo.backupmediafamily c 
ON c.media_set_id = a.media_set_id") 

[string] $DestinationQuery=$(" 
EXEC master..RestoreDatabase 
     @BackupFile = '$value', 
     @NewDatabaseName = '$RestoreDatabase', 
     @AdditionalOptions='STATS=5, REPLACE, NORECOVERY', 
     @DataFolder = '$DataFolder', 
     @LogFolder = '$LogFolder', 
     @ExecuteRestoreImmediately = 'Y' 

EXEC master..RestoreDatabase 
     @BackupFile = '$value1', 
     @NewDatabaseName = '$RestoreDatabase', 
     @AdditionalOptions='STATS=5, REPLACE, RECOVERY', 
     @DataFolder = '$DataFolder', 
     @LogFolder = '$LogFolder', 
     @ExecuteRestoreImmediately = 'Y' 
") 

function GenericSqlQuery ($SourceServer, $SourceDatabase, $SourceQuery, $DestinationServer, $DestinationDatabase, $DestnationQuery) 
{ 
    { 
    $SourceConnection = New-Object System.Data.SQLClient.SQLConnection 
    $SourceConnection.ConnectionString = "server='$SourceServer';database='$SourceDatabase';trusted_connection=true;" 
    $SourceConnection.Open() 
    $SourceCommand = New-Object System.Data.SQLClient.SQLCommand 
    $SourceCommand.Connection = $Connection 
    $SourceCommand.CommandText = $FullBackupSourceQuery 
    $SourceReader = $Command.ExecuteReader() 
    while ($SourceReader.Read()) { 
     $value=$SourceReader.GetValue($1) 
            } 
    $SourceCommand.CommandText = $DiffBackupSourceQuery 

    $SourceReader = $Command.ExecuteReader() 
    while ($SourceReader.Read()) { 
     $value1=$SourceReader.GetValue($1) 
           } 
    } 
     $SourceConnection.Close() 
    { 
    $DestinationConnection = New-Object System.Data.SQLClient.SQLConnection 
    $DestinationConnection.ConnectionString = "server='$DestinationServer';database='$DestinationDatabase';trusted_connection=true;" 
    $DestinationConnection.Open() 
    $DestinationCommand = New-Object System.Data.SQLClient.SQLCommand 
    $DestinationCommand.Connection = $Connection 
    $DestinationCommand.CommandText = $DestinationQuery 
    $DestinationReader = $Command.ExecuteReader() 
    } 
    $DestinationConnection.Close() 
} 
+0

檢查以下dbatools系統管理命令:https://dbatools.io/functions/restore-dbadatabase/,https://dbatools.io/函數/ restore-sqlbackupfromdirectory /。完整的dbatools命令列表可以在這裏找到:https://dbatools.io/functions/ –

回答

1

馬上你的功能GenericSqlQuery含有一種叫$1變量,我沒有看到設置任何東西。在perl中,這是一個特殊的變量,但在PowerShell中,只有將其設置爲某種東西時纔有意義。

正如所指出的,儘管包括日誌記錄和驗證的更有效的方法是利用dbatools模塊進行恢復。 [披露我是一個貢獻者,這個項目]

#if not installed 
Install-Module dbatools 

# import it 
Import-Module dbatools 

# Restore it 
$sourceServer = '.\LOCAL' 
$DestServer = '.\LOCAL2' 
$RestoreDb = 'test1' 
$DataFolder='C:\Program Files\Microsoft SQL Server\MSSQL11.LOCAL2\MSSQL\DATA' 
$LogFolder='C:\Program Files\Microsoft SQL Server\MSSQL11.LOCAL2\MSSQL\DATA' 

# IF you need to get backup history 
Get-DbaRestoreHistory -SqlServer $sourceServer -Databases $RestoreDb | 
    Restore-DbaDatabase -SqlServer $DestServer -DestinationDataDirectory $DataFolder -DestinationLogDirectory $LogFolder 

# IF you just want to base it on backup folder, will SCAN complete folder 
$dbBackupPath = "\\servername\Backup\$RestoreDb" 
Restore-DbaDatabase -SqlServer $DestServer -Path $dbBackupPath -DestinationDataDirectory $DataFolder -DestinationLogDirectory $LogFolder 
相關問題