Published on March 21, 2023
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.
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
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.
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.
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:
You can restore the current state of your Contentful space from that JSON file by using:
contentful space import --config PATH_TO_YOUR.json
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.
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.
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.