I’ve recently moved my Slack bots to Dokku running on DigitalOcean. The bots use local MongoDB database containers provided by the dokku-mongo plugin. Here’s the entire setup, from installation to backup to Dropbox.
Install
Create a Database
This starts a MongoDB instance which is not accessible from the outside world.
Backup with Dropbox
You can certainly rely on DigitalOcean’s weekly system backup or any other backup system. However I wanted a daily backup and a historical archive of the data in each individual MongoDB. I also wanted it to be free or cheap.
Install Dropbox
This creates ~/.dropbox-dist
, and you can manually start Dropbox via ~/.dropbox-dist/dropboxd
.
Configure First Time
Run Dropbox for the first time.
Dropboxd will tell you “This client is not linked to any account …“ and give you a link copy that and paste it in your local web browser, authenticate and validate the new connection. You can stop the daemon with Ctrl+C.
Auto-Start
Create /etc/init.d/dropbox
from this gist, eg. sudo vi /etc/init.d/dropbox
. Edit DROPBOX_USERS
below (eg. user1 user2
). I just use root
.
Set it to automatically start on boot.
Start the service.
Check that it’s running.
Selective Sync
If you need selective sync, check out this blog post.
Backup MongoDB Databases
Create an Export Script
Create Dropbox/bin/dokku-mongo-export.sh
. It enumerates Dokku MongoDB databases and runs mongo:export
on them. The archive that mongo:export produces is gzipped archive output of mongodump
.
Notice I just put the script in my Dropbox ;)
The latest version of this script is here as a gist. Make sure to chmod 700 dokku-mongo-export.sh
and run it manually to test.
Run the Script Daily
Note that only executables without an extension run from /etc.cron.*/
, hence the name of the symbolic link is different from the script.
Restoring Data
The archive that mongo:export produces is gzipped archive output of mongodump
. To restore in Dokku, use mongo:import
. To restore locally, run mongorestore --gzip --archive=filename
.
Neat
This will neatly organize your database backups in Dropbox.