如何复制Azure表

时间:2015-03-09 22:27:28

标签: azure azure-storage

我想制作Azure表的副本而不实际阅读其内容。我想知道是否有可能制作这样的副本,以及是否可以从c#

完成

谢谢!

2 个答案:

答案 0 :(得分:1)

如果是关于复制Azure表实体,可以使用Azcopy,请参阅下面的帖子了解详细信息。

http://azure.microsoft.com/en-us/documentation/articles/storage-use-azcopy/#copy-entities-in-an-azure-table-with-azcopy-preview-version-only

答案 1 :(得分:0)

是的,您可以使用C#将Azure表备份到Blob存储。执行此操作非常有用,例如,为灾难恢复方案创建表存储到Blob的定期备份。下面是一些代码。它使用AzCopy,这是一个有用的批量Azure数据移动实用程序,您可以here。我在下面的文件名后附加一个唯一的GUID,以防止此方法的常规运行覆盖以前复制的Azure表。

public void ExportAzureTableToBlobStorage(string locationOfAzCopy, string sourceAccountName,
        string sourceKey, string sourceTable, string targetAccountName, string targetAccountKey,
        string targetContainerName,
        string targetFileName)
    {

        CreateBlobContainerIfItDoesntExist(targetAccountName, targetAccountKey, targetContainerName);

        string uniqueFileIdentifier = Guid.NewGuid().ToString();
        string uniqueManifestIdentifier = Guid.NewGuid().ToString();

        string uniqueTargetFileName = $"{targetFileName}_{uniqueFileIdentifier}";
        string arguments =
            $@"/source:https://{sourceAccountName}.table.core.windows.net/{sourceTable} /sourceKey:{sourceKey
                } 
            /dest:https://{targetAccountName}.blob.core.windows.net/{targetContainerName
                }/ /Destkey:{targetAccountKey} 
            /manifest:{uniqueTargetFileName
                } /V:.\Files\ExportLog_{uniqueManifestIdentifier}.log /Z:.\Files\ExportJnl_{uniqueManifestIdentifier
                }.jnl";

        //Configure the process in which to launch AzCopy
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = locationOfAzCopy,
                Arguments = arguments,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            }
        };

        proc.Start();
        proc.WaitForExit();

        while (!proc.StandardOutput.EndOfStream)
        {
            var runReportMessage = proc.StandardOutput.ReadToEnd();
            //Inspect runReportMessage or whatever you need to do with it. 
        }
    }

以下是自动创建指定的Blob容器的方法(如果它尚未存在):

private void CreateBlobContainerIfItDoesntExist(string targetAccountName, string targetAccountKey, string containerName)
    {
        var blobClient = GetCloudBlobClient(GenerateBlobStorageConnectionString(targetAccountName, targetAccountKey));
        var container = blobClient.GetContainerReference(containerName);

        //Create a new container, if it does not exist
        container.CreateIfNotExists();
    }