Back Up Your Ghost Blog with Google Drive
I recently set up a new blog using the Ghost blogging platform on a self-hosted AWS EC2 instance. With many thanks to my years of experience as a DevOps Engineer, I was able to set up a snapshot policy to fully back up the drive of the instance each night. However, this isn’t a free option, and I’d like a more modular way to back up just the blog itself and its corresponding configurations. This way I could easily export my blog to another hosting platform if I were to choose to do so.
I began to search around for the cheapest (ideally free) options available. There were a few criteria that had to be met for this solution.
- Utility, app or a script to execute the backup.
- A location to store the backup.
- Some kind of versioning system that allowed me to pick and choose which backup to restore.
Working backward I decided it was easiest, and free, to use the cloud storage provider I already use every day, Google Drive. This solution would knock out two of my criteria, a location to store the backup, as well as a handy versioning system already built into the application. That leaves with one last requirement, the actual utility that will perform the backup. Enter ghost-backup.
How ghost-backup works
- Schedules a daily task to perform the backup.
- Automatically outputs the backup archives in your Google Drive.
- Ability to get a notification (via Telegram) about the status of your most recent backup.
Setup is quite simple and only involves two commands. The only prerequisite is that you must have Python 3 installed and configured. Follow this tutorial if you need help doing so.
Clone the repo
git clone https://github.com/gonzolively/ghost-backup && cd ghost-backup
Run the setup script
After running this script you’ll be taken through an interactive setup process, albeit crude and command-line based. In addition to backing up your blog posts, you can also back up the images and themes of your blog.
First, you will be prompted to choose whether or not you’d like to back up the images in your blog.
The second step prompts you whether or not you’d like to backup your theme.
Next, you will be prompted for your app name. This name will be used as a part of the filename format to provide more detail. This is useful if you have more than one blog on the server and subsequently more than one backup file.
The fourth step prompts you for the MySQL hostname (i.e. the hostname of your machine). In 99% of cases, I recommend the default of localhost.
Then you will be prompted for your MySQL database user's username.
The sixth step prompts you for your MySQL database user's password.
Lastly, you will be prompted for the database name in which your blog is stored.
Once this step is completed you’ll be prompted to authenticate your Telegram account if you chose to get back alerts. After that setup is done. Your blog is now able to backup to google drive! The next sections will discuss in more detail the backup file format and how to restore your blog using this file.
Backup file format
appname= Value entered in step 3.
20190325142251= Timestamp string including Year/Month/Day of Month/Time. (See chart below for more details.)
Restoring your blog from the backup file
Restoring your blog from the backup file created by ghost-backup is a very easy and seamless process. Follow these simple steps.
- Head over to your Google Drive account and download the backup file.
- Use SCP, FTP, or your favorite file transfer utility to get the backup file onto your server.
scp 20190325142251.tar.gz webserver:/tmp
3. Unpack the archive using the following command.
tar -xvzf appname-timestamp.tar.gz
4. Jump into the new directory that was created after un-archiving your file.
5. Inside this directory you’ll find a file ending in .sql, this is the SQL database dump file. The next step is to dump that SQL file back into your database with the following command.
mysql -h <hostname> -u <user> -p <password> dbname < filename.sql
(Note: You’ll need to replace the above variables for your specific configuration.)
That’s it! There’s not much more to this utility than what you saw. It provides simple, reliable, and quick backups of your Ghost blog with minimal fuss. Feel free to submit a pull request to my repository if you find a bug, or would like to add a feature. I hope you’ve enjoyed this post and enjoy using ghost-backup. Please check out more of my posts as I routinely post about DevOps, Software Engineering, and Web Hosting on a routine basis.