简体   繁体   English

Azure存储帐户备份(表和blob)

[英]Azure storage account backup (tables and blobs)

I need to periodically backup all blobs and tables in an Azure storage account so that we can restore all that data at a later time if we for any reason corrupt our data. 我需要定期备份Azure存储帐户中的所有blob和表,以便我们可以在以后因任何原因损坏我们的数据而还原所有数据。

While I trust that data that we store in Azure is durable and recoverable in case of data center failures, we still need data in our storage accounts to be backed up to prevent from accidental overwrites and deletions (the human error factor). 虽然我相信我们存储在Azure中的数据是持久的并且在数据中心发生故障时可以恢复,但我们仍然需要备份存储帐户中的数据以防止意外覆盖和删除(人为错误因素)。

We have implemented a solution for this that periodically lists all blobs and copies them over to a backup storage account. 我们为此实施了一个解决方案,定期列出所有blob并将其复制到备份存储帐户。 When a blob has been modified or deleted we simply create a snapshot of the old version in the backup account. 修改或删除blob后,我们只需在备份帐户中创建旧版本的快照。

This approach has worked OK for us. 这种方法对我们有效。 But it only handles blobs, not table entities. 但它只处理blob,而不是表实体。 We now need to support backing up table entities too. 我们现在也需要支持备份表实体。

Faced with this task now, I'm thinking that someone else probably have had this requirement before and come up with a smart solution. 现在面对这个任务,我想其他人可能之前已经有了这个要求并提出了一个智能解决方案。 Or maybe there are commercial products that will do this? 或者也许有商业产品可以做到这一点?

It is not a requirement that the backup target is another Azure storage account. 备份目标不是另一个Azure存储帐户。 All we need is a way to recover all blobs and tables as they were at the time we ran the backup. 我们所需要的只是一种恢复所有blob和表的方法,就像我们运行备份时一样。

Any help is appreciated! 任何帮助表示赞赏!

There are a variety of ways this can be handled. 有多种方法可以处理。

If you want to do this on your own you can use the storage libraries and write code to just run through the table and pull down the data. 如果您想自己动手,可以使用存储库并编写代码来运行表格并下拉数据。

There are also a few services that can do this for you as well (FULL Disclosure: I work for a company that provides this as a service). 还有一些服务可以为您执行此操作(完全披露:我为一家提供此服务的公司工作)。 Here is an article by Troy Hunt talking about our option: http://www.troyhunt.com/2014/01/azure-will-save-you-from-unexpected_28.html . 以下是Troy Hunt关于我们选择的文章: http//www.troyhunt.com/2014/01/azure-will-save-you-from-unexpected_28.html We also have PowerShell Cmdlets that can pull table data down for you (cerebrata.com). 我们还有PowerShell Cmdlet可以为您提取表格数据(cerebrata.com)。 To be fair we are not the only players in this space and there are others who have similar services. 公平地说,我们不是这个领域的唯一参与者,还有其他人拥有类似的服务。

Finally, at Tech Ed they announced that the AZCopy tool will be updated later this year so that it can pull down entire tables, which is just automating the reading through tables and pulling them down. 最后,在Tech Ed上,他们宣布AZCopy工具将在今年晚些时候更新,以便它可以拉下整个表格,这只是自动读取表格并将其拉下来。 There is currently no way to "Snapshot" a table so all of the methods above will result in a copy as the data is copied over, it might have changed in the source table by the time the copy is completed. 目前无法对表进行“快照”,因此上述所有方法都会在复制数据时生成副本,在复制完成时可能已在源表中进行了更改。

I've recently put together a simple solution to backup table storage. 我最近整理了一个简单的备份表存储解决方案。 It uses the AzCopy tool and the Storage Rest Api to pull down a list of all the tables and do a backup to JSON. 它使用AzCopy工具和Storage Rest Api下拉所有表的列表并对JSON进行备份。

Hope it's useful! 希望它有用!

param(
[parameter(Mandatory=$true)]
[string]$Account,
[parameter(Mandatory=$true)]
[string]$SASToken,
[parameter(Mandatory=$true)]
[string]$OutputDir
)
$ErrorActionPreference = "Stop"

