# How to Automatically Schedule Your Dokku Database Backups

Source: https://www.ironin.it/blog/how-to-automatically-schedule-your-dokku-database-backups.html

Published 2 March 2018

[devops](https://www.ironin.it/blog/tags/devops) [infrastructure](https://www.ironin.it/blog/tags/infrastructure) [database](https://www.ironin.it/blog/tags/database)

![How to Automatically Schedule Your Dokku Database Backups](https://www.ironin.it/api/media/file/ironin-seed-N2Q5YzdiNTAtOGE1Yi00N2U1LTg3MTMtNDExMTcyYmNlZTI0_how-to-automatically-schedule-your-dokku-database.jpg)

The first rule of software is that you need to create and store database backups from time to time. This is also the case when using Dokku with PostgreSQL for hosting your apps. To automate the process and make it easier, it’s best to schedule your backups automatically, encrypt them and send to your AWS S3. We provide the know how in our step-by-step explanation.

Having backups of your data is an essential security measure in the world of IT, although it may seem like a bit of a headache sometimes - remembering to creating backups for all your data, projects, apps, etc. Automation of this process in any measure will save you a bit of time and energy. Thanks to a recent [PostgreSQL plugin](https://github.com/dokku/dokku-postgres) version, you can now encrypt a database dump before sending it to the AWS S3, which makes it a feasible solution for backing up your database to one side. Let’s then take you through the process of backup management with Dokku: scheduling backups automatically, encrypting them and then sending them to AWS S3.

## Tutorial

First of all you need to ensure you are using latest version of the PostgreSQL plugin. To update it run:

```bash
dokku plugin:update postgresql
```

Now we can provide an encryption key for backup:

```bash
dokku postgres:backup-set-encryption app-name ENCRYPTION_KEY
```

\`ENCRYPTION_KEY\` must be a phrase (string) that will be used for encrypting the backup - at the time of writing this, the plugin does not support using GPG keys from a GPG agent.

We suggest using a password generator to generate random, long passwords for encryption.

After that, create a new user for AWS with \`ACCESS_KEY\` and \`SECRET_ACCESS_KEY\`. Here is the minimum policy which allows us to create backups in a bucket:

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:GetBucketLocation"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::your-bucket-name",
                "arn:aws:s3:::your-bucket-name/*"
            ]
        }
    ]
}
```

Remember to update \`your-bucket-name\` with the actual name of the S3 bucket where you are going to store your database dumps.

Now you can provide credentials to the plugin:

```bash
dokku postgres:backup-auth app-name AWS_ACCESS_KEY AWS_SECRET_ACCESS_KEY
```

To check if backups are working correctly, run the command:

```bash
dokku postgres:backup app-name bucket-name
```

It should create a new database dump in your bucket. You will need to provide the \`ENCRYPTION_KEY\` in order to decrypt it.

If everything is working, you can then schedule automatic backups using [crontab rules](https://crontab.guru/):

```bash
# Perform database backup for `app-name` and push to `bucket` every day at 10:00
dokku postgres:backup-schedule app-name 0 10 * * * bucket
```

To restore a database from the backup, use \`pg_restore\` (to avoid [encoding issues](https://github.com/dokku/dokku-postgres/issues/121)).

We hope you have found our step-by-step guide on automatically scheduled database backups in Dokku a useful one - and make sure to check out our other blog posts for other automation tips and tricks. At iRonin we are all about finding the solutions to help minimize repetitive manual work in our clients’ software management configurations. Our expert DevOps team can help to save you time, money, and resources by re-configuring your systems. [Contact us](https://www.ironin.it/contact-us.html) to find out more!

Share:

Author: iRonin IT Team

Need software like this built?

[Software development →](https://www.ironin.it/ai-software-engineering)

Our code is merged into 10 open-source AI-agent projects, two of them with tens of thousands of GitHub stars.

[Talk to us →](https://www.ironin.it/contact-us.html?offer=ai-software-engineering)

## Read similar articles

- [Automation = Time Savior](https://www.ironin.it/blog/automation-time-savior-using-hubot-as-a-deployment-assistant.html)
- [5 common mistakes of Ruby on Rails web application testing made by startups](https://www.ironin.it/blog/5-common-mistakes-of-ruby-on-rails-web-application-testing-made-by-startups.html)
- [19 Quick Fixes for Getting Your Servers and Application Security Tighter Than Fort Knox](https://www.ironin.it/blog/19-quick-fixes-for-getting-your-servers-and-application-secure.html)
- [Tutorial - deploy to S3 and OpsWorks from AWS by creating separate users](https://www.ironin.it/blog/tutorial-deploy-to-s3-and-opsworks-from-aws-by-creating-separate-users.html)

## Read next

- [It’s Ruby’s 25th Birthday!](https://www.ironin.it/blog/its-rubys-25th-birthday.html)
- [GraphQL Part #3 - Creating a GraphQL Client with the Vue.js Framework](https://www.ironin.it/blog/creating-a-graphql-client-with-the-vuejs.html)
- [A Simple Kubernetes Cluster on AWS - Tutorial](https://www.ironin.it/blog/a-simple-kubernetes-cluster-on-aws.html)
