in src/Microsoft.SqlTools.ServiceLayer/DisasterRecovery/BackupOperation/BackupOperation.cs [172:327]
public override void Execute()
{
// set the operation properties before using them to create backup obejct
this.SetBackupProps();
this.backup = new Backup();
this.backup.Database = this.backupInfo.DatabaseName;
this.backup.Action = this.backupActionType;
this.backup.Incremental = this.isBackupIncremental;
try
{
if (this.backup.Action == BackupActionType.Files)
{
IDictionaryEnumerator filegroupEnumerator = this.backupInfo.SelectedFileGroup.GetEnumerator();
filegroupEnumerator.Reset();
while (filegroupEnumerator.MoveNext())
{
string currentKey = Convert.ToString(filegroupEnumerator.Key,
System.Globalization.CultureInfo.InvariantCulture);
string currentValue = Convert.ToString(filegroupEnumerator.Value,
System.Globalization.CultureInfo.InvariantCulture);
if (currentKey.IndexOf(",", StringComparison.Ordinal) < 0)
{
// is a file group
this.backup.DatabaseFileGroups.Add(currentValue);
}
else
{
// is a file
int idx = currentValue.IndexOf(".", StringComparison.Ordinal);
currentValue = currentValue.Substring(idx + 1, currentValue.Length - idx - 1);
this.backup.DatabaseFiles.Add(currentValue);
}
}
}
this.backup.BackupSetName = this.backupInfo.BackupsetName;
for (int i = 0; i < this.backupInfo.BackupPathList.Count; i++)
{
string destName = Convert.ToString(this.backupInfo.BackupPathList[i], System.Globalization.CultureInfo.InvariantCulture);
int deviceType = (int)(this.backupInfo.BackupPathDevices[destName]);
switch (deviceType)
{
case (int)DeviceType.LogicalDevice:
int backupDeviceType =
GetDeviceType(Convert.ToString(destName,
System.Globalization.CultureInfo.InvariantCulture));
if (this.backupDeviceType == BackupDeviceType.Disk && backupDeviceType == constDeviceTypeFile)
{
this.backup.Devices.AddDevice(destName, DeviceType.LogicalDevice);
}
break;
case (int)DeviceType.File:
if (this.backupDeviceType == BackupDeviceType.Disk)
{
this.backup.Devices.AddDevice(destName, DeviceType.File);
}
break;
}
}
this.backup.CopyOnly = this.backupInfo.IsCopyOnly;
this.backup.FormatMedia = this.backupInfo.FormatMedia;
this.backup.Initialize = this.backupInfo.Initialize;
this.backup.SkipTapeHeader = this.backupInfo.SkipTapeHeader;
this.backup.Checksum = this.backupInfo.Checksum;
this.backup.ContinueAfterError = this.backupInfo.ContinueAfterError;
if (!string.IsNullOrEmpty(this.backupInfo.MediaName))
{
this.backup.MediaName = this.backupInfo.MediaName;
}
if (!string.IsNullOrEmpty(this.backupInfo.MediaDescription))
{
this.backup.MediaDescription = this.backupInfo.MediaDescription;
}
if (this.backupInfo.TailLogBackup
&& !this.backupRestoreUtil.IsHADRDatabase(this.backupInfo.DatabaseName)
&& !this.backupRestoreUtil.IsMirroringEnabled(this.backupInfo.DatabaseName))
{
this.backup.NoRecovery = true;
}
if (this.backupInfo.LogTruncation)
{
this.backup.LogTruncation = BackupTruncateLogType.Truncate;
}
else
{
this.backup.LogTruncation = BackupTruncateLogType.NoTruncate;
}
if (!string.IsNullOrEmpty(this.backupInfo.BackupSetDescription))
{
this.backup.BackupSetDescription = this.backupInfo.BackupSetDescription;
}
if (this.backupInfo.RetainDays >= 0)
{
this.backup.RetainDays = this.backupInfo.RetainDays;
}
else
{
this.backup.ExpirationDate = this.backupInfo.ExpirationDate;
}
this.backup.CompressionOption = (BackupCompressionOptions)this.backupInfo.CompressionOption;
if (!string.IsNullOrEmpty(this.backupInfo.EncryptorName))
{
this.backup.EncryptionOption = new BackupEncryptionOptions((BackupEncryptionAlgorithm)this.backupInfo.EncryptionAlgorithm,
(BackupEncryptorType)this.backupInfo.EncryptorType,
this.backupInfo.EncryptorName);
}
if (this.dataContainer.Server.ConnectionContext != null)
{
// Execute backup
this.backup.SqlBackup(this.dataContainer.Server);
// Verify backup if required
if (this.backupInfo.VerifyBackupRequired)
{
Restore restore = new Restore();
restore.Devices.AddRange(this.backup.Devices);
restore.Database = this.backup.Database;
string errorMessage = null;
restore.SqlVerifyLatest(this.dataContainer.Server, out errorMessage);
if (errorMessage != null)
{
throw new DisasterRecoveryException(errorMessage);
}
}
}
}
catch(Exception)
{
throw;
}
finally
{
if (this.serverConnection != null)
{
this.serverConnection.Disconnect();
}
if(this.dataContainer != null)
{
this.dataContainer.Dispose();
}
}
}