/
ExportDatabaseTask.cs
95 lines (83 loc) · 3.54 KB
/
ExportDatabaseTask.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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using NuGetGallery.Operations.Common;
using NuGetGallery.Operations.SqlDac;
namespace NuGetGallery.Operations
{
[Command("exportdatabase", "Exports a copy of the database to blob storage", AltName = "xdb", MinArgs = 0, MaxArgs = 0)]
public class ExportDatabaseTask : DatabaseTask
{
[Option("Azure Storage Account in which the exported database should be placed", AltName = "s")]
public CloudStorageAccount DestinationStorage { get; set; }
[Option("Blob container in which the backup should be placed", AltName = "c")]
public string DestinationContainer { get; set; }
[Option("URL of the SQL DAC endpoint to talk to", AltName = "dac")]
public Uri SqlDacEndpoint { get; set; }
public override void ValidateArguments()
{
base.ValidateArguments();
if (CurrentEnvironment != null)
{
if (DestinationStorage == null)
{
DestinationStorage = CurrentEnvironment.BackupStorage;
}
if (SqlDacEndpoint == null)
{
SqlDacEndpoint = CurrentEnvironment.SqlDacEndpoint;
}
}
ArgCheck.RequiredOrConfig(DestinationStorage, "DestinationStorage");
ArgCheck.RequiredOrConfig(SqlDacEndpoint, "SqlDacEndpoint");
ArgCheck.Required(DestinationContainer, "DestinationContainer");
}
public override void ExecuteCommand()
{
Log.Info("Exporting {0} on {1} to {2}", ConnectionString.InitialCatalog, Util.GetDatabaseServerName(ConnectionString), DestinationStorage.Credentials.AccountName);
string serverName = ConnectionString.DataSource;
if (serverName.StartsWith("tcp:"))
{
serverName = serverName.Substring(4);
}
WASDImportExport.ImportExportHelper helper = new WASDImportExport.ImportExportHelper(Log)
{
EndPointUri = SqlDacEndpoint.AbsoluteUri,
DatabaseName = ConnectionString.InitialCatalog,
ServerName = serverName,
UserName = ConnectionString.UserID,
Password = ConnectionString.Password,
StorageKey = Convert.ToBase64String(DestinationStorage.Credentials.ExportKey())
};
// Prep the blob
string blobUrl = null;
if (!WhatIf)
{
var client = DestinationStorage.CreateCloudBlobClient();
var container = client.GetContainerReference(DestinationContainer);
container.CreateIfNotExists();
var blob = container.GetBlockBlobReference(ConnectionString.InitialCatalog + ".bacpac");
if (blob.Exists())
{
Log.Info("Skipping export of {0} because the blob already exists", blob.Name);
}
else
{
Log.Info("Starting export to {0}", blob.Uri.AbsoluteUri);
// Export!
blobUrl = helper.DoExport(blob.Uri.AbsoluteUri, WhatIf);
}
}
Log.Info("Export Complete");
}
}
}