I've migrated my main project, from Heroku to Digital Ocean, with the help of the awesome meteor up tool.
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
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 '/#39; | tail -n +8 | xargs -I {} rm -- $DUMPFOLDER/{}
notify-send "... backup done."
Make sure the script is executable with chmod +x daily_backup.sh
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.
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"