每小时自动执行一次备份。我需要使用Azure SDK来执行恢复,并且该恢复应该基于最新的备份。
var 备份 = webSiteForRestore.GetSiteBackups(); varlatestBackup = backups.OrderByDescending(b => b.Data.CreatedOn).FirstOrDefault();
latestBackup 对我来说为空,我不知道为什么。
当我通过 Azure 门户进入并转到应用服务,然后转到备份时,我至少有 10 个备份,因为它每小时运行一次。
首先验证备份配置是否正确 在“设置”选项卡下,选择“备份”。确认备份计划每小时运行一次,并且以前的备份被列为成功。如果所有配置均正确,您应该会看到类似“每 1 小时”的计划以及状态为“成功”的最近备份列表。
如果未配置备份,请使用门户通过您的存储帐户和容器进行设置。
确保您可以通过编程方式访问备份。
如何?
运行此命令以创建 SAS 令牌
az storage container generate-sas --account-name arkoappbackups --name webapp-backups \
--permissions rw --expiry $(date -u -d "1 year" '+%Y-%m-%dT%H:%MZ') --output tsv
通过将令牌附加到容器 URL 来构造完整的 SAS URL
https://arkoappbackups.blob.core.windows.net/<your-container>?<SAS_TOKEN>
从代码端安装和配置 Azure SDK for .NET
dotnet add package Azure.Identity
dotnet add package Microsoft.Azure.Management.AppService.Fluent
dotnet add package Microsoft.Azure.Management.ResourceManager.Fluent
dotnet add package Microsoft.Azure.Management.Websites
示例
同样安装其他包
并相应地更新您的program.cs,替换为您自己的订阅、客户端和租户 ID
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.AppService.Fluent.Models;
public class BackupRestoreExample
{
public static async Task Main(string[] args)
{
var clientId = "77585126-123-456-abc-defghijk";
var clientSecret = "0lq8Q~x12345678abc~8iOJ_OS7aUN";
var tenantId = "9329c02a-1234-5678-abcd-b6e37b19af6d";
var subscriptionId = "12345-678-9abc-82d5-9d23dbddfc7d";
var resourceGroup = "arkorg";
var webAppName = "LogStreamApp";
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);
// Authenticate and create the Azure client
var azure = Microsoft.Azure.Management.Fluent.Azure
.Authenticate(credentials)
.WithSubscription(subscriptionId);
// Fetch the Web App
var webApp = azure.WebApps.GetByResourceGroup(resourceGroup, webAppName);
if (webApp == null)
{
Console.WriteLine("Web app not found.");
return;
}
// Retrieve Backups
var backups = await azure.WebApps.Manager.Inner.WebApps
.ListBackupsAsync(resourceGroup, webAppName);
// Get the Latest Backup
var latestBackup = backups
.OrderByDescending(b => b.Scheduled)
.FirstOrDefault();
if (latestBackup != null)
{
Console.WriteLine($"Restoring backup from {latestBackup.Scheduled}");
// Perform the Restore operation using the latest backup
var restoreRequest = new RestoreRequestInner
{
StorageAccountUrl = latestBackup.StorageAccountUrl,
BlobName = latestBackup.BlobName,
Overwrite = true
};
await azure.WebApps.Manager.Inner.WebApps
.RestoreAsync(resourceGroup, webAppName, webAppName, restoreRequest);
Console.WriteLine("Restore operation started successfully.");
}
else
{
Console.WriteLine("No backups found.");
}
}
}
您可以从门户网站进行验证