Home
%3CLINGO-SUB%20id%3D%22lingo-sub-368864%22%20slang%3D%22en-US%22%3EHow%20to%20automate%20Export%20Azure%20SQL%20DB%20to%20blob%20storage%20use%20Automation%20account%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-368864%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Feb%2007%2C%202017%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20I%20had%20a%20support%20case%20recently%26nbsp%3Bquestion%20how%20to%20automate%20Export%20%3CSTRONG%3E%20Azure%20SQL%20DB%20%3C%2FSTRONG%3E%20to%20a%20storage%20account%20%3CEM%3E%20(Blob%20container)%20%3C%2FEM%3E%20to%20.bacpac%20files.%20The%20first%20attempt%20made%20to%20configure%20a%20PowerShell%20Script%20to%20Export%20Azure%20SQL%20DB%20to%20blob%20container%20and%26nbsp%3Bto%20.bacpac%20format%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-export-powershell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20.%20this%20trial%20successfully%20completed%20but%20on%20demand%2C%20the%20coming%20questions%20is.%20How%20can%20we%20run%20this%20PowerShell%20script%20into%20a%20scheduled%20job%3F%20as%20you%20all%20know%20%3CSTRONG%3E%20Azure%20SQL%20DB%20%3C%2FSTRONG%3E%20have%20no%20SQL%20Agent%20in%20place.%20One%20way%20to%20do%20this%20either%20from%20client%20side%20or%26nbsp%3Busing%20Azure%20VM%26nbsp%3Bto%20schedule%20run%20PowerShell%20script!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20That%60s%20fine%2C%20but%20what%20if%20you%20can%20do%20this%20from%20Azure%20Portal%2C%20yes!%20you%20can%3B%20using%20Automation%20account%20runbooks%20feature.%20With%20no%20dependency%20on%20client%20or%20Azure%20VM%20schedule%20task%20to%26nbsp%3Bexecute%26nbsp%3BPowerShell%20export%20script%20for%20you!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%60ve%20searched%20for%20a%20complete%20document%20can%20demonstrate%20how%20to%20achieve%20this.%20Unfortunately%2C%20I%20did%20not%20find%20any.%20So%2C%20in%20this%20blog%2C%20we%20will%20walk%20through%20how%20to%20accomplish%20this%20step-by-step%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20PREREQUISITES%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20complete%20this%20exercise%2C%20you%20should%20have%20following%20resources%20in%20place%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201)%20Azure%20SQL%20Server%20and%20DB%2C%20for%20more%20reference%20how%20to%20create%20Azure%20SQL%20DB%2C%20Server%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-get-started%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202)%20Storage%20account%20created%20and%20configured%20include%20blob%20container%20for%20remote%20backup%20SQL%20DB.%20For%20more%20reference%20how%20to%20create%20new%20storage%20account%20and%20associated%20blob%20container%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fstorage%2Fstorage-create-storage-account%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20STEPS%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201)%20Go%20to%20%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fportal.azure.com%2F%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202)%20From%20More%20services%20in%20the%20bottom%20on%20the%20left%20hand%20search%20for%20%3CSTRONG%3E%20%22Automation%20Accounts%22%20%3C%2FSTRONG%3E%20%2C%20select%2C%20this%20will%20open%20Automation%20account%20blade%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203)%20Click%20%3CSTRONG%3E%20Add%20%3C%2FSTRONG%3E%20to%20create%20a%20new%20Automation%20account%2C%20type%20in%20Name%20field%3A%20%3CEM%3E%20%22MyAutomationAccount%22%20%3C%2FEM%3E%20%20%20or%20as%20you%20like%20to%20name%20your%20Automation%20account%2C%20select%20desired%20subscription%20if%20you%20register%20more%20than%20one%20subscription.%20(make%20sure%20all%20resources%20%3CEM%3E%20(SQL%20DB%2C%20Server%2C%20storage%20account)%20%3C%2FEM%3E%20existing%20in%20the%20same%20subscription%20as%20you%20create%20automation%20account).%20Use%20or%20create%20a%20new%20Resource%20group.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Leave%20Create%20Azure%20Run%20As%20account%20as%20default%20(Yes).%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204)%20First%2C%20make%20sure%20%3CEM%3E%20%22AzureClassicRunAsConnection%22%20%3C%2FEM%3E%20%20%20and%20%3CEM%3E%20%22AzureRunAsConnection%22%20%3C%2FEM%3E%20%20%20%3CSTRONG%3E%20connections%20%3C%2FSTRONG%3E%20created%20automatically%20during%20Automation%20account%20creation.%20Note%3A%20(%20%3CEM%3E%20%22AzureRunAsConnection%22%20%3C%2FEM%3E%20%20%20connection%20will%20be%20used%20in%20our%20PowerShell%20workflow%20script%20later%20to%20authenticate%20to%20Azure%20subscription)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205)%20Second%2C%20Verify%20that%20your%20Automation%20account%20has%20latest%20%3CSTRONG%3E%20modules%20%3C%2FSTRONG%3E%20imported%20and%20are%20up%20to%20date%20(AzureRM.Automation%20-%20AzureRM.Profile%20-%20AzureRM.Sql)%20to%20Version%3A%202.5.0%20(Current%20available%20%3CSTRONG%3E%20Version%3A%203.4.0%20%3C%2FSTRONG%3E%20)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206)%20If%20modules%20show%20a%20different%20version%2C%20%3CSTRONG%3E%20i.e.%26nbsp%3B1.0.3%20%3C%2FSTRONG%3E%20select%20modules%20show%20old%20version%26nbsp%3Band%20Import%20to%20update%20to%20version%202.5.0%20(Current%20available%20%3CSTRONG%3E%20Version%3A%203.4.0%20%3C%2FSTRONG%3E%20)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207)%20Third%2C%20Import%20PowerShell%20script%20Update-ModulesInAutomationToLatestVersion%20from%20runbook%20gallery%20and%20make%20sure%20it%20is%20published%20%3CSTRONG%3E%20(without%20this%20PowerShell%20script%20the%20Export%20PowerShell%20workflow%20will%20not%20work)%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20import%20Update-ModulesInAutomationToLatestVersion%20PowerShell%20script%20from%20runbook%20Gallery%2C%20follow%20below%20steps%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20a.%20Go%20to%20automation%20account%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20b.%20Select%20runbooks%20from%20middle%20blade%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20c.%20From%20top%20bar%20select%20%3CSTRONG%3E%20Browse%20gallery%20%3C%2FSTRONG%3E%20%20%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20d.%20Set%20the%20filter%20%3CSTRONG%3E%20Gallery%20Source%20%3C%2FSTRONG%3E%20%20%20to%20%3CSTRONG%3E%20PowerShell%20Gallery%20%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20e.%20Search%20for%20Update-ModulesInAutomationToLatestVersion%20PowerShell%20script%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20f.%20Select%20this%20script%20and%20import%20it%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20g.%20Revert%20to%20runbooks%20again%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20h.%20Select%20recently%20imported%20PowerShell%20script%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20i.%20From%20top%20bar%20select%20%3CSTRONG%3E%20Edit%20%3C%2FSTRONG%3E%20%2C%20then%20%3CSTRONG%3E%20Publish%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20j.%20Verify%20that%20this%20script%20in%20%3CSTRONG%3E%20Published%20%3C%2FSTRONG%3E%20state%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Now%20we%20will%26nbsp%3Bimport%20the%20PowerShell%20workflow%20script!%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201)%20From%20Automation%20account%20overview%20dashboard%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202)%20Select%20runbooks%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203)%20Click%20%3CSTRONG%3E%20Add%20a%20runbook%20%3C%2FSTRONG%3E%20(make%20sure%20you%20select%20PowerShell%20Workflow%20from%20category%20not%20PowerShell!)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204)%20Select%20Import%20an%20existing%20runbook%20and%20select%20to%20add%20%3CSTRONG%3E%20ExportAzureDB-PowerShellWorkflow%20%3C%2FSTRONG%3E%20(PowerShell%20script%20code%20below)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205)%20Once%20new%20runbook%20created%2C%20it%20will%20present%20in%20runbooks%20in%20new%20state!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206)%20Select%20%3CSTRONG%3E%20ExportAzureDB-PowerShellWorkflow%20%3C%2FSTRONG%3E%20and%20from%20top%20bar%20click%20%3CSTRONG%3E%20Edit%20%3C%2FSTRONG%3E%20%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207)%20Update%20required%20parameters%20match%20your%20environment%20and%20%3CSTRONG%3E%20Save%20%3C%2FSTRONG%3E%20%2C%20then%20%3CSTRONG%3E%20Test%20pane%20%3C%2FSTRONG%3E%20to%20start%20run%20this%20runbook%20to%20test%20result%20before%20set%20to%20Publish%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%208)%3C%2Fimg%3E%20Once%20%3CSTRONG%3E%20Test%20pane%20%3C%2FSTRONG%3E%20completed%2C%20click%20%3CSTRONG%3E%20Published%20%3C%2FSTRONG%3E%20%2C%20then%20you%20can%20assign%20a%20schedule%20for%20this%20runbook%20per%20your%20preference%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%209)%20Review%20storage%20blob%20container%20for%20successfully%20exported%20.bacpac%20file%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%2010)%20You%20can%20review%20completed%20jobs%20from%20runbooks%20jobs%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Updated%20PowerShell%20script%3A%20(2nd%20October%2C%202017)%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Add%20function%20to%20copy%20source%20DB%20on%20the%20same%20server%20and%20export%20copied%20DB%20to%20blob%20storage.%20then%20drop%20copied%20database.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22PowerShell%22%5D%26lt%3B%23%20%3CBR%20%2F%3E%20.SYNOPSIS%20%3CBR%20%2F%3E%20Export%20Azure%20SQL%20DB%20to%20blob%20storage%20in%20.bacpac%20format%20%3CBR%20%2F%3E%20.DESCRIPTION%20%3CBR%20%2F%3E%20This%20PowerShell%20workflow%20runbook%20script%20copy%20Azure%20SQL%20DB%20and%20Export%20copied%20database%20to%20blob%20storage%20container%20use%20below%20parameters.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20ServerName%20%3CBR%20%2F%3E%20Name%20of%20the%20SqlServer%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20DatabaseName%20%3CBR%20%2F%3E%20Name%20of%20the%20database%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20CopyDatabaseName%20%3CBR%20%2F%3E%20Name%20of%20the%20Copydatabase%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20ResourceGroupName%20%3CBR%20%2F%3E%20Name%20of%20resource%20group%20contains%20the%20SqlServer%20and%20DatabaseName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20%24ServerAdmin%20%3CBR%20%2F%3E%20Name%20of%20the%20Server%20admin%20Login%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20%24serverPassword%20%3CBR%20%2F%3E%20Input%20Server%20admin%20password%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20%24BaseStorageUri%20%3CBR%20%2F%3E%20Full%20uri%20for%20storage%20account%20name%20include%20container%20name%20%3CA%20href%3D%22https%3A%2F%2FSTORAGE-NAME.blob.core.windows.net%2FBLOB-CONTAINER-NAME%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2FSTORAGE-NAME.blob.core.windows.net%2FBLOB-CONTAINER-NAME%2F%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.PARAMETER%20%24StorageKey%20%3CBR%20%2F%3E%20Storage%20account%20access%20key%20%22YOUR%20STORAGE%20KEY%22%20go%20to%20storage%20account%20--%26gt%3B%20settings%20--%26gt%3B%20select%20Access%20Keys%20--%26gt%3B%20Copy%2FPaste%20key1%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20.NOTES%20%3CBR%20%2F%3E%20This%20script%20provided%20AS%20IS%2C%20Please%20review%20the%20code%20before%20executing%20this%20on%20production%20environment%20%3CBR%20%2F%3E%20For%20any%20issue%20or%20suggestion%20please%20email%20to%3A%20mobaioum%40microsoft.com%20%3CBR%20%2F%3E%20%23%26gt%3B%20%3CBR%20%2F%3E%20%23%20----%20Login%20to%20Azure%20----%20%3CBR%20%2F%3E%20workflow%20ExportAzureDB-PowerShellWorkflowv2%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20param%20%3CBR%20%2F%3E%20(%20%3CBR%20%2F%3E%20%23%20Name%20of%20the%20Azure%20SQL%20Database%20server%20%3CBR%20%2F%3E%20%5Bparameter(Mandatory%3D%24true)%5D%20%3CBR%20%2F%3E%20%5Bstring%5D%20%24ServerName%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Source%20Azure%20SQL%20Database%20name%20%3CBR%20%2F%3E%20%5Bparameter(Mandatory%3D%24true)%5D%20%3CBR%20%2F%3E%20%5Bstring%5D%20%24DatabaseName%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Target%20Azure%20SQL%20Database%20name%20%3CBR%20%2F%3E%20%5Bparameter(Mandatory%3D%24true)%5D%20%3CBR%20%2F%3E%20%5Bstring%5D%20%24CopyDatabaseName%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Resource%20Group%20Name%20%3CBR%20%2F%3E%20%5Bparameter(Mandatory%3D%24true)%5D%20%3CBR%20%2F%3E%20%5Bstring%5D%20%24ResourceGroupName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20inlineScript%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24connectionName%20%3D%20%22AzureRunAsConnection%22%20%3CBR%20%2F%3E%20try%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Get%20the%20connection%20%22AzureRunAsConnection%20%22%20%3CBR%20%2F%3E%20%24servicePrincipalConnection%3DGet-AutomationConnection%20-Name%20%24connectionName%20%3CBR%20%2F%3E%20%22Login%20to%20Azure%22%20%3CBR%20%2F%3E%20Add-AzureRmAccount%20%60%20%3CBR%20%2F%3E%20-ServicePrincipal%20%60%20%3CBR%20%2F%3E%20-TenantId%20%24servicePrincipalConnection.TenantId%20%60%20%3CBR%20%2F%3E%20-ApplicationId%20%24servicePrincipalConnection.ApplicationId%20%60%20%3CBR%20%2F%3E%20-CertificateThumbprint%20%24servicePrincipalConnection.CertificateThumbprint%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20catch%20%7B%20%3CBR%20%2F%3E%20if%20(!%24servicePrincipalConnection)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24ErrorMessage%20%3D%20%22Connection%20%24connectionName%20not%20found.%22%20%3CBR%20%2F%3E%20throw%20%24ErrorMessage%20%3CBR%20%2F%3E%20%7D%20else%7B%20%3CBR%20%2F%3E%20Write-Error%20-Message%20%24_.Exception%20%3CBR%20%2F%3E%20throw%20%24_.Exception%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20convert%20server%20admin%20password%20to%20secure%20string%20%3CBR%20%2F%3E%20%24serverAdmin%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24serverPassword%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24securePassword%20%3D%20ConvertTo-SecureString%20-String%20%24serverPassword%20-AsPlainText%20-Force%20%3CBR%20%2F%3E%20%24creds%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-ArgumentList%20%24serverAdmin%2C%20%24securePassword%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Generate%20a%20unique%20filename%20for%20the%20BACPAC%20%3CBR%20%2F%3E%20%24bacpacFilename%20%3D%20%22%24Using%3ACopyDatabaseName%22%20%2B%20(Get-Date).ToString(%22yyyy-MM-dd-HH-mm%22)%20%2B%20%22.bacpac%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Storage%20account%20info%20for%20the%20BACPAC%20%3CBR%20%2F%3E%20%24BaseStorageUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2FSTORAGE-NAME.blob.core.windows.net%2FBLOB-CONTAINER-NAME%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2FSTORAGE-NAME.blob.core.windows.net%2FBLOB-CONTAINER-NAME%3C%2FA%3E%22%20%3CBR%20%2F%3E%20%24BacpacUri%20%3D%20%24BaseStorageUri%20%2B%20%22%2FDaily%2F%22%20%2B%20%24bacpacFilename%20%3CBR%20%2F%3E%20%24StorageKeytype%20%3D%20%22StorageAccessKey%22%20%3CBR%20%2F%3E%20%24StorageKey%20%3D%20%22YOUR%20STORAGE%20KEY%22%20%3CBR%20%2F%3E%20New-AzureRmSqlDatabaseCopy%20-ResourceGroupName%20%22%24Using%3AResourceGroupName%22%20-ServerName%20%22%24Using%3AServerName%22%20-DatabaseName%20%22%24Using%3ADatabaseName%22%20%60%20%3CBR%20%2F%3E%20-CopyResourceGroupName%20%22%24Using%3AResourceGroupName%22%20-CopyServerName%20%22%24Using%3AServerName%22%20-CopyDatabaseName%20%22%24Using%3ACopyDatabaseName%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Write-Output%20%22Azure%20SQL%20DB%20%22%24Using%3ACopyDatabaseName%22%20Copy%20completed%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Write-Output%20%22Azure%20SQL%20DB%20%22%24Using%3ACopyDatabaseName%22%20Export%20Started%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24exportRequest%20%3D%20New-AzureRmSqlDatabaseExport%20-ResourceGroupName%20%22%24Using%3AResourceGroupName%22%20-ServerName%20%22%24Using%3AServerName%22%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%22%24Using%3ACopyDatabaseName%22%20-StorageKeytype%20%24StorageKeytype%20-StorageKey%20%24StorageKey%20-StorageUri%20%24BacpacUri%20%60%20%3CBR%20%2F%3E%20-AdministratorLogin%20%24creds.UserName%20-AdministratorLoginPassword%20%24creds.Password%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Check%20status%20of%20the%20export%20%3CBR%20%2F%3E%20%24exportStatus%20%3D%20Get-AzureRmSqlDatabaseImportExportStatus%20-OperationStatusLink%20%24exportRequest.OperationStatusLink%20%3CBR%20%2F%3E%20%5BConsole%5D%3A%3AWrite(%22Exporting%22)%20%3CBR%20%2F%3E%20while%20(%24exportStatus.Status%20-eq%20%22InProgress%22)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24exportStatus%20%3D%20Get-AzureRmSqlDatabaseImportExportStatus%20-OperationStatusLink%20%24exportRequest.OperationStatusLink%20%3CBR%20%2F%3E%20Start-Sleep%20-s%2010%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%24exportStatus%20%3CBR%20%2F%3E%20%24Status%3D%20%24exportStatus.Status%20%3CBR%20%2F%3E%20if(%24Status%20-eq%20%22Succeeded%22)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Output%20%22Azure%20SQL%20DB%20Export%20%24Status%20for%20%22%24Using%3ACopyDatabaseName%22%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Write-Output%20%22Azure%20SQL%20DB%20Export%20Failed%20for%20%22%24Using%3ACopyDatabaseName%22%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Drop%20Copy%20Database%20after%20successful%20export%20%3CBR%20%2F%3E%20Remove-AzureRmSqlDatabase%20-ResourceGroupName%20%22%24Using%3AResourceGroupName%22%20%60%20%3CBR%20%2F%3E%20-ServerName%20%22%24Using%3AServerName%22%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%22%24Using%3ACopyDatabaseName%22%20%60%20%3CBR%20%2F%3E%20-Force%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Write-Output%20%22Azure%20SQL%20DB%20%22%24Using%3ACopyDatabaseName%22%20Deleted%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Leave%20old%20PowerShell%20Script%20AS%20IS%20(in%20case%20needed)%3A%20%3C%2FSTRONG%3E%20%3CEM%3E%20%3CBR%20%2F%3E%20%3C%2FEM%3E%20%3CSTRONG%3E%20PowerShell%20script%20(copy%2Fpast%20and%20save%20as%20.ps1)%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23NOTE%3A%20%3CBR%20%2F%3E%20%23This%20script%20provided%20AS%20IS%2C%20Please%20review%20the%20code%20before%20executing%20this%20on%20production%20environment%20%3CBR%20%2F%3E%20%23For%20any%20issue%20or%20suggestion%20please%20email%20to%3A%20mobaioum%40microsoft.com%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20----%20Login%20to%20Azure%20----%20%3CBR%20%2F%3E%20workflow%20ExportAzureDB-PowerShellWorkflowScript%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20inlineScript%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24connectionName%20%3D%20%22AzureRunAsConnection%22%20%3CBR%20%2F%3E%20try%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Get%20the%20connection%20%22AzureRunAsConnection%20%22%20%3CBR%20%2F%3E%20%24servicePrincipalConnection%20%3D%20Get-AutomationConnection%20-Name%20%24connectionName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%22Login%20to%20Azure%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add-AzureRmAccount%20%60%20%3CBR%20%2F%3E%20-ServicePrincipal%20%60%20%3CBR%20%2F%3E%20-TenantId%20%24servicePrincipalConnection.TenantId%20%60%20%3CBR%20%2F%3E%20-ApplicationId%20%24servicePrincipalConnection.ApplicationId%20%60%20%3CBR%20%2F%3E%20-CertificateThumbprint%20%24servicePrincipalConnection.CertificateThumbprint%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20catch%20%7B%20%3CBR%20%2F%3E%20if%20(!%24servicePrincipalConnection)%20%7B%20%3CBR%20%2F%3E%20%24ErrorMessage%20%3D%20%22Connection%20%24connectionName%20not%20found.%22%20%3CBR%20%2F%3E%20throw%20%24ErrorMessage%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%7B%20%3CBR%20%2F%3E%20Write-Error%20-Message%20%24_.Exception%20%3CBR%20%2F%3E%20throw%20%24_.Exception%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Database%20to%20export%20%3CBR%20%2F%3E%20%24DatabaseName%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24ServerName%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24ServerAdmin%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24ResourceGroupName%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24serverPassword%20%3D%20%22hidden%22%20%3CBR%20%2F%3E%20%24securePassword%20%3D%20ConvertTo-SecureString%20-String%20%24serverPassword%20-AsPlainText%20-Force%20%24creds%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-ArgumentList%20%24serverAdmin%2C%20%24securePassword%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Generate%20a%20unique%20filename%20for%20the%20BACPAC%20%3CBR%20%2F%3E%20%24bacpacFilename%20%3D%20%24DatabaseName%20%2B%20(Get-Date).ToString(%22yyyy-MM-dd-HH-mm%22)%20%2B%20%22.bacpac%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Storage%20account%20info%20for%20the%20BACPAC%20%3CBR%20%2F%3E%20%24BaseStorageUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2FSTORAGE-NAME.blob.core.windows.net%2FBLOB-CONTAINER-NAME%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2FSTORAGE-NAME.blob.core.windows.net%2FBLOB-CONTAINER-NAME%3C%2FA%3E%22%20%3CBR%20%2F%3E%20%24BacpacUri%20%3D%20%24BaseStorageUri%20%2B%20%22%2FDaily%2F%22%20%2B%20%24bacpacFilename%20%3CBR%20%2F%3E%20%24StorageKeytype%20%3D%20%22StorageAccessKey%22%20%3CBR%20%2F%3E%20%24StorageKey%20%3D%20%22YOUR%20STORAGE%20KEY%22%20%3CBR%20%2F%3E%20%24exportRequest%20%3D%20New-AzureRmSqlDatabaseExport%20-ResourceGroupName%20%24ResourceGroupName%20-ServerName%20%24ServerName%20%60%20%3CBR%20%2F%3E%20-DatabaseName%20%24DatabaseName%20-StorageKeytype%20%24StorageKeytype%20-StorageKey%20%24StorageKey%20-StorageUri%20%24BacpacUri%20%60%20%3CBR%20%2F%3E%20-AdministratorLogin%20%24creds.UserName%20-AdministratorLoginPassword%20%24creds.Password%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Check%20status%20of%20the%20export%20%3CBR%20%2F%3E%20%24exportStatus%20%3D%20Get-AzureRmSqlDatabaseImportExportStatus%20-OperationStatusLink%20%24exportRequest.OperationStatusLink%20%3CBR%20%2F%3E%20Write-Output%20%22Azure%20SQL%20DB%20Export%20Completed%20at%20%24DatabaseName%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Enjoy%20automate%20Export%20Azure%20SQL%20DB%20!!%3C%2FSTRONG%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-368864%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Feb%2007%2C%202017%20I%20had%20a%20support%20case%20recently%26nbsp%3Bquestion%20how%20to%20automate%20Export%20Azure%20SQL%20DB%20to%20a%20storage%20account%20(Blob%20container)%20to%20.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-368864%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eautomation%20account%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Automation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eazure%20sql%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20SQL%20Database%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20SQL%20DB%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eblob%20auditing%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eblob%20storage%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eexport%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eexport%20azure%20sql%20db%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eexport%20to%20bacpac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eimportexport%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20Azure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Esql%20server%20backup%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
First published on MSDN on Feb 07, 2017
I had a support case recently question how to automate Export Azure SQL DB to a storage account (Blob container) to .bacpac files. The first attempt made to configure a PowerShell Script to Export Azure SQL DB to blob container and to .bacpac format from here . this trial successfully completed but on demand, the coming questions is. How can we run this PowerShell script into a scheduled job? as you all know Azure SQL DB have no SQL Agent in place. One way to do this either from client side or using Azure VM to schedule run PowerShell script!



That`s fine, but what if you can do this from Azure Portal, yes! you can; using Automation account runbooks feature. With no dependency on client or Azure VM schedule task to execute PowerShell export script for you!



I`ve searched for a complete document can demonstrate how to achieve this. Unfortunately, I did not find any. So, in this blog, we will walk through how to accomplish this step-by-step,



PREREQUISITES

To complete this exercise, you should have following resources in place:

1) Azure SQL Server and DB, for more reference how to create Azure SQL DB, Server from here

