Route53: Backup records for multiple hosted zone

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.

Picture 1. Our existing EC2

You can see that we already configured the default profile (exec: aws configure).

we also have existing dummy records on route53 and s3 bucket.

Picture 2. route53 hostedzones list
Picture 3. demo bucket

The architecture of our system should be shown like this.

Picture 4. Architecture for route53 backup

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.

Picture 5. Result on local
Picture 6. Result on S3

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!

Published by boy.suganda

My name is Boy Suganda Sinaga. I worked as Site Reliability Engineer (SRE) at Shipper Indonesia. I'm still developing my skill, both hard-skill and soft-skill. Let's work together, to bring better future for others.

Leave a Reply

Your email address will not be published. Required fields are marked *