예제 #1
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);
        }
예제 #2
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);
        }