Backing up the SQL Server 2014 database to the Azure BLOB storage service

Source: Internet
Author: User
Keywords Azure azure Memory Service
http://www.aliyun.com/zixun/aggregation/13357.html ">azure VM Customized script extensions (Custom script Extension) will allow you to download from the storage account PowerShell script and execute it, with such a simple feature, you can flexibly automate VM settings in a variety of VM-customized contexts. In this article we will show you how to use the customized script to extend the visitor to a SQL Server 2014 VM from Azure VM Image Gallery, and we will use SQL Powershell scripts to enable SQL Server Consolidator Backup to Microsoft Azure, which allows you to back up your SQL Server 2014 database to the Azure Blob storage service.   SQL Server Settings script   See the following program code, you only need to change [your azure storage account] and [your Azure storage account key] to your azure Memory accounts and vouchers, and save this script for CUSTOMSCRIPTSQLPS.PS1.   #import SQL Server PowerShell module  import-module  sqlps-disablenamechecking     #global Variables-replace Storage account name and key $credentialName = "Azurestoragecredential_" + (RANDOM) $storageAccountName = ' [Your azure Storage account] ' $storageAccountKey = ' [Your azure Storage account key] '     #start SQL Agent Service            write-host "Starting SQL Server Agent service ..." StaRt-service  sqlserveragent-verbose   CD sqlserver:sql\ $env: Computername\default   Write-host "Creating SQL credential ... "$secureString = convertto-securestring $storageAccountKey  -asplaintext-force       #Create credential new-sqlcredential–name $credentialName –identity $storageAccountName –secret $secureString     $encryptionOption = new-sqlbackupencryptionoption-noencryption   write-host "Enabling Consolidator SQL Server Backup ... "  Get-sqlsmartadmin | set-sqlsmartadmin-backupenabled $True  -backupretentionperiodindays 7-sqlcredential $credentialName- encryptionoption  $encryptionOption   write-host "Consolidator SQL Server Backup current revisit:" Get-sqlsmartadmin | fl    However, we cannot submit this script directly because the customized script extension uses the Ntauthority\system account to execute the script, so we have to create another script to play the admin account to perform the script.   Use another authentication to execute the script   first we create another script named Start.ps1, which reads as follows. To facilitate the explanation, I added line number below.   1: $password =  convertto-securestring "[Your Admin account user Password] "-asplaintext-force   2: $credential = New-object System.Management.Automation.PSCredential ("$env: Computername\[your Admin Account]", $password)   3: $command = $ File = $PSScriptRoot + "\customscriptsqlps.ps1"   4:enable-psremoting–force   5:invoke-command-filepath $ Command-credential $credential-computername $env: ComputerName   6:disable-psremoting-force   Line 1: Send administrator password from Plain text to Eastdil string, of course it is not a good idea to put the password directly in the script, we'll come back later.   Line 2: Establish a certification to perform the primary script, which you can use and prepare to create the same account as the VM.   Line 3: Specify the exact path of the primary script.   Line 4 & 6: To perform the primary script in a counterfeit authentication, we must enable Windows Remote Management (WinRM), which is line 4, while Line 6 is off.   Line 5: Use the Invoke-command cmdlet to perform the primary script (requires authentication parameters).   Using customized scripting extensions   Now we are ready to submit the above two scripts to the customized script extensions, upload two scripts to your memory account in the script container, and then execute the following script on top of your workstation.   1: $servicename = "[Cloud service that hosts the VM]"   2: $vmname = "[Name of the VM]"   3: $VM = Get-azurevm-ser Vicename $servicename-naMe $vmname   4:set-azurevmcustomscriptextension-containername scripts-storageaccountname ' [Your storage account Name] '-vm $vm-filename ' start.ps1 ', ' customscriptsqlps.ps1 '-run ' Start.ps1 ' | Update-azurevm-verbose   5: $status = get-azurevm-servicename $servicename-name $vmname   6: $result = $status. ResourceExtensionStatusList.ExtensionSettingStatus.SubStatusList | Select Name, @{"Label" = "message"; Expression = {$_. Formattedmessage.message}}    7: $result |FL   The most important is line 4th, which ensures that the installation of a customized script extension on the VM and downloads Start.ps1 and CUSTOMSCRIPTSQLPS.PS1, then execute start.ps1.   Read the password in the external file   We all know that you should avoid writing passwords in your program code, so now we're going to introduce the encryption and decryption service, and we'll assume that you've deployed the credentials with private key to the target VM, and you use the public key of the same credentials to encrypt the Password.txt file (base-64 format).   $cert = Get-childitem cert:\localmachine\my\[certificate Thumbprint]   $bytes = [Text.encoding]::utf8. GetBytes ("ABCDEFG")   $encrypted = $cert. PublicKey.Key.Encrypt ($bytes, $true)   $base = [Convert]::tobase64string ($encrypted)   Set-coNtent. \password.txt $base   If you are not familiar with VM voucher management, you can refer to this (offer instructional videos and command prompt character downloads).   Modify START.PS1 and replace the following program code with the original first line:   $cert = Get-childitem Cert:\localmachine\my\[certificate Thumbprint]   $base = get-content \password.txt   $encrypted = [Convert]::frombase64string ($base)   $bytes = $cert . Privatekey.decrypt ($encrypted, $true)   $password = [Text.encoding]::utf8. GetString ($bytes)     Finally, add a filename to the file list to confirm that the Password.txt file has been routed to the customized script extension.    set-azurevmcustomscriptextension-containername scripts-storageaccountname ' [Your storage account  Name] '-vm $vm-filename ' start.ps1 ', ' customscriptsqlps.ps1 ',  ' password.txt ', '-run ' ' start.ps1 ' | Update-azurevm-verbose
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.