private static async Task <int> ServerCopyAsync(ServerCopyOptions options, CancellationToken cancellationToken = default) { var sourceCloudStorageAccount = GetCloudStorageAccount(options.SourceConnectionString, options.SourceSASToken, options.SourceStorageAccountName); var sourceCloudFileClient = sourceCloudStorageAccount.CreateCloudFileClient(); var azureShareSnapshotManager = new AzureShareSnapshotManager(sourceCloudFileClient, $"AzureShareBackupServerCopySnapshot_{Guid.NewGuid():N}"); var snapshotShare = await azureShareSnapshotManager.CreateManagedSnapshotAsync(options.SourceShareName, cancellationToken); try { var targetCloudStorageAccount = GetCloudStorageAccount(options.TargetConnectionString, options.TargetSASToken, options.TargetStorageAccountName); var targetCloudFileClient = targetCloudStorageAccount.CreateCloudFileClient(); var targetFileShare = targetCloudFileClient.GetShareReference(options.TargetShareName); var targetDirectoryName = $"{snapshotShare.SnapshotTime:yyyyMMdd-HHmmss}"; var targetDirectory = targetFileShare.GetRootDirectoryReference().GetDirectoryReference(targetDirectoryName); await targetDirectory.CreateAsync(cancellationToken); var backupProvider = new AzureServerSideCopyBackupProvider(snapshotShare.GetRootDirectoryReference(), targetDirectory); await backupProvider.RunAsync(cancellationToken); } finally { // ReSharper disable once PossibleInvalidOperationException // we want this to happen regardless of the rest being cancelled await azureShareSnapshotManager.DeleteManagedSnapshotAsync(snapshotShare.Name, snapshotShare.SnapshotTime.Value, CancellationToken.None); } return(0); }
private static async Task <int> BackupLocalArchiveAsync(BackupLocalArchiveOptions options, CancellationToken cancellationToken) { var sourceCloudStorageAccount = GetCloudStorageAccount(options.SourceConnectionString, options.SourceSASToken, options.SourceStorageAccountName); var sourceCloudFileClient = sourceCloudStorageAccount.CreateCloudFileClient(); var azureShareSnapshotManager = new AzureShareSnapshotManager(sourceCloudFileClient, $"AzureShareBackupZipSnapshot_{Guid.NewGuid():N}"); var snapshotShare = await azureShareSnapshotManager.CreateManagedSnapshotAsync(options.SourceShareName, cancellationToken); try { var zipName = Path.Combine(options.TargetPath, $"{options.TargetFileNamePrefix}-{snapshotShare.SnapshotTime:yyyyMMdd-HHmmss}.{GetFileExtension(options.ArchiveType)}"); Log.Info(() => $"Backup to local zip: {zipName}"); var backupProvider = new LocalProcessBackupProvider( new AzureFileShareFileInfoProvider(sourceCloudFileClient, snapshotShare.Name, snapshotShare.SnapshotTime), GetOutputWriter(options.ArchiveType, new FileStream(zipName, FileMode.CreateNew))); await backupProvider.RunAsync(cancellationToken); } finally { // ReSharper disable once PossibleInvalidOperationException // we want this to happen regardless of the rest being cancelled await azureShareSnapshotManager.DeleteManagedSnapshotAsync(snapshotShare.Name, snapshotShare.SnapshotTime.Value, CancellationToken.None); } return(0); }
private static async Task <int> BackupBlobStorageArchiveAsync(BackupBlobStorageArchiveOptions options, CancellationToken cancellationToken) { var sourceCloudStorageAccount = GetCloudStorageAccount(options.SourceConnectionString, options.SourceSASToken, options.SourceStorageAccountName); var sourceCloudFileClient = sourceCloudStorageAccount.CreateCloudFileClient(); var azureShareSnapshotManager = new AzureShareSnapshotManager(sourceCloudFileClient, $"AzureShareBackupZipSnapshot_{Guid.NewGuid():N}"); var snapshotShare = await azureShareSnapshotManager.CreateManagedSnapshotAsync(options.SourceShareName, cancellationToken); try { var targetCloudStorageAccount = GetCloudStorageAccount(options.TargetConnectionString, options.TargetSASToken, options.TargetStorageAccountName); var targetCloudBlobClient = targetCloudStorageAccount.CreateCloudBlobClient(); var blobName = $"{options.TargetBlobNamePrefix}-{snapshotShare.SnapshotTime:yyyyMMdd-HHmmss}.{GetFileExtension(options.ArchiveType)}"; var blobContainer = targetCloudBlobClient.GetContainerReference(options.TargetBlobContainerName); Log.Info(() => $"Backup to blob zip: {blobContainer.Uri}/{blobName}"); var backupProvider = new LocalProcessBackupProvider( new AzureFileShareFileInfoProvider(sourceCloudFileClient, snapshotShare.Name, snapshotShare.SnapshotTime), GetOutputWriter(options.ArchiveType, await AzureStreamHelpers.GetBlobOutputStreamAsync(blobContainer, blobName, false, cancellationToken))); await backupProvider.RunAsync(cancellationToken); } finally { // ReSharper disable once PossibleInvalidOperationException // we want this to happen regardless of the rest being cancelled await azureShareSnapshotManager.DeleteManagedSnapshotAsync(snapshotShare.Name, snapshotShare.SnapshotTime.Value, CancellationToken.None); } return(0); }
private static async Task <int> SnapshotAsync(SnapshotOptions options, CancellationToken cancellationToken) { var cloudStorageAccount = GetCloudStorageAccount(options.ConnectionString, options.SASToken, options.StorageAccountName); var cloudFileClient = cloudStorageAccount.CreateCloudFileClient(); var azureShareSnapshotManager = new AzureShareSnapshotManager(cloudFileClient, $"AzureShareBackup_{options.MetadataKey}"); await azureShareSnapshotManager.CreateManagedSnapshotAsync(options.ShareName, cancellationToken); if (options.RetainSnapshots.HasValue) { await azureShareSnapshotManager.PruneManagedSnapshotsAsync(options.ShareName, new RetainLatestSnapshotsPolicy(options.RetainSnapshots.Value), cancellationToken); } return(0); }