2) Storage account created and configured include blob container for remote backup SQL DB. For more reference how to create new storage account and associated blob container from here



STEPS

1) Go to https://portal.azure.com/

2) From More services in the bottom on the left hand search for "Automation Accounts" , select, this will open Automation account blade

3) Click Add to create a new Automation account, type in Name field: "MyAutomationAccount"  or as you like to name your Automation account, select desired subscription if you register more than one subscription. (make sure all resources (SQL DB, Server, storage account) existing in the same subscription as you create automation account). Use or create a new Resource group.

Leave Create Azure Run As account as default (Yes).

4) First, make sure "AzureClassicRunAsConnection"  and "AzureRunAsConnection"  connections created automatically during Automation account creation. Note: ( "AzureRunAsConnection"  connection will be used in our PowerShell workflow script later to authenticate to Azure subscription)

5) Second, Verify that your Automation account has latest modules imported and are up to date (AzureRM.Automation - AzureRM.Profile - AzureRM.Sql) to Version: 2.5.0 (Current available Version: 3.4.0 )

6) If modules show a different version, i.e. 1.0.3 select modules show old version and Import to update to version 2.5.0 (Current available Version: 3.4.0 )

7) Third, Import PowerShell script Update-ModulesInAutomationToLatestVersion from runbook gallery and make sure it is published (without this PowerShell script the Export PowerShell workflow will not work)



