使用快照复制Azure Blob

时间:2015-11-11 22:36:45

标签: powershell azure azure-storage azure-storage-blobs

我正在尝试备份一些blob存储以防止意外删除容器,我可以使用PowerShell和start-CopyAzurBlog复制blob,或者从命令行使用AZCopy,但是这些blob有一个数字快照(用作版本控制过程的一部分),每当我复制时,都不会拍摄快照。

我知道你可以将/ snapshot命令与AZCopy一起使用,但这会为每个快照创建一个全新的blob,我需要它们作为单个blob的一部分。 反正有没有复制这些?理想情况下,这将在PowerShell中,因此我可以使用Azure自动化,但如果需要,很乐意在C#中执行某些操作。

2 个答案:

答案 0 :(得分:3)

这里是与赵昭璐逻辑一致的代码。方法是首先列出源blob的快照,从快照创建目标中的blob并获取该blob的快照。最后,您将基本blob复制到目标blob中。

    static void CopyBlobAndSnapshots()
    {
        var sourceAccountName = "<source-account-name>";
        var sourceAccountKey = "<source-account-key>";
        var sourceContainerName = "<source-container-name>";
        var targetAccountName = "<target-account-name>";
        var targetAccountKey = "<target-account-key>";
        var targetContainerName = "<target-container-name>";
        var blobName = "<source-blob-name>";
        var sourceAccount = new CloudStorageAccount(new StorageCredentials(sourceAccountName, sourceAccountKey), true);
        var targetAccount = new CloudStorageAccount(new StorageCredentials(targetAccountName, targetAccountKey), true);
        var sourceBlobClient = sourceAccount.CreateCloudBlobClient();
        var sourceContainer = sourceBlobClient.GetContainerReference(sourceContainerName);
        var sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
        var targetBlobClient = targetAccount.CreateCloudBlobClient();
        var targetContainer = targetBlobClient.GetContainerReference(targetContainerName);
        targetContainer.CreateIfNotExists();
        //Create a SAS Token on source blob with read permissions that is valid for 2 weeks.
        var sasToken = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                Permissions = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddDays(14))
            });
        //List blob snapshots first
        var baseBlobAndSnapshots = sourceContainer.ListBlobs(blobName, true, BlobListingDetails.Snapshots).ToList();
        //Since the list contains both base blob and snapshots, we should remove the base blob from this list.
        var blobSnapshots = baseBlobAndSnapshots.Where(b => ((CloudBlockBlob)b).SnapshotTime != null).ToList();
        //Now we should arrange them in reverse chronological order
        blobSnapshots.Reverse();
        CloudBlockBlob targetBlob = null;
        string sourceBlobUrl = string.Empty;
        foreach (var blob in blobSnapshots)
        {
            var blockBlob = (CloudBlockBlob) blob;
            Console.WriteLine("Copying blob snapshot. Snapshot date/time = " + blockBlob.SnapshotTime);
            sourceBlobUrl = string.Format("{0}&{1}", blockBlob.SnapshotQualifiedUri, sasToken.Substring(1));
            targetBlob = targetContainer.GetBlockBlobReference(blobName);
            targetBlob.StartCopy(new Uri(sourceBlobUrl));
            //Check the status;
            targetBlob.FetchAttributes();
            do
            {
                var copyState = targetBlob.CopyState;
                if (copyState.Status == CopyStatus.Pending)
                {
                    Thread.Sleep(1000);//Copy not completed. Wait for it to complete.
                    targetBlob.FetchAttributes();
                }
                else
                {
                    break;
                }
            } 
            while (true);
            Console.WriteLine("Copying blob snapshot complete. Snapshot date/time = " + blockBlob.SnapshotTime);
            Console.WriteLine("----------------------------");
            //Now take the blob snapshot
            Console.WriteLine("Taking blob snapshot....");
            targetBlob.Snapshot();
            Console.WriteLine("Blob snapshot taken....");
            Console.WriteLine("----------------------------");
        }
        Console.WriteLine("Copying base blob.");
        sourceBlobUrl = string.Format("{0}{1}", sourceBlob.Uri, sasToken);
        targetBlob = targetContainer.GetBlockBlobReference(blobName);
        targetBlob.StartCopy(new Uri(sourceBlobUrl));
        //Check the status;
        targetBlob.FetchAttributes();
        do
        {
            var copyState = targetBlob.CopyState;
            if (copyState.Status == CopyStatus.Pending)
            {
                Thread.Sleep(1000);//Copy not completed. Wait for it to complete.
                targetBlob.FetchAttributes();
            }
            else
            {
                break;
            }
        } 
        while (true);
        Console.WriteLine("Blob with snapshot copy completed!");
    }

答案 1 :(得分:2)

我必须指出,您提出的功能很难实现。

Snapshot Blob API仅支持为当前blob数据创建快照。因此,为了备份快照,我们需要使用与目标中的第一个快照相同的数据创建blob,然后创建快照;并使用与第二个快照相同的数据覆盖目标blob,然后创建快照; ......;继续这样做,直到在目标中创建所有快照,最后用与当前源blob相同的数据覆盖目标blob。

由于源blob的容量实际上并不是所有快照和当前blob的总和,如果您想在备份后保存目标价格,则必须在两个快照之间或两个快照之间进行区分。最后的快照&amp;当前的blob,并且只在每一步覆盖不同的块/页面,这可能会带来更多的复杂性。