How to Automatically Take Ghost Backups on GCP?

No matter how small your Ghost website is, it surely deserves backups.

How to Automatically Take Ghost Backups on GCP?

No matter how small your Ghost website is, it surely deserves backups because all the articles you have written on it are the diamonds of your effort. On Ghost 3.40.5, you can export (or take a backup of) your Ghost contents in JSON so that you can import it later. In this post, I will explain how I automated the daily backup process.

The deployment diagram of the system. Check out the git repository of backup Cloud Function.

I decided to use a series of managed services provided by Google Cloud Platform. Although I could have written a cron job in the same machine as the web server, there are mainly two reasons why I chose to use cloud services. The first reason is observability. As with any system, I would like to ensure that everything is working fine. Thanks to Error Reporting, I can get an email in case something went wrong. That includes when the VM runs out of its resources and stalls. The second reason is compatibility. If I chose to write a cron job, I would have to do the same thing whenever I move the web server to another VM. However, I can move the web server even to a managed environment without cron because my system calls Ghost's export API from outside the web server; the only thing I have to do is change the URL to that of the new web server.

I set Cloud Scheduler to send a message to a topic of Cloud Pub/Sub at 0:00 JST. That message pulls the trigger of the backup Cloud Function, which subscribes to the topic. The Cloud Function reads the environment variables first and gets the Ghost user's password from Secret Manager. It then authenticates to Ghost with the user's email address and password to request the backup. As the final step, Cloud Function stores the backup file to the Cloud Storage bucket. I configured the bucket's lifecycle to automatically discard any files older than ten days limit the bucket size hence the cost.

Please check the git repository of backup Cloud Function. Last but least, ensure that you have given enough permissions to the service account which runs the Cloud Function. Happy hacking:)