Configuring automatic backups for Contentful

Published on May 15, 2019

Header: Configuring automatic backups

Subscribe for updates

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

In this tutorial I will explain how to set up automatic backups for your Contentful Space(s) using an AWS Lambda Function and CloudWatch for scheduling. Contentful maintains a Command Line Interface (CLI) which offers a space export command that lets you download a backup of your Space.

If you are interested in reading all the different flags/options that you can pass to the space export command go to the REAMDE on Github, but this is basically how it works:

It will create a backup which is a JSON file with the whole space structure (content model, entries, assets, roles & permissions and webhooks).

The CLI space export command works great but we have bigger plans, we want to automate this process by scheduling a routine to do it for us.

Luckily there is yet another tool called the Contentful export tool. Which is a Javascript library that helps you to backup your Contentful space (as the space export CLI) in a programmatic way.

The plan

  1. Generate a backup of your Space. By writing a NodeJS script that can collect the export options, download the backup, save it as a JSON file locally.

  2. Upload the backup to AWS S3. Once we have the JSON dump locally, the plan is to upload it to a S3 Bucket where we can collect the backups.

  3. Lambda function. We will wrap our backup script in a Lambda function so we can have the advantage of trigger it in a variety of ways. For now one good option is AWS CloudWatch which can be configured to run as a Cron Job.

  4. Execute the backup script every day. For the purpose of this tutorial, I want to schedule my script on a daily basis. You are free to adapt it to your needs.

Implementation

Let’s create a new AWS Lambda function from the AWS Console.

You will quickly notice that there are three ways to create a lambda deployment package:

  1. Use the built in editor

  2. Upload a .zip file

  3. Upload a file from Amazon S3

We will use the Upload a .zip file option which means that we will have to upload a zip file that contains our function .js file and the node_modules required for our function.

Create a package.json file with the below dependencies:

package.json

Create another file called index.js This file will contain our main function, for now we are only downloading our backup file to our local tmp folder:

index.js

Note that we are using Environment variables on the script, you can set these ENV variables on the AWS Lambda function page.

1*ozzUghSAHmw4fgL-D0kRvA

We still need to upload the dump file to a S3 bucket, so go ahead to the AWS Console, create a new bucket (or pick an existing one).

Once you have your bucket in place, we will need to set up the communication between our lambda function and the S3 bucket. In order to do that we have two options …

  1. Use your API credentials in the lambda function to talk to S3.

  2. Or set up a new IAM Role.

I will lean towards the IAM Role option just to keep things clean and simple on our function. So again from the AWS Console, go to the Security, Identity, & Compliance section and choose IAM.

Create the new IAM role and make sure to attach the AmazonS3 policy to it.

1*e4740lwWYjrTtGm-zEp6gA

One last step to make it work, link the new IAM Role to your lambda execution option:

1*ykwC9lX03ZJ4wKktPwYbDg

Awesome! We just allowed our lambda function to talk directly with our S3 bucket. Let’s write the uploader part then, this is how the final function looks like:

Scheduling the Contentful backup function

Let’s pick a trigger for our lambda function, especifically a CloudWatch Event.
Go ahead, create the trigger and schedule it as you wish, I used the daily option:

Schedule expression: rate(1 day)

Super, make sure to compress your deployment package (index.js and node_modules ), upload the zip file to the lambda’s function code section and test it!

1*kUZuQ2Bw8ytf1GrN4pc8aA

This is how the final solution looks like on the AWS lambda page.

I hope this function helps you to have a continuous backup for your space.

You can try this and other Contentful functionality with our community edition for free.

Subscribe for updates

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

Related articles

Websites still play a key role in meeting customer expectations. Read on for tips to modernize your brand’s web strategies and deliver great experiences.
Guides

Modernize your web strategy and deliver customer-winning experiences

January 11, 2024

Content operations is the secret behind streamlining content management and delivery so your creative teams can focus on exceeding their content goals.
Guides

Content operations: Getting started with a people-first approach

October 19, 2023

Let's use Algolia to create search indexes and manage data-fetching logic in a React project, and then apply these techniques to data from Contentful.
Guides

Guide to Integrating Algolia and Contentful with React JS

August 31, 2023

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