Automatic Backups with PostgreSQL, Dokku & DigitalOcean Spaces

DigitalOcean DigitalOcean
DigitalOcean Spaces DigitalOcean Spaces
Dokku Dokku
PostgreSQL PostgreSQL

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.

Did You Enjoy the Tutorial?

Consider sending some love to Freddie the creator.

Comments (0)

You need to be logged in to comment..

Topics Mentioned in this Tutorial

DigitalOcean
2
0
1
DigitalOcean is cloud service provider that is popular among developers. DigitalOcean offers droplets (VPS instances) for an affordable price, with the ability to scale at demand. ...
DigitalOcean Spaces
1
0
0
DigitalOcean Spaces is a static storage and CDN provided by the cloud service provider DigitalOcean. Spaces is the equivalent of what Amazon S3 is for AWS, in fact it's even built on the S3 protocol. With DigitalOcean Spaces web developer can host static files including images, videos and assets and host them over a Content Delivery Network (CDN). The Spaces fuses in with you existing DigitalOcean Droplet and is easy to manage and configure with your existing project....
Dokku
7
0
0
With Dokku web developers can build their own PaaS (Platform-as-a-Service) with help of Docker and open sourced Heroku build packs. Dokku Installs on any hardware and drastically lowers your cost as Dokku is both free and open source. The dokku setup process is extremly simple and only takes a matter of minutes to complete. Once Dokku is installed and your PaaS is ready for use you can push your local development project using Git. Which builds your applicaion using Heroku buildpacks that runs in isolated Docker containers. As a result, you can run your own PaaS on the cloud provider of your c...
PostgreSQL
4
0
7
PostgreSQL, also known as Postgres, is a relational database management system (RDBMS) that is free and open-source. The software is widely popular for storing relation data for web-base applications. PostgreSQL emphasise extensibility and technical standards compliance. Which make the database used for a wide variety of web services with many concurrent users. PostgreSQL can be deployed on a single machine or spread out on clusters among different data centers....