There is currently no built in solution for backing up tables in Azure Storage, but we can easily do it with the help of a tool called AzCopy which is provided by Microsoft. Below is a PowerShell script I built to simplify the process of backing up and restoring. It’s a two-step rocket: First it enumerates all your tables and saves a CSV-file with their names. Then it uses this file to perform the actual backup (and restore if needed). This way you can easily edit the CSV-file to “configure” what to backup.
This script is also available at my GitHub repo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<# Simple script for backing up and restoring Azure Storage tables. Data is save on your local disk. Prerequisites Download and install AzCopy: http://aka.ms/downloadazcopy More info about AzCopy: https://docs.microsoft.com/en-us/azure/storage/storage-use-azcopy Backing up tables 1. Edit the config section 2. Run Init-StorageAccountTablesBackup to create tables.csv 3. Open tables.csv. Verify content and remove any table you don't want to backup. 4. Run Backup-StorageAccountTables Restore tables 1. First run Remove-StorageAccountTables 2. Wait a while to let Azure remove your tables 3. Run Restore-StorageAccountTables #> ### Config ### $backupDir = "c:\backup" $AzCopyFolder = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\" $storageAccountName = "mystorageaccount" $storageAccountKey = "rtyuiertyuqweasdfghjqwekliopasdfopzxcvbnmqiopasdfopwghjklzxcvbnm==" ### Functions ### function Init-StorageAccountTablesBackup { if(!(Test-Path $backupDir)) { Write-Host ("Creating folder $backupDir") -ForegroundColor Yellow New-Item -ItemType Directory -Path $backupDir } $ctx = New-AzureStorageContext $storageAccountName -StorageAccountKey $storageAccountKey $tables = Get-AzureStorageTable -Context $ctx $tables | Sort-Object -Property "CloudTable" | Select-Object CloudTable,Uri | Export-Csv "$backupDir\tables.csv" -Encoding UTF8 } function Remove-StorageAccountTables { $tables = import-csv "$backupDir\tables.csv" -Encoding UTF8 $ctx = New-AzureStorageContext $storageAccountName -StorageAccountKey $storageAccountKey foreach($t in $tables) { $tableName = $t.CloudTable Write-Host ("Removing table $tableName") -ForegroundColor Yellow Remove-AzureStorageTable –Name $tableName –Context $ctx } } function Backup-StorageAccountTables { $tables = import-csv "$backupDir\tables.csv" -Encoding UTF8 cd $AzCopyFolder foreach($t in $tables) { $tableUrl = $t.Uri $tableName = $t.CloudTable Write-Host ("`nBacking up table $tableName") -ForegroundColor Yellow .\AzCopy.exe /Source:"$tableUrl" /Dest:"$backupDir" /SourceKey:"$storageAccountKey" /Manifest:"$tableName.manifest" } } function Restore-StorageAccountTables { $tables = import-csv "$backupDir\tables.csv" -Encoding UTF8 cd $AzCopyFolder foreach($t in $tables) { $tableUrl = $t.Uri $tableName = $t.CloudTable Write-Host ("`nRestoring table $tableName") -ForegroundColor Yellow .\AzCopy.exe /Source:"$backupDir" /Dest:"$tableUrl" /DestKey:"$storageAccountKey" /Manifest:"$tableName.manifest" /EntityOperation:InsertOrReplace } } |
If you would like an alternate approach to this problem, please look at a package I released on NuGet AzureTableUtilities — https://www.nuget.org/packages/TheByteStuff.AzureTableUtilities/
You can copy a table to a file or blob file, restore a table from a file or blob file as well as copy to another table on the same account or a different account. Filtering on PartitionKey and Timestamp is also available.
I also created a reference command line code base and put it on GitHub — https://github.com/TheByteStuff/AzureTableBackupRestore which allows this to be executed from a command line or Docker container.
Let me know if this doesn’t quite match what you want, and I’ll see if I can enhance the functionality.