Skip to content

Instantly share code, notes, and snippets.

@vkhazin
Created April 10, 2020 15:45
Show Gist options
  • Save vkhazin/c97c1175ddedb58837eca2760f6c63e5 to your computer and use it in GitHub Desktop.
Save vkhazin/c97c1175ddedb58837eca2760f6c63e5 to your computer and use it in GitHub Desktop.
PowerShell restore Sql Server database
$AZFileShareURl = "tsqlbackup.file.core.windows.net"
$AZUserName = "Azure\tsqlbackup"
$AZPassword = "7RODyFgfsXx1nML/SfFifKL+ozoCVag89D8B/bCgTbDakqaB7gSVM41BzcF0uvRHugmHRMLYoPyecAzJGPzxrA=="
$BackupFileName = "sqlbackup\DBExport.bak"
$DownloadLocation = "f:\DBExport.bak"
$SQLServer = "VM2016"
$BackupFileLocation = "f:\DBExport.bak"
$DatabaseName = "DBTest"
$DataFileLocation = "f:\db_datafile2.mdf"
$LogFileLocation = "f:\db_dataLogfile2.ldf"
$DataFileName = "DBExport_Data"
$LogFileName = "DBExport_Log"
function DownloadBackup(){
Param (
[Parameter(Mandatory=$true)][string]$AZFileShar3eURl,
[Parameter(Mandatory=$true)][string]$AZUserName,
[Parameter(Mandatory=$true)][string]$AZPassword,
[Parameter(Mandatory=$true)][string]$BackupFileName,
[Parameter(Mandatory=$true)][string]$DownloadLocation
)
$connectTestResult = Test-NetConnection -ComputerName $AZFileShareURl -Port 445
if ($connectTestResult.TcpTestSucceeded) {
LogMessage -Message "Test connection to Azure file Share $AZFileShareURl completed successfully"
cmd.exe /C "cmdkey /add:`"AZFileShareURl`" /user:`"$AZUserName`" /pass:`"$AZPassword`""
$fullFileName = "\\$AZFileShareURl\$BackupFileName"
LogMessage -Message "Start downloading backup file to location "
Copy-Item -Destination $DownloadLocation -Path $fullFileName
LogMessage -Message "Download backup file completed successfully"
} else
{
LogMessage -Message "Unable to reach the Azure storage account via port 445. Check to make sure your organization or ISP is not blocking port 445, or use Azure P2S VPN, Azure S2S VPN, or Express Route to tunnel SMB traffic over a different port."
}
}
function Restore-SQLDataBase() {
Param (
[Parameter(Mandatory=$true)][string]$SQLServer,
[Parameter(Mandatory=$true)][string]$BackupFileLocation,
[Parameter(Mandatory=$true)][string]$DatabaseName,
[Parameter(Mandatory=$true)][string]$DataFileLocation,
[Parameter(Mandatory=$true)][string]$LogFileLocation,
[Parameter(Mandatory=$true)][string]$DataFileName,
[Parameter(Mandatory=$true)][string]$LogFileName
)
#build connection string
$connstring = "Server=$SQLServer; Database=master; Trusted_Connection=Yes; Integrated Security=SSPI;"
#connect to database
$connection = New-Object System.Data.SqlClient.SqlConnection($connstring)
$connection.Open()
#build query object
$tsqlRestoreCommand = 'RESTORE DATABASE @dbName FROM DISK = @backupFileLocation WITH FILE = 1, MOVE @dataFileName TO @dataFileLocation, MOVE @logFileName TO @logFileLocation, NOUNLOAD, REPLACE, STATS = 5'
$command = $connection.CreateCommand()
$command.CommandText = $tsqlRestoreCommand
$command.CommandTimeout = 0
$command.Parameters.AddWithValue("@dbName", $DatabaseName);
$command.Parameters.AddWithValue("@backupFileLocation", $BackupFileLocation);
$command.Parameters.AddWithValue("@dataFileName", $DataFileName);
$command.Parameters.AddWithValue("@dataFileLocation", $DataFileLocation);
$command.Parameters.AddWithValue("@logFileName", $LogFileName);
$command.Parameters.AddWithValue("@logFileLocation", $LogFileLocation);
#run query
$command.ExecuteNonQuery();
$connection.Close()
LogMessage -Message "Restore Database completed successfully";
}
function LogMessage(){
Param ([Parameter(Mandatory=$true)][string]$Message)
Write-Output $Message
}
try {
DownloadBackup -AZFileShareURl $AZFileShareURl -AZUserName $AZUserName -AZPassword $AZPassword -BackupFileName $BackupFileName -DownloadLocation $DownloadLocation
Restore-SQLDataBase -SQLServer $SQLServer -BackupFileLocation $DownloadLocation -DatabaseName $DatabaseName -DataFileName $DataFileName -DataFileLocation $DataFileLocation -LogFileName $LogFileName -LogFileLocation $LogFileLocation
}
Catch {
Write-Host "An error occurred."
Write-Host $_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment