Different Methods of Backing up and Automating Backups of Ghost on Linux (CentOS 6)

You may backup ghost using the ghost UI, or using command line. Each one has its advantages and disadvantages.

Ghost UI Command Line
Easy to use Requires command line knowledge
Doesn’t require stopping ghost requires stopping ghost
Can’t automate (currently) Can be automated
Doesn’t include images (currently) Includes images

Let’s go through them:

Ghost UI

  1. Sign in to your ghost blog as usual at yourdomain.com/ghost.
  2. Go to the debug screen at yourdomain.com/ghost/debug.
  3. There should be a big export button, click it and decide where you want to save it.
  4. Done

Ghost Debug Screen

Command Line

For those who want some more control, they can do it by command line.

Pre-requisites

  1. Root access to your server or have access to an account that has sudo powers on your servers – Instructions here to see how to give a linux user sudo powers.
  2. nodejs and npm installed – Instructions here.
  3. ghost installed – Instructions here
    1. Running with supervisord
    2. Running with forever

Making a backup directory

You can put your backups wherever you want, even on a different server or computer. For now I will just make a directory on the same server to put the backup.

[ahmed@amayem ~]$ mkdir ghostbackups
[ahmed@amayem ~]$ ls
ghostbackups

This directory will be in my home directory i.e. /home/ahmed. I can also make it in /var but then I would have to use sudo:

[ahmed@amayem ~]$ sudo mkdir /var/ghostbackups

Check here to see how to give a linux user sudo powers.

Discovering the content directory

  1. Log in to your server command line interface (probably by ssh)
  2. Go to your ghost blog directory

    [ahmed@amayem ~]$ cd /var/www/ghost/
    [ahmed@amayem ghost]$ ls -1
    bower.json
    config.example.js
    config.js
    content
    core
    Gruntfile.js
    index.js
    LICENSE
    node_modules
    npm-debug.log
    package.json
    README.md

Things look good. We are interested in the content directory, because that is where the content is obviously.

Let’s take a peek inside:

[ahmed@amayem ghost]$ ls content/
apps  data  images  themes
[ahmed@amayem ghost]$ ls content/data/
ghost.db  ghost-dev.db  README.md

That’s where the ghost database, and all your posts are.

Dangers of Copying a database while operating

We can just copy that file, however that may not be a good idea to do while ghost is running in case the database is being used. It is best to turn ghost off and then do the backing up before turning ghost back on again.

Stopping ghost

This will depend on how you are running ghost. You will probably be running it in the background using supervisor or forever.

Supervisor

I am using supervisor so it will look like the following:

[ahmed@amayem ~]$ sudo supervisorctl
ghost                            RUNNING    pid 654, uptime 14 days, 16:00:42
supervisor> stop ghost
ghost: stopped
supervisor> status
ghost                            STOPPED    Nov 05 10:41 PM
supervisor> quit
Forever

I only have ghost running on forever so I will use stopall.

$forever stopall
info:    Forever stopped processes:
data:        uid  command       script                  forever pid  logfile                 uptime      
data:    [0] 7z38 /usr/bin/node /var/www/ghost/index.js 791     793 /root/.forever/7z38.log 0:0:0:9.299

Making a copy of the content directory

You can choose to copy whatever you want. In this example I will copy the content directory only:

[ahmed@amayem ghost]$ cp -r content /home/ahmed/ghostbackups/2014-11-05GhostBackupContent
[ahmed@amayem ghost]$ ls /home/ahmed/ghostbackups/
2014-11-05GhostBackupContent

Looks good. Now we need to turn ghost back on.

Turning ghost on

Supervisor
[ahmed@amayem ghost]$ sudo supervisorctl
ghost                            STOPPED    Nov 05 10:41 PM
supervisor> start ghost
ghost: started
supervisor> status
ghost                            RUNNING    pid 25226, uptime 0:00:03

Looks good.

Forever
forever start /var/www/ghost/index.js
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: /var/www/ghost/index.js

Automating backups using cron

Now that we know how to backup a file we just need to automate it using cron:

[ahmed@amayem ghost]$ crontab -e
@weekly sudo supervisorctl stop ghost; cp -r /var/www/ghost/content "/home/ahmed/ghostbackups/$(date +%F)GhostBackupContent"; sudo supervisorctl start ghost

To test it replace @weekly with * * * * *, which should activate it every minute so you can test it out.

Of course replace weekly with whatever period you want. Also replace $(date +%F) with whatever format you want. You can see different formats in man date.

References

  1. allaboutghost.com‘s backup post.
  2. pantz.com‘s post on cron

Ahmed Amayem has written 90 articles

A Web Application Developer Entrepreneur.