How To Backup A Ghost Blog On Linux
The Ghost blogging platform is wonderful for new users looking to host their own software, due to its automatic installation script. Unfortunately, there is no automatic backup script. As a result, many Ghost installations go without backups. An easy way to backup a Ghost blog is to use the integrated backup manager. While it’s true that this method isn’t as thorough and won’t give as much freedom, it’s useful in a pinch.
To backup a Ghost blog from the Ghost interface, open up a new browser tab, and go to the following URL:
https://myghostblog.com/ghost/settings/labs/
On the Labs page in the Ghost web interface, look for the “Export” button and select it. Clicking Export will allow you to make a backup of the entire blog. To restore this type of backup at a later date, go to the same URL, click “Import”, browse for the backup and load it up. Soon after importing, settings and data should be back the way it was when you first backed it up.
Command-line Backup
Ghost has a decent backup tool built into its software, but it doesn’t offer up the same type of security, automation, and freedom that can come from a backup via the Linux command line. To backup a Ghost blog on a Linux host, create a new folder. This folder will hold all of the important backup files and data pertaining to Ghost.
In the server terminal, use the mkdir command to create a new folder. Do not do this as Root!
mkdir -p ~/ghost-blog-backup mkdir -p ~/ghost-blog-backup/sql
Running mkdir creates a new directory in the /home/ folder of the user currently logged in. This is a good location to hold backup files, as they won’t get lost in random locations on the Linux file system.
Next, use sqldump to export the Ghost SQL database.
Note: write Root in “username” and the name of the database given to Ghost during the installation.
mysqldump -u username -p databasename > db.ghost_blog.sql
Enter the Ghost directory using the CD command.
cd /var/www/ghost
Inside of the Ghost software directory, execute ghost stop to turn off the Ghost software.
ghost stop
With the server software turned off, it’s safe to make a complete copy of everything to ~/ghost-blog-backup.
cp -rp /var/www/ghost/* ~/ghost-blog-backup
Running the CP command with the RP flags will ensure all permissions set by the Ghost installation software stay intact. From here, use CD to move out of /var/www/ghost, and back to /home/username.
cd ~/
Compress Backup
All of the important Ghost blog files are in ~/ghost-blog-backup, with their permissions intact. The next step is to compress these files into a single Tar archive. Creating a Tar archive of Ghost makes it much easier to transport backups off of servers, load to places like Dropbox, NextCloud, or share with co-workers.
To create a new compressed archive of the Ghost backup, use the tar command in terminal.
Note: be sure to copy the command exactly. Failing to do so may fail to preserve file permissions during the compression process!
tar -zcvpf ghost-blog-backup.tar.gz /home/username/ghost-blog-backup
Encrypt Backup
The next step in the backup process is a critical one. Without this step, your Ghost blog’s database, critical system files, and more are exposed and accessible to anyone that can access where you’ve stored the Tar archive. Luckily, it’s very easy to encrypt a Tar archive on Linux.
The quickest way to encrypt from the Linux command line is to use GnuPG. To encrypt, ensure that “gpg” is installed. From there, run the following command:
sudo gpg -c ghost-blog-backup.tar.gz
Running gpg with the c flag will instantly start the encryption process. When the process finishes, the end result is ghost-blog-backup.tar.gz.gpg.
Now that the backup is encrypted with GPG, it’s safe to delete the unencrypted Tar archive. To delete it, use the rm command.
rm ghost-blog-backup.tar.gz
Restore Backup
Restoring a Ghost backup on a new system starts by decrypting the backup. Move ghost-blog-backup.tar.gz.gpg to /home/username/ on the server and decrypt with:
gpg ghost-blog-backup.tar.gz.gpg
Next, extract the unencrypted archive.
tar -xvpf ghost-blog-backup.tar.gz
Using CD, move the terminal into the SQL folder and use the mysqldump command to restore the database.
cd ~/ghost-blog-backup/sql sudo -s mysqldump -u username -p databasename < db.ghost_blog.sql
rm -rf /home/username/ghost-blog-backup/sql
After restoring the database, move the system files into place, with CP.
cp -rp /home/username/ghost-blog-backup/* /var/www/
Once everything is restored, re-install Ghost-cli with NPM.
Note: you may need to re-install NodeJS, Yarn, etc. Refer to our tutorial for help here.
sudo npm i -g ghost-cli
Finally, start the Ghost server software up with:
ghost start
Running the ghost start command should instantly turn on the Ghost blogging service on the server.