How To: Backup your Ghost Blog

Ghost stores the content of your posts in a database and the images that are attached to them in the file system.

MySQL Backup

My blog uses MySQL, which means I can use AutoMySQLBackup to create scheduled (daily, weekly, and monthly) backups of my Ghost database.

Create a Ghost Backup User

First, we need to use MySQL's command line interface:

mysql -uroot -p

To create a user:

create user 'ghost_backup'@'localhost' identified by 'GHOST_BACKUP_PASSWORD';

And, to grant the new user only limited privileges:

GRANT SELECT, RELOAD, SHOW DATABASES, LOCK TABLES ON *.* to 'ghost_backup'@'localhost' IDENTIFIED BY 'GHOST_BACKUP_PASSWORD';

Make sure all the changes are committed:

flush privileges;

Before you exit MySQL's command line interface:

quit

Install AutoMySQLBackup

First, we need to get a copy of AutoMySQLBackup:

sudo mkdir /usr/local/sbin/automysqlbackup
cd /usr/local/sbin/automysqlbackup
sudo wget http://ufpr.dl.sourceforge.net/project/automysqlbackup/AutoMySQLBackup/AutoMySQLBackup%20VER%203.0/automysqlbackup-v3.0_rc6.tar.gz 

And, unpack the tarball:

sudo tar xzvf automysqlbackup-v3.0_rc6.tar.gz

Then, make the script executable and create a soft link to a directory already in our path:

sudo chmod +x /usr/local/sbin/automysqlbackup/automysqlbackup
sudo ln -s /usr/local/sbin/automysqlbackup/automysqlbackup /usr/sbin/automysqlbackup

Configure AutoMySQLBackup

First, we need to create a backup directory:

sudo mkdir /home/homer/automysqlbackup

The next step is to make a copy of the default AutoMySQLBackup configuration file:

sudo mkdir /etc/default/automysqlbackup
sudo cp automysqlbackup.conf /etc/default/automysqlbackup/automysqlbackup.conf
cd /etc/default/automysqlbackup
sudo cp automysqlbackup.conf robferguson_org.conf

Open it with a text editor. I used nano:

sudo nano robferguson_org.conf

And, update it as follows:

# Username to access the MySQL server e.g. dbuser
CONFIG_mysql_dump_username='ghost_user'

# Password to access the MySQL server e.g. password
CONFIG_mysql_dump_password='GHOST_BACKUP_PASSWORD'

# Backup directory location e.g /backups
CONFIG_backup_dir='/home/homer/automysqlbackup'

# List of DBNAMES to EXCLUDE if DBNAMES is empty, i.e. ().
CONFIG_db_exclude=( 'information_schema' 'performance_schema' )

# List of tables to exclude, in the form db_name.table_name
CONFIG_table_exclude=( 'mysql.event' )

To test your configuration, enter the following command:

sudo automysqlbackup /etc/default/automysqlbackup/robferguson_org.conf

Then, check your backup directory (e.g., /home/homer/automysqlbackup) to see if it worked. It should have created the following directories:

daily/  
fullschema/  
latest/  
monthly/  
status/  
tmp/  
weekly/

And, made entries for your database, in the daily directory.

Scheduled Backups

Now, we can create a simple backup script:

sudo nano /usr/local/sbin/backupghost

And, update it as follows:

#!/bin/sh
/usr/local/sbin/automysqlbackup /etc/default/automysqlbackup/robferguson_org.conf

Then, we need to make the script executable and create a soft link to a directory already in our path:

sudo chmod +x /usr/local/sbin/backupghost
sudo ln -s /usr/local/sbin/backupghost /usr/sbin/backupghost

Finally, we can update crontab:

sudo crontab -e

And, schedule our Ghost backup to run every night at midnight:

0 0 * * * /usr/sbin/backupghost