Skip to content

Instantly share code, notes, and snippets.

@Alex-Yates
Created July 22, 2016 10:04
Show Gist options
  • Save Alex-Yates/efbf9e6be2b65d85bfde3367cf4cafd4 to your computer and use it in GitHub Desktop.
Save Alex-Yates/efbf9e6be2b65d85bfde3367cf4cafd4 to your computer and use it in GitHub Desktop.
DLM Automation PS scripts: Restore Prod DB over Acceptance DB (run before dry run deployments)
param
(
[Parameter(Mandatory=$true)]
[string]$ProductionDB = $null,
[Parameter(Mandatory=$true)]
[string]$AcceptanceDB = $null,
[Parameter(Mandatory=$true)]
[string]$DatabaseServer = $null
)
$ErrorActionPreference = "Stop"
Invoke-Sqlcmd -Query "EXECUTE master..sqlbackup '-SQL ""BACKUP DATABASE [$ProductionDB] TO DISK = ''C:\backups\<AUTO>.sqb'' WITH COMPRESSION = 3""'" -ServerInstance $DatabaseServer -AbortOnError
Invoke-Sqlcmd -Query "EXECUTE master..sqlbackup '-SQL ""RESTORE DATABASE [$AcceptanceDB] FROM DISK = ''C:\Backups\*.sqb'' SOURCE = ''$ProductionDB'' LATEST_FULL WITH RECOVERY, DISCONNECT_EXISTING, MOVE ''$ProductionDB'' TO ''C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\$AcceptanceDB.mdf'', MOVE ''$($ProductionDB)_log'' TO ''C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\$($AcceptanceDB)_log.ldf'', REPLACE""'" -ServerInstance $DatabaseServer -AbortOnError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment