Skip to content
KD
Go back

How to Set Up Automatic Backup for your Ghost Blog

Updated:

You can back up your website data from a Ghost CMS (Content Management System) in several ways. There are two main methods to back up a Ghost website.

Data loss can happen anywhere, yet we can’t afford to lose data. We have to be prepared one way or the other. We can avoid this using our web host’s backup plans, but it’ll cost us some bucks.

We can do it ourselves for free using Google Drive as our backup host. I’ve developed an easy-to-use command-line interface to automate this task. You can find the source code on GitHub.

How it works Schedules a daily task on your blog server. Task runs daily and pushes backup archives to your Google Drive without your consent or intervention (Initial authentication required). You’ll also get daily Telegram notifications about the backup status if you want.

Setup The setup will be simple and straightforward. The whole setup process will be interactive. You just have to run the following two commands on your blog server.

git clone https://github.com/dkarthe/ghost-backup && cd ghost-backup
python3 setup.py

After executing the above two commands, you’ll be welcomed by an interactive command-line interface. There will be on-screen instructions. Still, I’ll walk you through each step.

The whole purpose of this post is to take a daily backup of your blog database. But, you can also back up your images & themes along with the DB.

It stores the backup files as .tar.gz archives inside the folder Ghost Backup. It overwrites the previous file daily. But, we can get upto last 30 revisions. To get the previous version of the backup right click the file on Google Drive and select Mange versions…

Note: Whenever you provide invalid input, the command line will ask for your input again.

If you want to back up your images, themes, or both, select Yes when prompted, then enter the absolute path to the images or themes directory.

When asked for your DB details like username, password, host, and database name, give the details correctly; otherwise, you have to rerun the setup.

Wait till the external dependencies download completion. Now, you’ll be asked to visit a URL to obtain an authorisation code.

Visit that URL and select the Google account where you want to save the backup files.

Copy the authorisation code you see in the browser & paste it into the terminal.

That’s it, Google Drive setup is done.

It is highly recommended to set up Telegram notifications as you’ll get to know about the status of daily backup or about the errors that occur in the process.

Backup file Name format
Example: `20180115172329.tar.gz`
1-4 digits `2018` -> Year
5-6 digits `01` -> Month
7-8 digits `15` -> Date
From 9th digit `172329` -> Time, represents `7:23:29

When will the backup happen Backup task will be scheduled to run at midnight (i.e) 12 in the night. Timezone will be of your blog server. To change the timezone or to change the time at which the task runs daily you can run the following command.

sudo dpkg-reconfigure tzdata

After changing your timezone, you have to restart cron to make the changes take effect.

sudo service cron restart

How to Restore Backup

Download the archive from your Google Drive. Use scp to transfer the files from your local machine to server. To unpack the archive use the below command.

tar -xvzf filename.tar.gz

Finally, dump the .sql file to your database.

mysql -hhostname -uroot -ppassword dbname < filename.sql

Replace the hostname, root, password, dbname with what is appropriate for you.

Troubleshooting This section will deal with the possible issues you’ll face during setup or backup process.

Permission denied error Sometimes, the logged-in user will not have access to copy the script files to the /opt directory. In that case, run the following command to eliminate the issue. This happens during setup.

sudo setfacl -R -m u:$(whoami):rwx /opt

setfacl: command not found If you get the above error, install acl utility.

sudo apt install acl

If you set up image or theme backups and don’t have write access to the images or themes directory, you’ll get an exception when the backup script runs. In that case, run the following command.

sudo setfacl -R -m u:$(whoami):rwx /var/www/ghost/content

Replace the marked path with yours if you have a different installation path. Permission denied is the most common error during setup. If you get this error at any point during the process, replace the marked path with the path reported in the error.

Privacy Policy

The app ‘Ghost Blog Backup’ is free to use, and it doesn’t collect, use, or share any user info. However, the authorisation code is obtained from you solely to authenticate your access to Google Drive. All the required tokens are stored on your blog server.


Share this post on:

Next Post
How to Recover AWS EC2 Instance Without Key Pair (Manual + Automated DevOps Guide)