Backing Up Your Ghost Blog

While setting up the ghost blogging engine, I noticed there isn't a great way to back up content. The official way is to go to the "labs" section of the control panel and select the export option. This downloads a big JSON file to your computer. A start, but lacks images, themes, and possibly other content (it's beta). Also. being manual process, prone to be forgotten... not exactly what you want for your backups.

To solve the problem I wrote a little shell script which you can run daily (or any schedule you'd like).

  • Compressed to save space
  • Automatic cleanup of old backups
  • Can be run manually or scheduled via cron
  • Shuts down ghost to avoid corruption, but only for the shortest amount of time needed
  • Configurable to your specific installation

A word of caution: this script backs up the configuration to the same machine your blog lives on. When that machine dies, you will be left without a way to recover your blog and you will be sad. Don't be sad! Use an offsite backup. Some ideas:

  • modify this script to rsync the backup file to a different server at the end
  • make your backup folder a synced folder (Dropbox, Box, etc)
  • use a cloud backup service to backup your backup folder

Installation Instructions

  1. login to your server as root (or use sudo su - to become root)
  2. Run the following commands to download the script and make it executable cd /root curl -O https://raw.githubusercontent.com/LambdaDriver/ghost-backup/master/ghost-backup.sh chmod 755 ghost-backup.sh
  3. Adjust the configuration at the top of the script as needed
  4. Add to crontab so the backup is automated (see below cron schedules for some examples if you're new to cron) crontab -e
  5. Make sure you can run the script manually without errors ./ghost-backup.sh

Installation Example

Example Cron schedules

Daily at 12:05AM/00:05
5 0 * * * /root/ghost-backup.sh >/dev/null 2>&1

Weekly on Sunday morning at 12:05AM/00:05
5 0 * * 0 /root/ghost-backup.sh >/dev/null 2>&1

ghost-backup.sh

https://github.com/LambdaDriver/ghost-backup

#!/bin/bash
# backs up Ghost blog content
# requires downtime, but attempts to keep it at a minimum
# created by Dave Snigier - https://davesnigier.com/backups-for-ghost-blogs

### ----- Configuration ----- ###

GHOST_HOME_DIRECTORY="/var/www/ghost"  
GHOST_STOP_CMD="service ghost stop"  
GHOST_START_CMD="service ghost start"  
BACKUP_LOG="/var/log/ghost-backup.log"  
BACKUP_DIRECTORY="/root/ghost-backup"  
BACKUP_DAYS_TO_KEEP=30

### --------- Code ---------- ###

# log a line with datetime stamp and also send to stdout
log () {  
    echo "${@}"
    echo $(date '+%Y-%m-%d %H:%M:%S') "${@}" >> "${BACKUP_LOG}"
}

log "[INFO] Started ghost backup script"

# create backup directory if it doesn't exist
output=$(mkdir -p "${BACKUP_DIRECTORY}" 2>&1)  
if [[ $? != 0 ]]; then  
    log "[ERROR] Cannot create backup directory: ${output}"
    exit 1
fi

# stop ghost so database doesn't get corrupted
output=$(${GHOST_STOP_CMD} 2>&1)  
if [[ $? != 0 ]]; then  
    log "[ERROR] Cannot stop Ghost. Call the ghostbusters?: ${output}"
    exit 1
fi  
log "[INFO] Ghost stopped"

# bundle and compress all files in ghost install
backup_filename="ghost-backup_$(date '+%Y-%m-%dT%H%M%S').tar.gz"  
backup_path="${BACKUP_DIRECTORY}/${backup_filename}"  
cd "${GHOST_HOME_DIRECTORY}"  
output=$(tar cfz "${backup_path}" "content" 2>&1)  
if [[ $? != 0 ]]; then  
    log "[ERROR] Cannot create backup file: ${output}"
else  
    log "[INFO] Ghost backed up to ${backup_filename}"
fi

# start ghost back up again
output=$(${GHOST_START_CMD} 2>&1)  
if [[ $? != 0 ]]; then  
    log "[ERROR] Cannot start Ghost: ${output}"
    exit 1
fi  
log "[INFO] Ghost started"


# remove old backups
log "[INFO] Deleting backups older than ${BACKUP_DAYS_TO_KEEP} days"  
output=$(find "${BACKUP_DIRECTORY}" -iname 'ghost-backup_*.tar.gz' -mtime +${BACKUP_DAYS_TO_KEEP} -delete 2>&1)  
if [[ $? != 0 ]]; then  
    log "[ERROR] Unable to remove old backups: ${output}"
    exit 1
fi


log "[INFO] Script finished"  

View Comments