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
dokku plugin:install https://github.com/dokku/dokku-mongo.git mongo
Create a Database
This starts a MongoDB instance which is not accessible from the outside world.
root@dblock-plum:/# dokku mongo:create market-bot
-----> Starting container
Waiting for container to be ready
=====> MongoDB container created: market-bot
DSN: mongodb://market-bot:******@dokku-mongo-market-bot:27017/market-bot
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
cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86_64" | tar xzf -
This creates ~/.dropbox-dist
, and you can manually start Dropbox via ~/.dropbox-dist/dropboxd
.
Configure First Time
Run Dropbox for the first time.
~/.dropbox-dist/dropboxd
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
.
DROPBOX_USERS="root"
DAEMON=.dropbox-dist/dropboxd
...
Set it to automatically start on boot.
sudo chmod +x /etc/init.d/dropbox
sudo update-rc.d dropbox defaults
Start the service.
service dropbox start
Check that it’s running.
service dropbox status
dropboxd for USER root: running (pid 993)
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 ;)
#!/bin/bash
echo "Backing up MongoDB databases to Dropbox ..."
dt=$(date +"%Y-%m-%d")
echo " today is $dt"
BACKUP_PATH=~/Dropbox/mongo/backup/$(date +"%Y")/$(date +"%B")
echo " creating $BACKUP_PATH .."
dbs=$(dokku mongo:list | grep -v ===)
for db in $dbs
do
echo " backing up $db ..."
mkdir -p $BACKUP_PATH/$db
f=$BACKUP_PATH/$db/$dt-$db.dump.gz
dokku mongo:export $db > $f
done
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
cd /etc/cron.daily/
ln -s /root/Dropbox/bin/dokku-mongo-export.sh dokku-mongo-export
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.