To import Update-ModulesInAutomationToLatestVersion PowerShell script from runbook Gallery, follow below steps:

a. Go to automation account

b. Select runbooks from middle blade

c. From top bar select Browse gallery 

d. Set the filter Gallery Source  to PowerShell Gallery

e. Search for Update-ModulesInAutomationToLatestVersion PowerShell script

f. Select this script and import it

g. Revert to runbooks again

h. Select recently imported PowerShell script

i. From top bar select Edit , then Publish

j. Verify that this script in Published state



Now we will import the PowerShell workflow script!

1) From Automation account overview dashboard

2) Select runbooks

3) Click Add a runbook (make sure you select PowerShell Workflow from category not PowerShell!)

4) Select Import an existing runbook and select to add ExportAzureDB-PowerShellWorkflow (PowerShell script code below)

5) Once new runbook created, it will present in runbooks in new state!

6) Select ExportAzureDB-PowerShellWorkflow and from top bar click Edit ,

7) Update required parameters match your environment and Save , then Test pane to start run this runbook to test result before set to Publish

8) Once Test pane completed, click Published , then you can assign a schedule for this runbook per your preference

9) Review storage blob container for successfully exported .bacpac file

10) You can review completed jobs from runbooks jobs



Updated PowerShell script: (2nd October, 2017)
Add function to copy source DB on the same server and export copied DB to blob storage. then drop copied database.