##Example Usage
#.\Backup-TableStorage.ps1 -OutputDir "d:\tablebackup" -Account "examplestorageaccount" -SASToken "?sv=2015-04-05&ss=t&srt=sco&sp=rl&st=2016-04-08T07%3A44%3A00Z&se=2016-04-09T07%3A55%3A00Z&sig=CNotAREALSIGNITUREBUTYOURESWOUDLGOHERE3D"

if (-not (Test-Path "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\AzCopy\AzCopy.exe"))
{
    throw "Azcopy not installed - get it from here: https://azure.microsoft.com/en-gb/documentation/articles/storage-use-azcopy/"
}

Write-host ""
Write-Host "Starting backup for account" -ForegroundColor Yellow 
Write-host "--------------------------" -ForegroundColor Yellow
Write-Host " -Account: $Account"
Write-Host " -Token: $SASToken"

$response = Invoke-WebRequest -Uri "https://$Account.table.core.windows.net/Tables/$SASToken"
[xml]$tables = $response.Content
$tableNames = $tables.feed.entry.content.properties.TableName

Write-host ""
Write-host "Found Tables to backup" -ForegroundColor Yellow
Write-host "--------------------------" -ForegroundColor Yellow
foreach ($tableName in $tableNames)
{
    Write-Host " -Table: $tableName"
}


foreach ($tableName in $tableNames)
{
    $url = "https://$Account.table.core.windows.net/$tableName"

    Write-host ""
    Write-Host "Backing up Table: $url"-ForegroundColor Yellow
    Write-host "--------------------------" -ForegroundColor Yellow
    Write-host ""

    & "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\AzCopy\AzCopy.exe" /Source:$url /Dest:$OutputDir\$account\ /SourceSAS:$SASToken /Z:"$env:temp\$([guid]::NewGuid()).azcopyJournal"   

    Write-host ""
    Write-host "Backup completed" -ForegroundColor Green
    Write-host ""
    Write-host ""

}

For more details on usage have a look here: 有关使用的更多详细信息,请查看此处:

https://gripdev.wordpress.com/2016/04/08/backup-azure-table-storage-quick-powershell-script/ https://gripdev.wordpress.com/2016/04/08/backup-azure-table-storage-quick-powershell-script/

You can backup any Azure Table Storage table (not blobs though) with free software like Slazure Light. 您可以使用Slazure Light等免费软件备份任何Azure表存储表(不过blob)。 The following C# code backup all your Azure Tables to json files: 以下C#代码将所有Azure表备份到json文件:

Download NuGet packages first: 首先下载NuGet包:

Install-Package Azure.Storage.Slazure.Light 安装包Azure.Storage.Slazure.Light

Create a console application in Visual Studio and add the following code: 在Visual Studio中创建一个控制台应用程序并添加以下代码:

using System;
using System.Linq;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
using SysSurge.Slazure.AzureTableStorage;

namespace BackupAzureTableStore
{
    class Program
    {
        /// <summary>
        /// Usage: BackupAzureTableStore.exe "UseDevelopmentStorage=true"
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var storage = new DynStorage(args.Length == 0 ? "UseDevelopmentStorage=true" : args[0]);

            foreach (var cloudTable in storage.Tables)
            {
                var tableName = cloudTable.Name;
                var fileName = $"{tableName}.json";

                using (var file = new System.IO.StreamWriter(fileName))
                {
                    var dynTable = new DynTable(storage.StorageAccount, tableName);

                    TableContinuationToken token = null; // Continuation token required if > 1,000 rows per table
                    do
                    {
                        var queryResult =
                            dynTable.TableClient.GetTableReference(tableName)
                                .ExecuteQuerySegmented(new TableQuery(), token);
                        file.WriteLine("{{{0} : [", JsonConvert.SerializeObject(tableName));
                        var cntr = 0;
                        foreach (var entity in queryResult.Results)
                        {
                            var dynEntity = dynTable.Entity(entity.PartitionKey, entity.RowKey);
                            dynEntity.LoadAll().ToList(); // Force pre-downloading of all properties

                            file.WriteLine("{0}{1}", cntr++ > 0 ? "," : string.Empty,
                                JsonConvert.SerializeObject(dynEntity));
                        }

                        file.WriteLine("]}");

                        token = queryResult.ContinuationToken;
                    } while (token != null);
                }

            }

            Console.WriteLine("Done. Press a key...");
            Console.ReadKey();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM