Reader Question: Backing Up RDS Instances

Anthony Asks How To Back Up RDS Instances On A Schedule

Posted by Ryan S. Brown on Tue, Nov 10, 2015
In Reader Question
Tags: question, python, rds, ec2, backup, scheduling, cron

After the two-part series on snapshotting EBS volumes and pruning old backups, one reader emailed in a question about adding to the code.

Have you considered integrating the automation of RDS instance backups into the Lambda function from the EBS backup post? It seems it would be easy to do.

-Anthony

I have even better news for you. There’s no code needed to get snapshots and backup rotation to RDS, it’s built right in! There is a “backup retention” parameter on RDS instances that can be set when you create the instance, or added later.

If the backup retention is set to zero, then no backups are taken. For any non-zero number, the daily backups are kept for that number of days before being deleted automatically.

With the ever-dependable awscli, you can do this by running:

aws rds modify-db-instance --db-instance-identifier [your DB] --backup-retention-period 7

This will configure backups to be taken automatically and retained for 7 days, which is plenty for me. However, that’s not all I want to do when I enable backups. By default, this change will be applied during the next maintenance window for your database. If you want it to be applied ASAP, add the --apply-immediately option. Be aware that setting this from 0 to a positive value may cause an outage for your database so be wary of that.

I’m a heavy user of resource tags, and I especially like how much more visibility they give me in my AWS bill. Stackdriver has some great tagging tips if you’re interested in learning more.

To make sure that my bill accurately reflects my cost centers, I want the snapshots to have the same resource tags as the database they came from. You can add --copy-tags-to-snapshot to the command line above to have the resource tags propagate.

It’s also a good idea to have backups run at low-traffic periods, and RDS also has a setting to allow that. The --preferred-backup-window is a 30-minute (or longer) time period where you want AWS to take the snapshot. If your users are primarily in the United States, then around 3 a.m. is probably a good time.

In UTC, that’s 8:00, so let’s use that as our backup time. To set it, we’ll pass --preferred-backup-window 08:00-09:00 to the command line. The final command to set up our RDS snapshots would look like:

aws rds modify-db-instance --db-instance-identifier [your DB] \
    --backup-retention-period 7 \
    --copy-tags-to-snapshot \
    --preferred-backup-window "08:00-09:00"

Now we’ve got backups that are retained for one week, have all our cost tags, and are taken at a low-traffic period for our application. As a bonus, we didn’t need to write any code at all.

Find me on Twitter as @ryan_sb or email me at ryan@serverlesscode.com if you have an idea, question, comment, or just want to say hello.

For updates on new posts and projects, subscribe via RSS or email.


Tweet this, send to Hackernews, or post on Reddit