Automatic Backups with PostgreSQL, Dokku & DigitalOcean Spaces

Automatic Backups with PostgreSQL, Dokku & DigitalOcean Spaces

With DigitalOcean Spaces it's easy to serve your static files for your website or application over a CDN (Content Delivery Network) and at the same time keep database backups for your DigitalOcean hosted app. DigitalOcean Spaces utilise the same technology used by Amazon S3, namely the Amazon S3 protocol. In most cases that means that you can use the same packages or services you currently use to backup your database with Amazon S3.

In this tutorial we are going to use the dokku-postgres plugin to automatically create backups with cronjobs, that will be safely stored on your DigitalOcean Spaces on a regular schedule.

We all know how important backups are, without them your whole business or application can be rendered useless overnight, if you haven't taken the right persuasions.

So lets start by going over the technologies we are going to achieve your automatic backups, right away.

Requirements

This tutorial is aimed at people who already got an application running with Dokku and postgreSQL, which they currently host at DigitalOcean.

  • DigitalOcean Droplet
  • DigitalOcean Spaces bucket
  • Your own Dokku PaaS
  • dokku-postgres plugin

Use SSH to login to your Dokku powered DigitalOcean droplet

ssh root@123.45.67.8.910

Authenticate your Database with DigitalOcean Spaces

Once your in, its time to figure out what database you got running and what applications they are connected to. That information is necessary to authenticate with our DigitalOcean Spaces bucket.

You can see all databases linked to a specific app in Dokku by running the postgres:app-links command.

dokku postgres:app-links appname

The database name will be listed in the command-line, lets use this database authenticate with your DigitalOcean Spaces bucket. You can do that with the postgres:backup-auth command.

dokku postgres:backup-auth dbname SPACE_ACCESS_KEY_ID SPACES_SECRET_ACCESS_KEY

Use the keys that were provided to you when you first created your DigitalOcean Spaces instance.

Now your database should be authenticated. However the command is simply blank in this case. It would be great if the guys and gals behind dokku could add some sort of confirmation message here.

If you messed up something with your keys you can simply run backup-deauth and repeat the process all over again.

dokku postgres:backup-deauth dbname

Encrypt Your Backups

Now it is time to set encryption for the database. It would be a big security concern just leaving the database unencrypted on your Spaces bucket, even if the bucket it self is protected. You never know who has access to your accounts, or if someone somehow get hold of the database when it is in transit from an application to another, or to you personal computer.

To encrypt the database we can simply run the postgres:backup-set-encryption

dokku postgres:backup-set-encryption dbname passphrase

☝️ Important

Do not use your database passphrase here, and remember to keep you password somewhere safe, if you loose it you wont be able to access the database thats been saved since you first initialised the encryption.

You can always unset the encryption at anytime by using the unset command.

dokku postgres:backup-unset-encryption dbname

Manually Backup PostgreSQL Database to DigitalOcean Spaces

dokku postgres:backup dbname folder-name

Viola, now your backup should be present in your DigitalOcean Spaces under your project in your DigitalOcean dashboard.

Set Cron Job to Automatically Backup Your Database

# schedule a backup
# CRON_SCHEDULE is a crontab expression, eg. "0 3 * * *" for each day at 3am
dokku postgres:backup-schedule dbname CRON_SCHEDULE BUCKET_NAME

If you use the cron job specified above, your database will be backed up every night at 3am, server time.

Freddie Freddie 4 years, 11 months ago 0
Login to Comment
No comments have been posted yet, be the first one to comment.
10 Ways to Manage Environment Variables with Dokku Config Commands
10 Ways to Manage Environment Variables with Dokku Config Commands
Dokku is an awesome open-soure project that makes it really simple to setup and manage your own docker-powered PaaS (Platform-as-a-Service). If you're a solo developer or working in a small team with a low budget, Dokku is godsend. Most applications requires some sort configuration to...
How to Install & Setup Redis for Dokku
How to Install & Setup Redis for Dokku
Redis is a free and open-source in-memory data store which can be used as cache or in same cases even a database. Redis is super simple to install if you manage your applications with your own Dokku PaaS (Platform as a Service). In today's tutorial I will show you how we can install a...
Setup SSL Certificates & Serve your Applications over HTTPS for Free with Dokku & Let's Encrypt
Setup SSL Certificates & Serve your Applications over HTTPS for Free with Dokku & Let's Encrypt
In this tutorial we are going to take a look at how we can setup free SSL certificates for our Dokku powered applications with Let's Encrypt. Let's Encrypt is a non-profit organization that provides TLS (Transport Layer Security) encryption at no charge. Their goal is to make the web ...