Sub topic 9
Running on-demand backups
This chapter assumes you have already created a backup vault and an IAM service role for AWS Backup, and that you can read JSON output from the AWS CLI. An on-demand backup is a backup that you start yourself, instead of waiting for a schedule in a backup plan. This chapter covers when to use one, how to start one, how to follow it to completion, and the mistakes that cause failed or never-expiring recovery points.
When to use an on-demand backup
Scheduled rules in a backup plan cover routine protection. On-demand backups cover moments that no schedule anticipates:
- Before a risky change, such as a schema migration, an OS upgrade, or a large data load.
- When you add a new resource and want a recovery point now, not at 05:00 UTC tomorrow.
- To test your restore procedure with a fresh recovery point.
- To keep a copy with a different retention period from your plan, such as a snapshot you want to keep for a year.
An on-demand backup is independent of any plan. Editing or deleting a plan does not change it, and the plan's lifecycle rules do not apply to it. The lifecycle you give at start time is the only retention rule it has.
Starting a job
The start-backup-job command needs three values: the vault that will hold the recovery point, the ARN of the resource, and the ARN of the service role AWS Backup will assume.
aws backup start-backup-job \
--backup-vault-name app-vault \
--resource-arn arn:aws:ec2:us-east-1:123456789012:volume/vol-0123456789abcdef0 \
--iam-role-arn arn:aws:iam::123456789012:role/service-role/AWSBackupDefaultServiceRole \
--lifecycle DeleteAfterDays=35The command returns right away. It does not wait for the backup to finish. The response contains:
BackupJobId, which identifies the job and is what you use to check its progress.RecoveryPointArn, the ARN the recovery point will have when the job completes.CreationDate, the time the job was created.
Save the job ID. Everything else in this chapter depends on it.
Optional parameters worth knowing
| Parameter | What it does | Default |
|---|---|---|
| `--start-window-minutes` | How long AWS Backup waits for the job to start before it marks the job expired. | 480 minutes (8 hours) |
| `--complete-window-minutes` | How long the job may run, counted from the start window, before AWS Backup cancels it. | 10080 minutes (7 days) |
| `--lifecycle` | Sets `MoveToColdStorageAfterDays` and `DeleteAfterDays`. | None, so the recovery point never expires |
| `--recovery-point-tags` | Tags applied to the recovery point, as `Key=Value` pairs. | None |
| `--idempotency-token` | A string that makes a retried request return the original job instead of starting a second one. | None |
Use the idempotency token in scripts. If a network error makes you retry the call, the token prevents two backups of the same resource.
Always set a lifecycle
If you omit --lifecycle, the recovery point has no expiration date. AWS Backup keeps it, and bills for it, until someone deletes it. This is the most common cost surprise with on-demand backups, because scheduled rules always carry a lifecycle but a hand-typed command often does not.
If you use cold storage, DeleteAfterDays must be at least 90 days greater than MoveToColdStorageAfterDays. A request that breaks this rule is rejected. Cold storage is also available only for some resource types, so a request to move an unsupported type to cold storage fails.
If the vault has a Vault Lock configuration, the lifecycle must fit within its minimum and maximum retention. A job that violates the lock is rejected, and a missing lifecycle can also fail when the vault enforces a maximum.
Following a job to completion
A job moves through a small set of states. Knowing them helps you tell a slow job from a stuck one.
EXPIRED means the job did not start within the start window. This usually happens when the resource is busy, or when too many jobs compete for the same resource. FAILED means the job started but could not finish. Some resource types can also report PARTIAL, which means part of the work succeeded and part did not.
Check one job with describe-backup-job:
aws backup describe-backup-job \
--backup-job-id <backup-job-id> \
--query '{State:State,Percent:PercentDone,Message:StatusMessage,Arn:RecoveryPointArn}'The StatusMessage field is the first place to look when a job fails. It usually names the missing permission, the unsupported setting, or the resource state that blocked the backup.
The AWS CLI has no built-in waiter for backup jobs, so scripts poll. This loop stops on any final state and prints the recovery point ARN on success:
#!/usr/bin/env bash
set -euo pipefail
JOB_ID="$1"
while true; do
STATE=$(aws backup describe-backup-job \
--backup-job-id "$JOB_ID" \
--query 'State' --output text)
echo "State: $STATE"
case "$STATE" in
COMPLETED)
aws backup describe-backup-job \
--backup-job-id "$JOB_ID" \
--query 'RecoveryPointArn' --output text
exit 0 ;;
FAILED|EXPIRED|ABORTED|PARTIAL)
aws backup describe-backup-job \
--backup-job-id "$JOB_ID" \
--query 'StatusMessage' --output text
exit 1 ;;
esac
sleep 30
doneTo see every job in a vault or in a given state, use list-backup-jobs. Filtering by --by-state FAILED and --by-backup-vault-name app-vault gives a quick failure report.
Common errors
Most failed on-demand jobs come from a short list of causes.
- Access denied on the role. The service role must have the AWS Backup managed policy for the resource type, and it must trust
backup.amazonaws.com. A role that works for EBS may lack permission for another service. - Passing a role you cannot pass. The caller needs
iam:PassRoleon the service role, orstart-backup-jobis denied before any job is created. - Wrong Region. Vault and resource must be in the same Region as the command. An ARN from
eu-west-1sent to aus-east-1endpoint fails. - Resource type not enabled. Some services must be opted in under the AWS Backup service settings before they can be backed up.
- Encryption key access. If the vault uses a customer managed KMS key, the role needs permission to use it.
- Lifecycle rejected. The 90-day cold storage rule or a Vault Lock limit was violated.
Finding the result
When a job completes, the recovery point appears in the vault. List the vault's recovery points to confirm it is there:
aws backup list-recovery-points-by-backup-vault \
--backup-vault-name app-vault \
--query 'RecoveryPoints[].{Arn:RecoveryPointArn,Status:Status,Created:CreationDate,Delete:CalculatedLifecycle.DeleteAt}'The DeleteAt value is the check that your lifecycle took effect. If it is empty, the recovery point has no expiration, which usually means the lifecycle was left out. The next chapter looks at recovery points in more detail.
Exercise
You run aws backup start-backup-job for an RDS instance without a --lifecycle option. The job completes. What is the problem, and how would you confirm and correct it?
Show answer
Without a lifecycle, the recovery point has no expiration and is kept, and billed, indefinitely. To confirm, list the recovery points in the vault and check that CalculatedLifecycle.DeleteAt is empty. To correct it, set a lifecycle on the existing recovery point with aws backup update-recovery-point-lifecycle and --lifecycle DeleteAfterDays=35, and add --lifecycle to the start command from then on.
Exercise
A script starts a backup job and gets a network timeout before it sees the response. It runs the command again. How do you avoid creating two backup jobs for the same resource?
Show answer
Pass the same --idempotency-token value on both calls. AWS Backup then treats the second request as a repeat of the first and returns the original job instead of starting another.