Wondering how to create an AWS Backup plan with AWS CLI? We can help you with this!
As a part of our AWS Support Services, we often receive similar requests from our AWS customers.
Today, let’s see the steps followed by our Support Techs to help our customers to create an AWS Backup plan with AWS CLI.
Create AWS Backup plan with AWS CLI
A backup plan is a document that contains information that Backup uses to schedule tasks that create recovery points for resources.
Here, the AWS Backup plan is set up with a copy job configuration in the backup rule.
At first, we need to create a primary backup vault in the source AWS Region and the primary vault stores the source recovery points.
Then we need to create a secondary vault in the destination Region. The secondary vault stores the recovery points that AWS Backup creates as part of the copy configuration in the backup plan.
Now let’s see the steps to create an AWS Backup plan with AWS CLI:
- Firstly, we need to create a primary vault in the source region by running the create-backup-vault command.
aws backup create-backup-vault --backup-vault-name primary --region eu-west-1
2. Then we need to create a secondary vault in the destination region by running the command again.
aws backup create-backup-vault --backup-vault-name secondary --region eu-east-2
3. After that, we can create a JSON file with the options and parameters for the backup plan.
{
"BackupPlanName": "sampleplan",
"Rules": [{
"RuleName": "HalfDayBackups",
"TargetBackupVaultName": "primary",
"ScheduleExpression": "cron(0 5/12 ? * * *)",
"StartWindowMinutes": 480,
"CompletionWindowMinutes": 10080,
"Lifecycle": {
"DeleteAfterDays": 20
},
"CopyActions": [{
"DestinationBackupVaultArn": "arn:aws:backup:eu-west-2:987654321:backup-vault:secondary",
"Lifecycle": {
"DeleteAfterDays": 20
}
}]
}]
}
Here for the ScheduleExpression field, we need to set the value according to the recovery point objective of the organization and for the Lifecycle field, we can enter a value based on the retention policy of your backup.
4. Run the create-backup-plan command and then we need to pass the JSON file as an input parameter and note the value for BackupPlanId from the output of the command.
aws backup create-backup-plan --backup-plan file://
5. Then we need to create a JSON file that sets the parameters to assign the resources to the backup plan. To specify resources for a backup plan., we can use ARNs, tags, or both.
{
"SelectionName": "Myselection",
"IamRoleArn": "arn:aws:iam::987654321:role/service-role/AWSBackupDefaultServiceRole",
"Resources": ["arn:aws:ec2:eu-west-1:987654321:volume/vol-0abcdef9876"],
"ListOfTags": [{
"ConditionType": "STRINGEQUALS",
"ConditionKey": "backup",
"ConditionValue": "yes"
}]
}
6. Then run the create-backup-selection command and pass the JSON file as an input parameter:
aws backup create-backup-selection --backup-plan-id abcd-efgh-ijkl-mnop --backup-selection file://
Here for backup-plan-id, enter the value of BackupPlanId.
On-demand job on AWS Backup
We need to run the start-backup-job command to run an on-demand backup job.
aws backup start-backup-job --backup-vault-name primary --resource-arn arn:aws:ec2:eu-west-1:987654321:volume/vol-0abcdef1234 --iam-role-arn arn:aws:iam::987654321:role/service-role/AWSBackupDefaultServiceRole --idempotency-token 623f13d2-78d2-11ea-bc58-0252ac130009 --start-window-minutes 60 --complete-window-minutes 10080 --lifecycle DeleteAfterDays=30 --region eu-west-1
Here the value idempotency-token is a unique string that we provide to distinguish between StartBackupJob calls. We can generate a unique identifier by running the uuid command on Linux OS.
We need to run the start-copy-job command to run an on-demand copy job
aws backup start-copy-job --recovery-point-arn arn:aws:ec2:eu-west-1::snapshot/snap-0abcdaf2247b33dbc --source-backup-vault-name primary --destination-backup-vault-arn arn:aws:backup:eu-west-2:987654321:backup-vault:secondary --iam-role-arn arn:aws:iam::987654321:role/service-role/AWSBackupDefaultServiceRole --idempotency-token 5aac8974-78d2-11ea-bc55-0242ac130009 --lifecycle DeleteAfterDays=30 --region eu-west-1
Here the command runs a job that copies the recovery point from the source vault (primary) to a destination vault (secondary).
Run the start-restore-job command to start a restore job. We can use the following steps to start a restore job for an Amazon EBS volume.
- First, run the get-recovery-point-restore-metadata command on the recovery point and note the values for volume ID and encryption from the output of the command.
aws backup get-recovery-point-restore-metadata --backup-vault-name primary --recovery-point-arn arn:aws:ec2:eu-west-1::snapshot/snap-0abcdaf2247b33dbc
2. Then create a JSON file that sets the parameters for the required –metadata option of the start-restore-job command. For encrypted and volumeId, enter the values that we noted in step
{
"availabilityZone":"eu-west-1a",
"encrypted":"false",
"volumeId":"vol-0ck270d4c0b2e44c9",
"volumeSize":"100",
"volumeType":"gp2"
}
4. Then run the start-restore-job command and pass the JSON file as an input parameter:
aws backup start-restore-job --recovery-point-arn arn:aws:ec2:eu-west-1::snapshot/snap-0abcdaf2247b33dbc --metadata file:// --iam-role-arn arn:aws:iam::987654321:role/service-role/AWSBackupDefaultServiceRole --idempotency-token 52e602ce-78d2-11ea-bc55-0242ac130003 --resource-type EBS --region eu-west-1
[Need help with more AWS queries? We’d be happy to assist]
Conclusion
To conclude, today we discussed the steps followed by our Support Engineers to help our customers to create an AWS Backup plan with AWS CLI.
0 Comments