Last active
February 18, 2021 13:05
-
-
Save ilya-chumakov/01d3e408c520ac19b5a4 to your computer and use it in GitHub Desktop.
Simple powershell script to create BACPAC of your SQL Server (or Azure SQL) database. Ready to run from SQL Server Agent job. "Operating system" step type is recommended for SQL 2012 because of http://www.sqlhammer.com/running-powershell-in-a-sql-agent-job/
This file contains 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
#how to run script from cmd | |
#powershell.exe -version 3.0 -ExecutionPolicy Bypass -file "C:\Scripts\SqlServer_Create_Bacpac.ps1" | |
import-module sqlps | |
$ConnectionString = "" | |
$DatabaseName = "" | |
$DacAssembly = "c:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\Microsoft.SqlServer.Dac.dll" | |
$backupFolder = "C:\Backups\" | |
$timestamp = $(get-date -f yyyy-MM-dd-HH-mm-ss) | |
$OutputFile = $backupFolder + $DatabaseName + "-" + $timestamp + ".bacpac" | |
# Load DAC assembly. | |
Write-Host "Loading Dac Assembly: $DacAssembly" | |
Add-Type -Path $DacAssembly | |
Write-Host "Dac Assembly loaded." | |
# Initialize Dac service. | |
$now = $(Get-Date).ToString("HH:mm:ss") | |
$Services = new-object Microsoft.SqlServer.Dac.DacServices $ConnectionString | |
if ($Services -eq $null) | |
{ | |
exit | |
} | |
# Start the actual export. | |
Write-Host "Starting backup at $DatabaseName at $now" | |
$Watch = New-Object System.Diagnostics.StopWatch | |
$Watch.Start() | |
$Services.ExportBacpac($OutputFile, $DatabaseName) | |
$Watch.Stop() | |
Write-Host "Backup completed in" $Watch.Elapsed.ToString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment