Automating local backups of DO MUP on Ubuntu

DO + MUP = 💙

I've migrated my main project, https://ciboulette.net, from Heroku to digital ocean, with the help of the awesome meteor up tool that's still actively maintained.

I wanted a way to automate the backups of production to my own computer. In meteor, the only thing really worth backing up is the content of the mongoDB.

We need two things : a script that does the backup, and a cron job to run regularly

Backup script

You just need to configure the two environment variables at the top (in bold)

daily_backup.sh

#!/bin/sh

# folder where to store the last 7 daily backups, without trailing slash
DUMPFOLDER="/home/renan/cl/app/dump"

# IP of your digital ocean droplet
DO_INSTANCE_IP="46.101.232.182"

# so that notifications know on which display to show when called from the cron tab
export DISPLAY=:0

# Let it quit when it crash
set -e

DUMPFILE="$DUMPFOLDER/daily_$(date +"%y-%m-%d").backup.gzip"

if [ ! -f $DUMPFILE ];
then
continue
else
echo "Already done today" && exit 0
fi

notify-send "Backing up ..."

# ssh to the instance, then ask the mongodb container to output an archive of all dbs, then stream that back to your machine.
ssh "root@$DO_INSTANCE_IP" "docker exec mongodb mongodump --archive --gzip" > "${DUMPFILE}"

# Only keep the last 7 dumps
ls -tp $DUMPFOLDER/ | grep -v '/$' | tail -n +8 | xargs -I {} rm -- $DUMPFOLDER/{}

notify-send "... backup done."

Make sure the script is executable with chmod +x daily_backup.sh

CRON setup

We will run this script every hour. The first time it runs it will do the backup, and the next time it will see the file exists already and just exit. The next day the filename will be different and therefore a new backup will be run. 

Open the cron editor : crontab -e 

Add the following line, modifying the path in bold to point to your script.  

1 * * * * /home/renan/cl/app/.production/daily_backup.sh >/dev/null 2>&1

This will run the script every hour when minutes are equal to 1, so if you start your computer at 8:30 it will first run at 9:01

The redirect after the command are to avoid having errors show up in the logs. Ubuntu is a bit confused when cron jobs generate outputs.  

How to restore such backup

Having backup is nice, but knowing out to restore them is better. 

#!/bin/sh

# folder with the dumps
DUMPFOLDER="/home/renan/cl/app/dump"
# IP of your digital ocean droplet
DO_INSTANCE_IP="46.101.232.182"

# Let it quit when it crash
set -e

# find the latest backup file
LATEST_FILE=$(ls -t1 "$DUMPFOLDER/" | head -n 1)
LATEST_DUMP="$DUMPFOLDER/$LATEST_FILE"
echo "Restoring to production from $LATEST_DUMP"
read -p "Press enter to continue .."

# pipe file to the mongorestore inside the mongodb docker container inside the droplet
cat "${LATEST_DUMP}" | ssh "root@$DO_INSTANCE_IP" "cat | docker exec -i mongodb mongorestore --archive --gzip --drop"