Deploy an automation runbook to automatically rebuild indexes
Estimated Time: 30 minutes
You have been hired as a Senior Database Administrator to help automate day to day operations of database administration. This automation is to help ensure that the databases for AdventureWorks continue to operate at peak performance as well as provide methods for alerting based on certain criteria. AdventureWorks utilizes SQL Server in both Infrastructure as a Service (IaaS) and Platform as a Service (PaaS) offerings.
đ These exercises may ask you to copy and paste T-SQL code and makes use of existing SQL resources. Please verify that the code has been copied correctly, before executing the code.
Setup environment
If your lab virtual machine has been provided and pre-configured, you should find the lab files ready in the C:\LabFiles folder. Take a moment to check, if the files are already there, skip this section. However, if youâre using your own machine or the lab files are missing, youâll need to clone them from GitHub to proceed.
-
From the lab virtual machine or your local machine if one wasnât provided, start a Visual Studio Code session.
-
Open the command palette (Ctrl+Shift+P) and type Git: Clone. Select the Git: Clone option.
-
Paste the following URL into the Repository URL field and select Enter.
https://github.com/MicrosoftLearning/dp-300-database-administrator.git
-
Save the repository to the C:\LabFiles folder on the lab virtual machine or your local machine if one wasnât provided (create the folder if it does not exist).
Setup your SQL Server in Azure
Log in to Azure and check if you have an existing Azure SQL Server instance running in Azure. Skip this section if you already have a SQL Server instance running in Azure.
-
From the lab virtual machine or your local machine if one wasnât provided, start a Visual Studio Code session and navigate to the cloned repository from the previous section.
-
Right-click on the /Allfiles/Labs folder and select Open in Integrated Terminal.
-
Letâs connect to Azure using the Azure CLI. Type the following command and select Enter.
az login
đ Note that a browser window will open. Use your Azure credentials to log in.
-
Once you are logged in to Azure, itâs time to create a resource group if it doesnât already exist, and create a SQL server and database under that resource group. Type the following command and select Enter. The script will take a few minutes to complete.
cd ./Setup ./deploy-sql-database.ps1
đ Note that by default this script will create or a resource group called contoso-rg, or use a resource whose name start with contoso-rg if it exists. By default it will also create all resources on the West US 2 region (westus2). Finally it will generate a random 12 character password for the SQL admin password. You can change these values by using one or more of the parameters -rgName, -location and -sqlAdminPw with your own values. The password will have to meet the Azure SQL password complexity requirements, at least 12 characters long, and contain at least 1 uppercase letter, 1 lowercase letter, 1 number and 1 special character.
đ Note that the script will add your current Public IP address to the SQL server firewall rules.
-
Once the script has completed, it will return the resource group name, SQL server name and database name, and admin user name and password. Take note of these values as you will need them later in the lab.
Create an Automation Account
-
From the lab virtual machine or your local machine if one wasnât provided, start a browser session and navigate to https://portal.azure.com. Connect to the Portal using your Azure credentials.
-
In the Azure portal in the search bar type automation and then select Automation Accounts from the search results, and then select + Create.
-
On the Create an Automation Account page, enter the information below, and then select Review + Create.
- Resource Group: <Your resource group>
- Automation account name: autoAccount
- Region: Use the default.
-
On the review page, select Create.
đ Your automation account could take a few minutes to create.
Connect to an existing Azure SQL Database
-
In the Azure portal, navigate to your database by searching for sql databases.
-
Select the SQL database AdventureWorksLT.
-
On the main section for your SQL Database page, select Query editor (preview).
-
You will be prompted for credentials to sign in to your database using the database admin account and select OK.
This will open a new tab in your browser. Select Add client IP and then select Save. Once saved, return to the previous tab and select OK again.
đ You might receive the error message Cannot open server âyour-sql-server-nameâ requested by the login. Client with IP address âxxx.xxx.xxx.xxxâ is not allowed to access the server. If so, you will need to add your current Public IP address to the SQL server firewall rules.
If you need to setup the firewall rules, follow these steps:
- select Set server firewall from the top menu bar of the databaseâs Overview page.
- Select Add your current IPv4 address (xxx.xxx.xxx.xxx) and then select Save.
- Once saved, return to the AdventureWorksLT database page and select Query editor (preview) again.
- You will be prompted for credentials to sign in to your database using the database admin account and select OK.
-
In the Query editor (preview), select Open query.
-
Select the browse folder icon and navigate to the C:\LabFiles\dp-300-database-administrator\Allfiles\Labs\Module13 folder. Select the usp_AdaptiveIndexDefrag.sql file and select Open, and then select OK.
-
Delete USE msdb and GO on lines 5 and 6 of the query, and then select Run.
-
Expand the Stored Procedures folder to see the newly created stored procedures.
Configure Automation Account assets
The next steps consist of configuring the assets required in preparation for the runbook creation. Then select Automation Accounts.
-
On the Azure portal, in the top search box, type automation and select Automation Accounts.
-
Select the autoAccount automation account that you created.
-
Select Modules from the Shared Resources section of the Automation blade. Then select Browse gallery.
-
Search for SqlServer within the Gallery.
-
Select SqlServer which will direct to the next screen, and then select the Select button.
-
On the Add a module page, select the latest runtime version available, then select Import. This will import the PowerShell module into your Automation account.
-
Youâll need to create a credential to securely sign in to your database. From the blade for the Automation Account navigate to the Shared Resources section and select Credentials.
-
Select + Add a Credential, enter the information below, and then select Create.
- Name: SQLUser
- User name: sqladmin
- Password: <Enter a strong password, 12 characters long, and containing at least 1 uppercase letter, 1 lowercase letter, 1 number and 1 special character.>
- Confirm password: <Re-enter the password you previously entered.>
Create a PowerShell runbook
-
In the Azure portal, navigate to your database by searching for sql databases.
-
Select the SQL database AdventureWorksLT.
-
On the Overview page copy the Server name of your Azure SQL Database (Your server name should start with dp300-lab). Youâll paste this in later steps.
-
On the Azure portal, in the top search box, type automation and select Automation Accounts.
-
Select the autoAccount automation account.
-
Expand to the Process Automation section of the Automation account blade, select Runbooks.
-
Select + Create a runbook.
đ As weâve learned, note that there are two existing runbooks created. These were automatically created during the automation account deployment.
-
Enter the runbook name as IndexMaintenance and a runbook type of PowerShell. Select the latest runtime version available, then select Review + Create.
-
On the Create runbook page, select Create.
-
Once the runbook has been created, copy and paste the Powershell code snippet below into your runbook editor.
đ Please verify that the code has been copied correctly, before saving the runbook.
$AzureSQLServerName = '' $DatabaseName = 'AdventureWorksLT' $Cred = Get-AutomationPSCredential -Name "SQLUser" $SQLOutput = $(Invoke-Sqlcmd -ServerInstance $AzureSQLServerName -UserName $Cred.UserName -Password $Cred.GetNetworkCredential().Password -Database $DatabaseName -Query "EXEC dbo.usp_AdaptiveIndexDefrag" -Verbose) 4>&1 Write-Output $SQLOutput
đ Note that the code above is a PowerShell script that will execute the stored procedure usp_AdaptiveIndexDefrag on the AdventureWorksLT database. The script uses the Invoke-Sqlcmd cmdlet to connect to the SQL server and execute the stored procedure. The Get-AutomationPSCredential cmdlet is used to retrieve the credentials stored in the Automation account.
-
On the first line of the script paste in the server name you copied in the previous steps.
-
Select Save, and then select Publish.
-
Select Yes to confirm the publish action.
-
The IndexMaintenance runbook is now published.
Create a schedule for a runbook
Next you will schedule the runbook to execute on a regular basis.
-
Under Resources in the left hand navigation of your IndexMaintenance runbook, select Schedules.
-
Select + Add a schedule.
-
Select Link a schedule to your runbook.
-
Select + Add a schedule.
-
Enter the information below, and then select Create.
- Name: DailyIndexDefrag
- Description: Daily Index defrag for AdventureWorksLT database.
- Starts: 4:00 AM (next day)
- Time zone: <Select the time zone that matches your location>
- Recurrence: Recurring
- Recur every: 1 day
- Set expiration: No
đ Note that the start time is set to 4:00 AM the next day. The time zone is set to your local time zone. The recurrence is set to every 1 day. Never expires.
-
Select Create, and then select OK.
-
The schedule is now created and linked to the runbook. Select OK.
Azure Automation delivers a cloud-based automation, and configuration service that supports consistent management across your Azure and non-Azure environments.
Cleanup Resources
If you are not using the Azure SQL Server for any other purpose, you can clean up the resources you created in this lab.
Delete the Resource Group
If you created a new resource group for this lab, you can delete the resource group to remove all resources created in this lab.
-
In the Azure portal, select Resource groups from the left navigation pane or search for Resource groups in the search bar and select it from the results.
-
Go into the resource group that you created for this lab. The resource group will contain the Azure SQL Server and other resources created in this lab.
-
Select Delete resource group from the top menu.
-
In the Delete resource group dialog, type the name of the resource group to confirm and select Delete.
-
Wait for the resource group to be deleted.
-
Close the Azure portal.
Delete the Lab resources only
If you didnât create a new resource group for this lab, and want to leave the resource group and its previous resources intact, you can still delete the resources created in this lab.
-
In the Azure portal, select Resource groups from the left navigation pane or search for Resource groups in the search bar and select it from the results.
-
Go into the resource group that you created for this lab. The resource group will contain the Azure SQL Server and other resources created in this lab.
-
Select all the resources prefixed with the SQL Server name you previously specified in the lab.
-
Select Delete from the top menu.
-
In the Delete resources dialog, type delete and select Delete.
-
Select Delete again to confirm the deletion of the resources.
-
Wait for the resources to be deleted.
-
Close the Azure portal.
Delete the LabFiles folder
If you created a new LabFiles folder for this lab, and no longer need it, you can delete the LabFiles folder to remove all files created in this lab.
- From the lab virtual machine or your local machine if one wasnât provided, open file explorer and navigate to the C:\ drive.
- Right-click on the LabFiles folder and select Delete.
- Select Yes to confirm the deletion of the folder.
You have successfully completed this lab.
By completing this exercise youâve automated the defragging of indexes on a SQL server database to run every day, at 4am.