AWS Backup 使用示例 AWS CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

AWS Backup 使用示例 AWS CLI

以下代码示例向您展示了如何使用with来执行操作和实现常见场景 AWS Backup。 AWS Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何使用 create-backup-plan

AWS CLI

创建备份计划

以下create-backup-plan示例创建保留期为 35 天的指定备份计划。

aws backup create-backup-plan \ --backup-plan "{\"BackupPlanName\":\"Example-Backup-Plan\",\"Rules\":[{\"RuleName\":\"DailyBackups\",\"ScheduleExpression\":\"cron(0 5 ? * * *)\",\"StartWindowMinutes\":480,\"TargetBackupVaultName\":\"Default\",\"Lifecycle\":{\"DeleteAfterDays\":35}}]}"

输出:

{ "BackupPlanId": "1fa3895c-a7f5-484a-a371-2dd6a1a9f729", "BackupPlanArn": "arn:aws:backup:us-west-2:123456789012:backup-plan:1fa3895c-a7f5-484a-a371-2dd6a1a9f729", "CreationDate": 1568928754.747, "VersionId": "ZjQ2ZTI5YWQtZDg5Yi00MzYzLWJmZTAtMDI1MzhlMDhjYjEz" }

有关更多信息,请参阅《Backup 开发者指南》中的创建AWS 备份计划

以下代码示例演示如何使用 create-backup-vault

AWS CLI

创建备份保管库

以下create-backup-vault示例创建了具有指定名称的备份存储库。

aws backup create-backup-vault --backup-vault-name sample-vault

此命令不生成任何输出。输出:

{ "BackupVaultName": "sample-vault", "BackupVaultArn": "arn:aws:backup:us-west-2:123456789012:backup-vault:sample-vault", "CreationDate": 1568928338.385 }

有关更多信息,请参阅《备份开发者指南》中的创建AWS 备份保管库

以下代码示例演示如何使用 get-backup-plan-from-template

AWS CLI

从模板中获取现有备份计划

以下get-backup-plan-from-template示例从模板中获取现有备份计划,该模板指定了保留期为 35 天的每日备份。

aws backup get-backup-plan-from-template \ --backup-plan-template-id "87c0c1ef-254d-4180-8fef-2e76a2c38aaa"

输出:

{ "BackupPlanDocument": { "Rules": [ { "RuleName": "DailyBackups", "ScheduleExpression": "cron(0 5 ? * * *)", "StartWindowMinutes": 480, "Lifecycle": { "DeleteAfterDays": 35 } } ] } }

有关更多信息,请参阅《Backup 开发者指南》中的创建AWS 备份计划

以下代码示例演示如何使用 get-backup-plan

AWS CLI

获取备份计划的详细信息

以下get-backup-plan示例显示了指定备份计划的详细信息。

aws backup get-backup-plan \ --backup-plan-id "fcbf5d8f-bd77-4f3a-9c97-f24fb3d373a5"

输出:

{ "BackupPlan": { "BackupPlanName": "Example-Backup-Plan", "Rules": [ { "RuleName": "DailyBackups", "TargetBackupVaultName": "Default", "ScheduleExpression": "cron(0 5 ? * * *)", "StartWindowMinutes": 480, "CompletionWindowMinutes": 10080, "Lifecycle": { "DeleteAfterDays": 35 }, "RuleId": "70e0ccdc-e9df-4e83-82ad-c1e5a9471cc3" } ] }, "BackupPlanId": "fcbf5d8f-bd77-4f3a-9c97-f24fb3d373a5", "BackupPlanArn": "arn:aws:backup:us-west-2:123456789012:backup-plan:fcbf5d8f-bd77-4f3a-9c97-f24fb3d373a5", "VersionId": "NjQ2ZTZkODktMGVhNy00MmQ0LWE4YjktZTkwNTQ3OTkyYTcw", "CreationDate": 1568926091.57 }

有关更多信息,请参阅《Backup 开发者指南》中的创建AWS 备份计划

以下代码示例演示如何使用 list-backup-jobs

AWS CLI

示例 1:列出所有备份任务

以下list-backup-jobs示例返回有关您 AWS 账户中备份任务的元数据。

aws backup list-backup-jobs

输出:

{ "BackupJobs": [ { "BackupJobId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "BackupVaultName": "Default", "BackupVaultArn": "arn:aws:backup:us-west-2:123456789012:backup-vault:Default", "ResourceArn": "arn:aws:ec2:us-west-2:123456789012:instance/i-12345678901234567", "CreationDate": 1600721892.929, "State": "CREATED", "PercentDone": "0.0", "IamRoleArn": "arn:aws:iam::123456789012:role/service-role/AWSBackupDefaultServiceRole", "StartBy": 1600725492.929, "ResourceType": "EC2" }, { "BackupJobId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222", "BackupVaultName": "Default", "BackupVaultArn": "arn:aws:backup:us-west-2:123456789012:backup-vault:Default", "RecoveryPointArn": "arn:aws:backup:us-west-2:123456789012:recovery-point:a1b2c3d4-5678-90ab-cdef-EXAMPLE33333", "ResourceArn": "arn:aws:elasticfilesystem:us-west-2:123456789012:file-system/fs-12345678", "CreationDate": 1600721724.77, "CompletionDate": 1600721744.488, "State": "COMPLETED", "PercentDone": "100.0", "BackupSizeInBytes": 71, "IamRoleArn": "arn:aws:iam::123456789012:role/service-role/AWSBackupDefaultServiceRole", "StartBy": 1600725324.77, "ResourceType": "EFS" } ] }

有关更多信息,请参阅《Backup 开发者指南》中的创建AWS备份

示例 2:列出已完成的备份任务

以下list-backup-jobs示例返回有关您在 AWS 账户中已完成的备份任务的元数据。

aws backup list-backup-jobs \ --by-state COMPLETED

输出:

{ "BackupJobs": [ { "BackupJobId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222", "BackupVaultName": "Default", "BackupVaultArn": "arn:aws:backup:us-west-2:123456789012:backup-vault:Default", "RecoveryPointArn": "arn:aws:backup:us-west-2:123456789012:recovery-point:a1b2c3d4-5678-90ab-cdef-EXAMPLE33333", "ResourceArn": "arn:aws:elasticfilesystem:us-west-2:123456789012:file-system/fs-12345678", "CreationDate": 1600721724.77, "CompletionDate": 1600721744.488, "State": "COMPLETED", "PercentDone": "100.0", "BackupSizeInBytes": 71, "IamRoleArn": "arn:aws:iam::123456789012:role/service-role/AWSBackupDefaultServiceRole", "StartBy": 1600725324.77, "ResourceType": "EFS" } ] }

有关更多信息,请参阅《Backup 开发者指南》中的创建AWS备份