Backing up your Ghost blog

in Linux, Ghost, SSH

One of the reasons I prefer Ghost to Wordpress is the ease of backing up: copy /var/www/ghost and you're done.

Well, that's not always correct. That method works fine if your installation uses an SQLite3 database. Some installations (like mine) use MySQL. I'll show a way to back up the MySQL database too.

You can find the database your installation uses by looking in /var/www/ghost/config.js.

Note: If you use these instructions, I recommend ensuring your backups work by testing them on your local environment before a catastrophe happens.

Remote Backup

Here's how to set up a cronjob that backs up the blog and MySQL database once a week (based on instructions here):

SSH into the vps and make a new directory:

mkdir /var/www/ghostbackups

Bring up the crontab:

crontab -e

You may be asked to select an editor if you have never done so.

At the bottom paste the following line:

0 0 * * 1 service ghost stop && rm -r /var/www/ghostbackups/* && cp -a /var/www/ghost/ /var/www/ghostbackups/ghostbackup`date +"%d%B%Y"`.bak && mysqldump -u ghost -p[MySQL Password] ghost > /var/www/ghostbackups/ghostbackup`date +"%d%B%Y"`.sql && service ghost start

(Note the backticks and enter your MySQL password immediately after the -p option [less the square brackets]. You can find the MySQL password in /var/www/ghost/config.js).

What this Does

We first stop Ghost to ensure nothing changes while we are backing up. To conserve space, we delete all older backups already in the directory. An archive copy of the entire blog (including images) is copied to a separate folder and the MySQL database exported.

The '-a' option is important because it preserves everything about the original copy: most importantly, the permissions. You want the permissions to stay the same or you will have big problems when you go to reinstall from your backup!

Home Backup

I tested this on a Linux workstation.

Anacron is a good choice for Linux workstations that aren't on all the time. Anacron is similar to cron but unlike cron will run missed jobs the next time the computer is turned on.

Make sure SSH keys are set up with your host. SSH into your vps and add the following command to the bottom of /etc/anacrontab:

7 1 updateGhost rsync -chazsvvP --stats root@[ipAddress]:/var/www/ghostbackups/ '/path to where you want your backups/Ghost Backups/'

Replace "ipAddress" with your blog's IP address, minus the square brackets.

I am copying the backups made earlier instead of copying the blog directly. This is because my host blocks the ports I need to receive the MySQL dump.

If you want to know what is going on there, I found a cool site called explainshell.com. It explains the different options so you can adjust them to suit your needs.

Bonus Trick

You might not want to set up SSH keys for your root user. If that's the case, in the previous anacron job add sudo -u user just before rsync, replacing 'user' with the user for whom SSH keys are set up.

Restoring from Backup

To restore from backup you will replace the contents of /var/www/ghost/ with the contents of the latest .bak file. i.e.

cd /var/www/

rm -r ghost (Use with care- the files are gone forever)

cp -r /var/www/ghostbackups/ghostbackup28December2016.bak/* /var/www/ghost/

If you use MySQL, you will have to reinstall the database as well. Before doing that though, I'd see if the current database is working. If it isn't, use the backup sql file you made. Details about how to repopulate your database can be found here.

Bottom Line

Is this a terribly complicated way to back up a blog? Yes, yes it is.

Comments