Short Description
Route53 is part of AWS Resources, it’s managing DNS for your services on the cloud. You can access the services easier!
You know that when you publish your endpoint to be accessed by the user, you also need to take care of DNS. Why? What if the record accidentally were deleted? Then your application is not able to access by the user. From the business side, it’s a loss because we can’t handle user requests.
Then, the suggestion is to add a backup function for your records.
This is really simple, you just need to execute bash, and if you need a scheduler, you can add it into cronjob to execute.
Prerequisites
You may need to install jq and add permission to IAM user. We will use aws-cli from the instance.
Implementation
The resources that we will use, they are EC2, IAM user, route53, and S3.
For creating EC2 instance, will not share it here, and let’s skip that part and assume we have an existing instance.
You can see that we already configured the default profile (exec: aws configure
).
we also have existing dummy records on route53 and s3 bucket.
The architecture of our system should be shown like this.
And for the simple script should be like this
!/bin/bash
set -euxo pipefail
TARGET_BUCKET="s3://boy-demos-bucket" # change to your own bucket
ROUTE53_BUCKET_PATH="route53"
zones_id=( $(aws route53 list-hosted-zones | jq -r '.HostedZones[].Id' | sed "s/\/hostedzone\///") )
zones_name=( $(aws route53 list-hosted-zones | jq -r '.HostedZones[].Name' | sed "s/.$//") )
current_date=$(date "+%Y%m%d")
for ((i=0;i<${#zones_id[@]};i++));
do
echo "creating dir ${zones_name[$i]}"
mkdir -p $ROUTE53_BUCKET_PATH/${zones_name[$i]}
echo -e "Information:\nThe Zone name\t: ${zones_name[$i]}\nThe Zone ID\t\t: ${zones_id[$i]}" > "$ROUTE53_BUCKET_PATH/${zones_name[$i]}/${zones_id[$i]}.txt"
echo "backup the record of ${zones_name[$i]}"
(aws route53 list-resource-record-sets --hosted-zone-id ${zones_id[$i]}) > "$ROUTE53_BUCKET_PATH/${zones_name[$i]}/${zones_id[$i]}_recordsets_${current_date}.json"
done
echo "store the route53 backup to s3..."
aws s3 cp ./$ROUTE53_BUCKET_PATH $TARGET_BUCKET/$ROUTE53_BUCKET_PATH --recursive --sse
echo "done..."
By this script we will backup the zone id and list of records inside multiple hostedzones. If we execute the bash script with ./backup-route53.sh
, it will give result like this both local and s3 target.
I almost forgot about the scheduler, it’s really simple. We can just add rule on cronjob like this by knowing the current path and name of our script.
# crontab –e
0 3 * * * /root/backup-route53.sh >/dev/null 2>&1
It will automatically execute your script at 3.00 everyday (based on your local instance time).
Finally, you get the backup system for route53 records!