Automatic Contentful backups with AWS CDK

Published on March 21, 2023

2023-03 Automatic Contentful Backups with AWS CDK

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Contentful Solution Partner kernpunkt Digital made an automatic backup tool for Contentful with the AWS CDK. It positively rocks. Read all about it in this guest post, which originally appeared on the kernpunkt Digital blog and is republished with permission.

Automatic Contentful Backups with AWS CDK

Every time one of our customers at kernpunkt asks for an intuitive or powerful content management solution, we literally cannot recommend Contentful fast enough. In fact, we like them so much we became a Bronze Solution Partner. Not only is Contentful sufficiently robust to master any task the editors throw at it, but it’s also insanely fun to develop with.

For example, our devs never grow tired of talking about the Contentful Migration Tool to design the content model under the Infrastructure as Code paradigm, because it adds the benefit of all changes to the model (new fields being added, etc.) being persisted to our GitHub repository. In this way, our code and the content model we develop for never diverge. But what about the content that our customer’s editors produce?

Contentful provides a backup tool via their excellent Contentful CLI. To export your Contentful space, including the content model and all content, use the following code to automatically download a JSON file with everything you need to restore your Contentful space to the current state:

contentful space export --config example-config.json

Here’s the thing about backups

Skipping backups is like skipping brushing your teeth: Sure it’s fine if you do it here and there, but if you don’t stick to it over time, it will lead to disasters down the line. Backups only really work when they’re automated. Sure, you could rely on one of your devs to regularly execute that command and put the resulting JSON file somewhere where it’s safe and sound. You might even make sure to have a recurring event in your calendar to remind you of that, or a recurring Todoist task, but why not outsource the worrying to computers?

Boy, do we have a tool for you. We built an automatic backup tool you can deploy to your AWS account with the AWS CDK Toolkit. After some easy configuration, you just need to execute cdk deploy once, and the tool will regularly — according to a schedule you define — download a backup and store it into an S3 bucket, where you will never have to worry about it ever again — until you need to restore it.

But we’re getting ahead of ourselves, let’s get to work.

Using the backup tool

1. Installation

You can find the backup tool in our GitHub Repository. To install it, first clone it to a directory of your choice:

git clone git@github.com:kernpunkt/contentful-backup.git
cd contentful-backup

Now to configure the tool. The repository provides you with a .env.example file, which you will have to customize:

cp .env.example .env

To customize the project to your liking, first set the variables for CONTENTFUL_MANAGEMENT_TOKEN (how to find your token), the CONTENTFUL_SPACE_ID (how to find your space ID), and the CONTENTFUL_ENVIRONMENT_ID (usually master).

After that, you can set the PREFIX variable. This will be used to prefix every AWS resource in the CloudFormation stack, so you can have multiple instances of the backup tool running in the same AWS profile (for example, if multiple customers are using Contentful).

If you want, you can also change the TIMEOUT_MINUTES and MEMORY_SIZE for the Lambda function, but the most important variable to set is the SCHEDULE. This will tell the backup tool how often it is supposed to create a new Contentful backup. For example, if you want to execute a backup once a day at 11pm, you could write:

(0 23 * * ? *)

This variable follows the CRON syntax. Please keep in mind that AWS uses UTC time, and adjust accordingly.

2. Deploying the stack to your AWS

To deploy the stack to your AWS environment, you first have to make sure you selected the right profile. If you’re unsure how to do that, don’t worry, here is a short tutorial.

When you’re sure your configuration is looking 👌 and you have the correct profile selected, simply run:

cdk deploy

Et voilà! After your CDK stack deploys, you should have a fully functioning, autonomously automatic backup, and you will never lose sleep over your content again!

To confirm that everything worked, head to your S3 buckets and look for one starting with the PREFIX you chose in the .env file. After your schedule triggers for the first time, you should see a JSON file in that bucket:

hello world contentful backup bucket

You can restore the current state of your Contentful space from that JSON file by using:

contentful space import --config PATH_TO_YOUR.json

3. Changing the schedule

As projects mature, updates to content also become less and less frequent, so you might want to change the backup schedule to save on resources. No problem! Simply change the SCHEDULE variable in your .env file and run cdk deploy again.

4. Uninstalling

To uninstall the backup tool (and delete the associated CloudFormation stack), run the following command with the same AWS profile you used during installation:

cdk destroy

Because of a limitation of the cdk command line tool, you will first have to make sure the S3 bucket is empty by manually deleting all files in it. After that, you can safely destroy the stack.

Conclusion

Working with Contentful is a lot of fun for editors and developers alike. Both will sleep easier knowing their content is safe and sound by employing a simple tool and never worrying about it again. Grab the repository and let us know what you think of the tool! Or, better yet: open an issue to make the tool better!

If you’re interested in a behind the scenes on how we built this tool, stick around, because we have a blog post planned to detail exactly that. Happy hacking! 🚀

We love to build cool stuff at kernpunkt, and if you share that love, we’d love to welcome you on board as soon as possible. We’re always on the lookout for talented developers, so feel free to reach out on our website kernpunkt.de/jobs.

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Related articles

In this post, we're going to unpack everything you need to know about personalization at scale to get the most ROI from your content and customer data.
Insights

Everything you need to know about personalization at scale

March 3, 2024

Illustrated graphic of two hands interacting with floating particles
Insights

Contentful brings an atomic boost to your platform

February 15, 2022

Find the right CMS for your business. Exploring the pros and cons of remaining on Craft or migrating to cloud-native headless platforms such as Contentful.
Insights

Craft to Contentful: When the right tool for where you’ve been is the wrong tool for where you need to go

September 4, 2024

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started