-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
161 lines (144 loc) · 7.27 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using Microsoft.Azure.Management.Sql.Fluent;
using Microsoft.Azure.Management.Sql.Fluent.Models;
using System;
namespace ManageSqlWithRecoveredOrRestoredDatabase
{
public class Program
{
private static readonly string sqlServerName = SdkContext.RandomResourceName("sqlserver", 20);
private static readonly string rgName = SdkContext.RandomResourceName("rgsql", 20);
private static readonly string administratorLogin = "sqladmin3423";
private static readonly string administratorPassword = Utilities.CreatePassword();
private static readonly string dbToDeleteName = "db-to-delete";
private static readonly string dbToRestoreName = "db-to-restore";
/**
* Azure SQL sample for managing point in time restore and recover a deleted SQL Database -
* - Create a SQL Server with two database from a pre-existing sample.
* - Create a new database from a point in time restore
* - Delete a database then restore it from a recoverable dropped database automatic backup
* - Delete databases and SQL Server
*/
public static void RunSample(IAzure azure)
{
try
{
// ============================================================
// Create a SQL Server with two databases from a sample.
Utilities.Log("Creating a SQL Server with two databases from a sample.");
var sqlServer = azure.SqlServers.Define(sqlServerName)
.WithRegion(Region.USEast2)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.DefineDatabase(dbToDeleteName)
.FromSample(SampleName.AdventureWorksLT)
.WithStandardEdition(SqlDatabaseStandardServiceObjective.S0)
.Attach()
.DefineDatabase(dbToRestoreName)
.FromSample(SampleName.AdventureWorksLT)
.WithStandardEdition(SqlDatabaseStandardServiceObjective.S0)
.Attach()
.Create();
Utilities.PrintSqlServer(sqlServer);
// Sleep for 5 minutes to allow for the service to be aware of the new server and databases
SdkContext.DelayProvider.Delay(5 * 60 * 1000);
var dbToBeDeleted = sqlServer.Databases
.Get(dbToDeleteName);
Utilities.PrintDatabase(dbToBeDeleted);
var dbToRestore = sqlServer.Databases
.Get(dbToRestoreName);
Utilities.PrintDatabase(dbToRestore);
// ============================================================
// Loop until a point in time restore is available.
Utilities.Log("Loop until a point in time restore is available.");
int retries = 50;
while (retries > 0 && dbToRestore.ListRestorePoints().Count == 0)
{
retries--;
// Sleep for about 3 minutes
SdkContext.DelayProvider.Delay(3 * 60 * 1000);
}
if (retries == 0)
{
return;
}
var restorePointInTime = dbToRestore.ListRestorePoints()[0];
// Restore point might not be ready right away and we will have to wait for it.
long waitForRestoreToBeReady = (long) ((System.TimeSpan)restorePointInTime.EarliestRestoreDate.GetValueOrDefault().Subtract(DateTime.Now.ToUniversalTime())).TotalMilliseconds + 300000;
if (waitForRestoreToBeReady > 0)
{
SdkContext.DelayProvider.Delay((int)waitForRestoreToBeReady);
}
var dbRestorePointInTime = sqlServer.Databases
.Define("db-restore-pit")
.FromRestorePoint(restorePointInTime)
.Create();
Utilities.PrintDatabase(dbRestorePointInTime);
dbRestorePointInTime.Delete();
// ============================================================
// Delete the database than loop until the restorable dropped database backup is available.
Utilities.Log("Deleting the database than loop until the restorable dropped database backup is available.");
dbToBeDeleted.Delete();
retries = 24;
while (retries > 0 && sqlServer.ListRestorableDroppedDatabases().Count == 0)
{
retries--;
// Sleep for about 5 minutes
SdkContext.DelayProvider.Delay(5 * 60 * 1000);
}
var restorableDroppedDatabase = sqlServer.ListRestorableDroppedDatabases()[0];
var dbRestoreDeleted = sqlServer.Databases
.Define("db-restore-deleted")
.FromRestorableDroppedDatabase(restorableDroppedDatabase)
.Create();
Utilities.PrintDatabase(dbRestoreDeleted);
// Delete databases
dbToRestore.Delete();
dbRestoreDeleted.Delete();
// Delete the SQL Server.
Utilities.Log("Deleting a Sql Server");
azure.SqlServers.DeleteById(sqlServer.Id);
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception e)
{
Utilities.Log(e.ToString());
}
}
}
}