Last updated on April 27, 2023
Introduction
In this article, I’m gonna show you how you can create an automated Confluence backup in Atlassian using an AWS serverless solution. The motivation behind this article stems from a recent task I was working on a couple of weeks ago. My goal was to run a scheduled backup of Confluence data and export it to Amazon S3. Since Confluence backups are deleted 14 days after creation, it’s a good idea to have additional copies in a different location in case something goes wrong with your Confluence instance or if you need to restore data from a point in time. I couldn’t find any cloud-based references online that resembled my intended approach, so I thought it would be worthwhile to share this solution in case it proves useful to someone else.
Why Step Functions?
Initially, I thought of writing the code in a single Lambda function. However, at the time of writing, Lambda functions can only run for a maximum of 15 minutes. Since creating backups can take a while, this limitation could potentially result in failure. To circumvent this issue, I broke up the logic into separate tasks using AWS Step Functions. A workflow in Step Functions can execute for up to one year, allowing us to manage long-running processes like taking backups.
Solution Overview
The solution consists of 3 main components (2 Lambda functions and 1 CodeBuild build project) — all orchestrated by Step Functions.
- StartBackup – handles the logic for initiating the backup.
- GetBackupProgress – handles the logic for getting the backup progress.
- MoveBackupToS3 – downloads the backup to a CodeBuild environment and moves it to an S3 bucket. I opted to use CodeBuild over a Lambda function for long-term purposes. As the backup size grows over time, there is a risk that the Lambda function may eventually fail due to its execution timeout limit. By using CodeBuild, which has a longer maximum runtime, we can avoid this potential issue and ensure the reliability of the solution in the long run.
Steps
Creating an API token in your Atlassian account
- Go to https://id.atlassian.com/manage-profile/security/api-tokens.
- Click the Create API token button.
- On the dialog that appears, enter a short and descriptive label for your token.
- Copy the created token and save it somewhere.
Creating AWS Systems Manager Agent (SSM) Parameters
- Create three parameters in AWS SSM for your Confluence email, token, and site name:
Example:
- /confluence/email – enter the email address associated with your Confluence account, which should match the one under which you created the API token
- /confluence/token – enter the API token that you’ve created
- /confluence/sitename – enter the name of your Confluence site. For instance, if your site URL is tutorialsdojo.atlassian.net, enter ‘tutorialsdojo‘ (without quotes) as the site name.
Creating an S3 bucket
- Create an S3 bucket. This bucket will serve as the destination for your backups.
- In the bucket, create a folder named lambda_functions and another one named scripts.
3. Upload these zip files in the lambda_functions folder.
4. Upload this script inside the scripts folder.
Deploying the Step Functions workflow
- Download the automated_confluence_backup.yaml template from this repository
- Go to the AWS CloudFormation Console and deploy the template. Don’t forget to deploy it in the same region where you created the S3 bucket and SSM parameters.
- Enter a stack name.
- Provide the corresponding values for the parameters.
5. Click Next > Next > Submit (click the dialog that says “I acknowledge that AWS CloudFormation might create IAM resources.“)
6. Wait until the stack creation is done.
Verification
- Under the Resources tab, go to the newly created Step Functions state machine by clicking on its Physical ID link.
2. Click Start Execution
3. Wait for the backup to be completed. If all goes well, you should see an output similar to the screenshot below:
4. Confirm if the backup is saved to your S3 bucket.
So what’s next?
You can take this solution further by running the Step Functions workflow on a schedule you prefer through Amazon EventBridge. Feel free to tinker with the workflow and include a notification in case something goes wrong with the backup process. Plus, you don’t want to let super old backups pile up in your S3 bucket. So, think about adding an S3 Lifecycle policy to help tidy up and remove outdated backups.
Conclusion
And that’s about it! Thanks for taking the time to read this article. I hope you gained something valuable. Please don’t hesitate to share it with anyone who might find it helpful too!