Created
April 25, 2018 19:10
-
-
Save digitalohm/048058c8b1a9a61bbeb9b49808b603a7 to your computer and use it in GitHub Desktop.
multi threaded restore dbatools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get credentials and store them securely in an encrypted xml file | |
if (-NOT (Test-Path "${env:\userprofile}\DBA001.Cred")){ | |
$Credential = Get-Credential | |
$Credential | Export-CliXml -Path "${env:\userprofile}\DBA001.Cred" | |
} | |
#Load the credentials | |
$SqlCredential = Import-CliXml -Path "${env:\userprofile}\DBA001.Cred" | |
# Create a CSV of all the databases that are in the availability group. This updates each time to be complete | |
# The -SqlInstance parameter should be the primary server in the AG | |
$AgDatabases = Get-DbaAgDatabase -SqlCredential $SqlCredential -SqlInstance "<Your Server Name, Port if needed>" | |
$AgDatabases | select DatabaseName | Export-Csv "${env:\userprofile}\ag_servers.csv" | |
$CsvPath = "${env:\userprofile}\ag_servers.csv" | |
#Load the databases into the variable, skipping the first line | |
$DatabasesToRestore = Get-Content -Path $CsvPath | Select-Object -Skip 1 | ConvertFrom-Csv | |
$DatabasesToRestore = $DatabasesToRestore | Foreach {"$($_.DatabaseName)"} | |
# Create a workflow so that the restores can run in parallel, still not sure exactly how this is working | |
workflow mt_command { | |
param ([string[]]$DatabasesToRestore) | |
$DestinationServer = "<Name of server to restore to>" | |
$CredentialsForInline = Import-CliXml -Path "${env:\userprofile}\DBA001.Cred" | |
foreach -parallel ($Database in $DatabasesToRestore){ | |
InlineScript { | |
Restore-DbaDatabase -SqlCredential $Using:CredentialsForInline -SqlInstance $Using:DestinationServer -Path ('\\storageserver\path\servername\'+$Using:Database+'\') -MaintenanceSolutionBackup -DestinationDataDirectory D:\SQLDATA -DestinationLogDirectory L:\SQLLOGS | |
} | |
} | |
} | |
mt_command -DatabasesToRestore $DatabasesToRestore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment