Skip to content

Commit

Permalink
set of revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
msebolt committed Mar 19, 2020
1 parent af98725 commit 12ccd3e
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 473 deletions.
21 changes: 9 additions & 12 deletions sql-database/backup-database/backup-database.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/bin/bash

$subscription = "<subscriptionId>" # add subscription here
$location = "East US"

$randomIdentifier = $(Get-Random)

$resourceGroup = "resource-$randomIdentifier"
$resource = "resource-$randomIdentifier"
$server = "server-$randomIdentifier"
$database = "database-$randomIdentifier"
$storage = "storage$randomIdentifier"
Expand All @@ -16,24 +13,24 @@ $bacpac = "backup.bacpac"
$login = "sampleLogin"
$password = "samplePassword123!"

echo "Using resource group $($resourceGroup) with login: $($login), password: $($password)..."
echo "Using resource group $($resource) with login: $($login), password: $($password)..."

echo "Creating resource groups..."
az group create --name $resourceGroup --location $location
az group create --name $resource --location $location

echo "Creating $($storage)..."
az storage account create --name $storage --resource-group $resourceGroup --location $location --sku Standard_LRS
az storage account create --name $storage --resource-group $resource --location $location --sku Standard_LRS

echo "Creating $($container) on $($storage)..."
$key = az storage account keys list --account-name $storage --resource-group $resourceGroup -o json --query [0].value
$key = az storage account keys list --account-name $storage --resource-group $resource -o json --query [0].value
az storage container create --name $container --account-key $key --account-name $storage

echo "Creating $($server)..."
az sql server create --name $server --resource-group $resourceGroup --location $location --admin-user $login --admin-password $password
az sql server firewall-rule create --resource-group $resourceGroup --server $server --name AllowAzureServices --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0
az sql server create --name $server --resource-group $resource --location $location --admin-user $login --admin-password $password
az sql server firewall-rule create --resource-group $resource --server $server --name AllowAzureServices --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0

echo "Creating $($database)..."
az sql db create --name $database --resource-group $resourceGroup --server $server --edition GeneralPurpose --sample-name AdventureWorksLT
az sql db create --name $database --resource-group $resource --server $server --edition GeneralPurpose --sample-name AdventureWorksLT

echo "Backing up $($database)..."
az sql db export --admin-password $password --admin-user $login --storage-key $key --storage-key-type StorageAccessKey --storage-uri "https://$storage.blob.core.windows.net/$container/$bacpac" --name $database --resource-group $resourceGroup --server $server
az sql db export --admin-password $password --admin-user $login --storage-key $key --storage-key-type StorageAccessKey --storage-uri "https://$storage.blob.core.windows.net/$container/$bacpac" --name $database --resource-group $resource --server $server
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/bin/bash

$subscription = "<subscriptionId>" # add subscription here
$location = "East US"

$randomIdentifier = $(Get-Random)

$resourceGroup = "resource-$randomIdentifier"
$resource = "resource-$randomIdentifier"
$server = "server-$randomIdentifier"
$database = "database-$randomIdentifier"

$targetResourceGroup = "targetResource-$randomIdentifier"
$targetResource = "targetResource-$randomIdentifier"
$targetLocation = "West US"
$targetServer = "targetServer-$randomIdentifier"
$targetDatabase = "targetDatabase-$randomIdentifier"
Expand All @@ -19,16 +16,16 @@ $password = "samplePassword123!"

echo "Using resource group $($resourceGroup) with login: $($login), password: $($password)..."

echo "Creating $($resourceGroup) (and $($targetResourceGroup))..."
az group create --name $resourceGroup --location $location
az group create --name $targetResourceGroup --location $targetLocation
echo "Creating $($resource) (and $($targetResource))..."
az group create --name $resource --location $location
az group create --name $targetResource --location $targetLocation

echo "Creating $($server) in $($location) (and $($targetServer) in $($targetLocation))..."
az sql server create --name $server --resource-group $resourceGroup --location $location --admin-user $login --admin-password $password
az sql server create --name $targetServer --resource-group $targetResourceGroup --location $targetLocation --admin-user $login --admin-password $password
az sql server create --name $server --resource-group $resource --location $location --admin-user $login --admin-password $password
az sql server create --name $targetServer --resource-group $targetResource --location $targetLocation --admin-user $login --admin-password $password

echo "Creating $($database) on $($server)..."
az sql db create --name $database --resource-group $resourceGroup --server $server --service-objective S0
az sql db create --name $database --resource-group $resource --server $server --service-objective S0

echo "Copying $($database) (on $($server)) to $($targetDatabase) (on $($targetServer))..."
az sql db copy --dest-name $targetDatabase --dest-resource-group $targetResourceGroup --dest-server $targetServer --name $database --resource-group $resourceGroup --server $server
az sql db copy --dest-name $targetDatabase --dest-resource-group $targetResource --dest-server $targetServer --name $database --resource-group $resource --server $server
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
#!/bin/bash
$location = "East US"
$randomIdentifier = $(Get-Random)

# set execution context (if necessary)
az account set --subscription <replace with your subscription name or id>
$resource = "resource-$randomIdentifier"
$server = "server-$randomIdentifier"
$database = "database-$randomIdentifier"

# Set the resource group name and location for your server
resourceGroupName=myResourceGroup-$RANDOM
location=westus2
$login = "sampleLogin"
$password = "samplePassword123!"

# Set an admin login and password for your database
adminlogin=ServerAdmin
password=`openssl rand -base64 16`
# password=<EnterYourComplexPasswordHere1>
$startIP=0.0.0.0
$endIP=0.0.0.0

# The logical server name has to be unique in the system
servername=server-$RANDOM
echo "Creating $($resource)..."
az group create --name $resource --location $location

# The ip address range that you want to allow to access your DB
startip=0.0.0.0
endip=0.0.0.0
echo "Creating $($server) in $($location)..."
az sql server create --name $server --resource-group $resource --location $location --admin-user $login --admin-password $password

# Create a resource group
az group create \
--name $resourceGroupName \
--location $location
echo "Configuring firewall..."
az sql server firewall-rule create --resource-group $resource --server $server -n AllowYourIp --start-ip-address $startIP --end-ip-address $endIP

# Create a logical server in the resource group
az sql server create \
--name $servername \
--resource-group $resourceGroupName \
--location $location \
--admin-user $adminlogin \
--admin-password $password

# Configure a firewall rule for the server
az sql server firewall-rule create \
--resource-group $resourceGroupName \
--server $servername \
-n AllowYourIp \
--start-ip-address $startip \
--end-ip-address $endip

# Create a database in the server with zone redundancy as false
az sql db create \
--resource-group $resourceGroupName \
--server $servername \
--name mySampleDatabase \
--sample-name AdventureWorksLT \
--edition GeneralPurpose \
--family Gen4 \
--capacity 1 \
--zone-redundant false

# Zone redundancy is only supported in the premium and business critical service tiers

# Echo random password
echo $password
echo "Creating $($database) on $($server)..."
az sql db create --resource-group $resource --server $server --name $database --sample-name AdventureWorksLT --edition GeneralPurpose --family Gen4 --capacity 1 --zone-redundant false # zone redundancy is only supported on premium and business critical service tiers
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/bin/bash

$subscription = "<subscriptionId>" # add subscription here
$location = "East US"

$randomIdentifier = $(Get-Random)

$resourceGroup = "resource-$randomIdentifier"
$resource = "resource-$randomIdentifier"
$server = "server-$randomIdentifier"
$database = "database-$randomIdentifier"
$storage = "storage$randomIdentifier"
Expand All @@ -15,22 +12,22 @@ $notification = "changeto@your.email;changeto@your.email"
$login = "sampleLogin"
$password = "samplePassword123!"

echo "Using resource group $($resourceGroup) with login: $($login), password: $($password)..."
echo "Using resource group $($resource) with login: $($login), password: $($password)..."

echo "Creating $($resourceGroup)..."
az group create --name $resourceGroup --location $location
echo "Creating $($resource)..."
az group create --name $resource --location $location

echo "Creating $($server) in $($location) ..."
az sql server create --name $server --resource-group $resourceGroup --location $location --admin-user $login --admin-password $password
echo "Creating $($server) in $($location)..."
az sql server create --name $server --resource-group $resource --location $location --admin-user $login --admin-password $password

echo "Creating $($database) on $($server)..."
az sql db create --name $database --resource-group $resourceGroup --server $server --service-objective S0
az sql db create --name $database --resource-group $resource --server $server --service-objective S0

echo "Creating $($storage)..."
az storage account create --name $storage --resource-group $resourceGroup --location $location --sku Standard_LRS
az storage account create --name $storage --resource-group $resource --location $location --sku Standard_LRS

echo "Setting access policy on $($storage)..."
az sql db audit-policy update --name $database --resource-group $resourceGroup --server $server --state Enabled --storage-account $storage
az sql db audit-policy update --name $database --resource-group $resource --server $server --state Enabled --storage-account $storage

echo "Setting threat detection policy on $($storage)..."
az sql db threat-policy update --email-account-admins Disabled --email-addresses $notification --name $database --resource-group $resourceGroup --server $server --state Enabled --storage-account $storage
az sql db threat-policy update --email-account-admins Disabled --email-addresses $notification --name $database --resource-group $resource --server $server --state Enabled --storage-account $storage
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/bin/bash

$subscription = "<subscriptionId>" # add subscription here
$location = "East US"

$randomIdentifier = $(Get-Random)

$resourceGroup = "resource-$randomIdentifier"
$resource = "resource-$randomIdentifier"
$server = "sqlserver-$randomIdentifier"
$pool = "pool-$randomIdentifier"
$database = "database-$randomIdentifier"
Expand All @@ -17,45 +14,45 @@ $failoverServer = "sqlsecondary-$randomIdentifier"
$login = "sampleLogin"
$password = "samplePassword123!"

echo "Using resource group $($resourceGroup) with login: $($login), password: $($password)..."
echo "Using resource group $($resource) with login: $($login), password: $($password)..."

echo "Creating $($resourceGroup)..."
az group create --name $resourceGroup --location $location
echo "Creating $($resource)..."
az group create --name $resource --location $location

echo "Creating $($server) in $($location)..."
az sql server create --name $server --resource-group $resourceGroup --location $location --admin-user $login --admin-password $password
az sql server create --name $server --resource-group $resource --location $location --admin-user $login --admin-password $password

echo "Creating $($database) on $($server)..."
az sql db create --name $database --resource-group $resourceGroup --server $server --sample-name AdventureWorksLT
az sql db create --name $database --resource-group $resource --server $server --sample-name AdventureWorksLT

echo "Creating $($pool) on $($server)..."
az sql elastic-pool create --name $pool --resource-group $resourceGroup --server $server
az sql elastic-pool create --name $pool --resource-group $resource --server $server

echo "Adding $($database) to $($pool)..."
az sql db update --elastic-pool $pool --name $database --resource-group $resourceGroup --server $server
az sql db update --elastic-pool $pool --name $database --resource-group $resource --server $server

echo "Creating $($failoverServer) in $($failoverLocation)..."
az sql server create --name $failoverServer --resource-group $resourceGroup --location $failoverLocation --admin-user $login --admin-password $password
az sql server create --name $failoverServer --resource-group $resource --location $failoverLocation --admin-user $login --admin-password $password

echo "Creating $($pool) on $($failoverServer)..."
az sql elastic-pool create --name $pool --resource-group $resourceGroup --server $failoverServer
az sql elastic-pool create --name $pool --resource-group $resource --server $failoverServer

echo "Creating $($failover) between $($server) and $($failoverServer)..."
az sql failover-group create --name $failover --partner-server $failoverServer --resource-group $resourceGroup --server $server --failover-policy Automatic --grace-period 2
az sql failover-group create --name $failover --partner-server $failoverServer --resource-group $resource --server $server --failover-policy Automatic --grace-period 2

$databaseId = az sql elastic-pool list-dbs --name $pool --resource-group $resourceGroup --server $server --query [0].name -o json
$databaseId = az sql elastic-pool list-dbs --name $pool --resource-group $resource --server $server --query [0].name -o json

echo "Adding $($database) to $($failover)..."
az sql failover-group update --name $failover --add-db $databaseId --resource-group $resourceGroup --server $server
az sql failover-group update --name $failover --add-db $databaseId --resource-group $resource --server $server

echo "Confirming role of $($failoverServer) is secondary..." # note ReplicationRole property
az sql failover-group show --name $failover --resource-group $resourceGroup --server $server
az sql failover-group show --name $failover --resource-group $resource --server $server

echo "Failing over to $($failoverServer)..."
az sql failover-group set-primary --name $failover --resource-group $resourceGroup --server $failoverServer
az sql failover-group set-primary --name $failover --resource-group $resource --server $failoverServer

echo "Confirming role of $($failoverServer) is now primary..." # note ReplicationRole property
az sql failover-group show --name $failover --resource-group $resourceGroup --server $server
az sql failover-group show --name $failover --resource-group $resource --server $server

echo "Failing back to $($server)...."
az sql failover-group set-primary --name $failover --resource-group $resourceGroup --server $server
az sql failover-group set-primary --name $failover --resource-group $resource --server $server
Loading

0 comments on commit 12ccd3e

Please sign in to comment.