[code language="PowerShell"]<#
.SYNOPSIS
Export Azure SQL DB to blob storage in .bacpac format
.DESCRIPTION
This PowerShell workflow runbook script copy Azure SQL DB and Export copied database to blob storage container use below parameters.

.PARAMETER ServerName
Name of the SqlServer

.PARAMETER DatabaseName
Name of the database

.PARAMETER CopyDatabaseName
Name of the Copydatabase

.PARAMETER ResourceGroupName
Name of resource group contains the SqlServer and DatabaseName

.PARAMETER $ServerAdmin
Name of the Server admin Login

.PARAMETER $serverPassword
Input Server admin password

.PARAMETER $BaseStorageUri
Full uri for storage account name include container name https://STORAGE-NAME.blob.core.windows.net/BLOB-CONTAINER-NAME/

.PARAMETER $StorageKey
Storage account access key "YOUR STORAGE KEY" go to storage account --> settings --> select Access Keys --> Copy/Paste key1

.NOTES
This script provided AS IS, Please review the code before executing this on production environment
For any issue or suggestion please email to: mobaioum@microsoft.com
#>
# ---- Login to Azure ----
workflow ExportAzureDB-PowerShellWorkflowv2 {



param
(
# Name of the Azure SQL Database server
[parameter(Mandatory=$true)]
[string] $ServerName,

# Source Azure SQL Database name
[parameter(Mandatory=$true)]
[string] $DatabaseName,

# Target Azure SQL Database name
[parameter(Mandatory=$true)]
[string] $CopyDatabaseName,

# Resource Group Name
[parameter(Mandatory=$true)]
[string] $ResourceGroupName

)

inlineScript
{
$connectionName = "AzureRunAsConnection"
try
{

# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Login to Azure"
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}

# convert server admin password to secure string
$serverAdmin = "hidden"
$serverPassword = "hidden"
$securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword

# Generate a unique filename for the BACPAC
$bacpacFilename = "$Using:CopyDatabaseName" + (Get-Date).ToString("yyyy-MM-dd-HH-mm") + ".bacpac"

# Storage account info for the BACPAC
$BaseStorageUri = "https://STORAGE-NAME.blob.core.windows.net/BLOB-CONTAINER-NAME"
$BacpacUri = $BaseStorageUri + "/Daily/" + $bacpacFilename
$StorageKeytype = "StorageAccessKey"
$StorageKey = "YOUR STORAGE KEY"
New-AzureRmSqlDatabaseCopy -ResourceGroupName "$Using:ResourceGroupName" -ServerName "$Using:ServerName" -DatabaseName "$Using:DatabaseName" `
-CopyResourceGroupName "$Using:ResourceGroupName" -CopyServerName "$Using:ServerName" -CopyDatabaseName "$Using:CopyDatabaseName"

Write-Output "Azure SQL DB "$Using:CopyDatabaseName" Copy completed"

Write-Output "Azure SQL DB "$Using:CopyDatabaseName" Export Started"

$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName "$Using:ResourceGroupName" -ServerName "$Using:ServerName" `
-DatabaseName "$Using:CopyDatabaseName" -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
-AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

# Check status of the export
$exportStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink
[Console]::Write("Exporting")
while ($exportStatus.Status -eq "InProgress")
{
$exportStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink
Start-Sleep -s 10
}
$exportStatus
$Status= $exportStatus.Status
if($Status -eq "Succeeded")
{
Write-Output "Azure SQL DB Export $Status for "$Using:CopyDatabaseName""
}
else
{
Write-Output "Azure SQL DB Export Failed for "$Using:CopyDatabaseName""
}


# Drop Copy Database after successful export
Remove-AzureRmSqlDatabase -ResourceGroupName "$Using:ResourceGroupName" `
-ServerName "$Using:ServerName" `
-DatabaseName "$Using:CopyDatabaseName" `
-Force

Write-Output "Azure SQL DB "$Using:CopyDatabaseName" Deleted"
}
}[/code]



Leave old PowerShell Script AS IS (in case needed):
PowerShell script (copy/past and save as .ps1)


#NOTE:
#This script provided AS IS, Please review the code before executing this on production environment
#For any issue or suggestion please email to: mobaioum@microsoft.com

# ---- Login to Azure ----
workflow ExportAzureDB-PowerShellWorkflowScript {

inlineScript
{
$connectionName = "AzureRunAsConnection"
try {

# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName

"Login to Azure"

Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}

catch {
if (!$servicePrincipalConnection) {
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
}
else {
Write-Error -Message $_.Exception
throw $_.Exception
}
}

# Database to export
$DatabaseName = "hidden"
$ServerName = "hidden"
$ServerAdmin = "hidden"
$ResourceGroupName = "hidden"
$serverPassword = "hidden"
$securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword

# Generate a unique filename for the BACPAC
$bacpacFilename = $DatabaseName + (Get-Date).ToString("yyyy-MM-dd-HH-mm") + ".bacpac"

# Storage account info for the BACPAC
$BaseStorageUri = "https://STORAGE-NAME.blob.core.windows.net/BLOB-CONTAINER-NAME"
$BacpacUri = $BaseStorageUri + "/Daily/" + $bacpacFilename
$StorageKeytype = "StorageAccessKey"
$StorageKey = "YOUR STORAGE KEY"
$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
-DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
-AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

# Check status of the export
$exportStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink
Write-Output "Azure SQL DB Export Completed at $DatabaseName"
}
}


Enjoy automate Export Azure SQL DB !!