GHOST CMS: backup and restore ⏤ Self-hosting.

Having a copy of all website data, such as HTML, CSS, JavaScript, images, videos, and databases that store crucial information, like user data and content management system configurations guards you against potential risks and ensures the ability to restore your website in case of data loss, hacking, or other unforeseen events.

🎧



According to IBM, The global average cost of a data breach in 2023 was USD 4.45 million, a 15% increase over 3 years. This hefty cost is a lot, especially for small businesses. No matter your industry, the size of your customer base, or how often you post content to your website, having a website backup is the digital equivalent of having a spare tire in your trunk. While you hope you not to use it, there is a certain peace of mind knowing it’s available. The same is true for website backups. BACKUPS ARE CRUCIAL [period!]


Backup Your Business


Ghost doctor

Before running into the backup we suggest that you run the following command ghost doctor first under your working directory.
Running this will check the system for potential hiccups, and provide information about what needs to be resolved if any issues arise.

See below (hiccups !!!)

$ ghost doctor
...
..
.


✔ Checking system Node.js version - found v18.19.0
✔ Checking logged in user
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
✔ Checking system compatibility
✔ Checking for a MySQL installation
+ sudo systemctl is-active ghost_localhost
Instance is currently running
ℹ Validating config [skipped]
✖ Checking folder permissions
✖ Checking file permissions
✖ Checking content folder ownership
✔ Checking memory availability
✔ Checking binary dependencies
✔ Checking free space
✔ Checking systemd unit file
✔ Checking systemd node version - found v18.19.0
One or more errors occurred.

1) Checking folder permissions

Message: Ghost can't access some files or directories to check for correct permissions.
Help: Run sudo find ./ -type d -exec chmod 00775 {} \; and try again.


2) Checking file permissions

Message: Ghost can't access some files or directories to check for correct permissions.
Help: Run sudo find ./ -type d -exec chmod 00775 {} \; and try again.


3) Checking content folder ownership

Message: Ghost can't access some files or directories to check for correct permissions.
Help: Run sudo find ./ -type d -exec chmod 00775 {} \; and try again.

Debug Information:
    OS: Ubuntu, v22.04.3 LTS
    Node Version: v18.19.0
    Ghost Version: 5.76.2
    Ghost-CLI Version: 1.25.3
    Environment: production
    Command: 'ghost doctor'

Try running ghost doctor to check your system for known issues.


As you can see from the report, Ghost can't access some files or directories because of some misconfigured permissions.
In this case, we have to run the following command to get it fixed.

$ sudo find ./ -type d -exec chmod 00775 {} \;


Let's break it down a little.

sudo : we execute a command as root to level privileges.
find ./ : the find command is one of the most powerful tools in Linux, it searches for files in a directory hierarchy.
-type d : find all directories in the current working directory.
-exec {} \; : this tells find to execute a specified command on each file it locates. The {} acts as a placeholder that gets replaced with the current file name, and \; marks the end of the command. The command runs in the directory where find was originally executed. Only one instance of {} is allowed in the command. While many files may match the criteria, the actual number of command executions may be fewer, as find tries to group file names efficiently where possible.
chmod : change file mode bits.


Now, let's run the command one more time:

$ ghost doctor
...
..
.


✔ Checking system Node.js version - found v18.19.0
✔ Checking logged in user
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
✔ Checking system compatibility
✔ Checking for a MySQL installation
+ sudo systemctl is-active ghost_localhost
Instance is currently running
ℹ Validating config [skipped]
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
✔ Checking binary dependencies
✔ Checking free space
✔ Checking systemd unit file
✔ Checking systemd node version - found v18.19.0

All clear!

Now, let's run the backup command:

$ ghost backup
...
..
.
= Checking for deprecations
...
..


+ sudo systemctl is-active ghost_localhost
+ sudo mkdir -p /var/www/snub_test/backup
? Enter your Ghost administrator email address youremail@whatever.com
? Enter your Ghost administrator password [hidden]
+ sudo cp /var/www/snub_test/backup/content-from-v5.76.2-on-2024-01-29-00-59-18.json /var/www/snub_test/content/data/content-from-v5.76.2-on-2024-01-29-00-59-18.json
+ sudo cp /var/www/snub_test/backup/members-from-v5.76.2-on-2024-01-29-00-59-18.csv /var/www/snub_test/content/data/members-from-v5.76.2-on-2024-01-29-00-59-18.csv
+ sudo chown -R ghost:ghost /var/www/snub_test/content
✔ Backing up site
Backup saved to /var/www/snub_test/backup-from-[backup-name].zip


Ghost backup

The fastest way to back up your Ghost site is by using the Ghost CLI. With a single ghost backup command, it automatically generates a ZIP file containing all your site data. This step is especially important before upgrading to a major new version.
The backup ZIP file includes:

  • Your content in JSON format
  • A full member CSV export
  • All installed themes, including your current active theme
  • Images, files, and media (including video and audio)
  • A copy of routes.yaml and redirects.yaml (or redirects.json)


How to Remove Existing Content for a Clean Ghost Backup

To avoid duplicating posts and to create a fresh backup, remove all existing content through the Ghost admin interface.

Here's how you can delete content from your Ghost CMS:

  1. Log in to your Ghost admin dashboard:
    Access your Ghost site's admin dashboard by navigating to your site's URL followed by "/ghost" and logging in with your credentials.
  2. Settings:
    Go to settings (the gear or cog wheel)
  3. Navigate to Labs:
    On the Left end side, scroll down until you see Labs.
  4. Labs menu
    On the right end side, under Danger Zone, click on Delete all content.
    *This will permanently delete all posts and tags from the database, a hard reset.

Import your backup data

With your new site up and running, it's time to bring your data over.

Starting in your Ghost installation directory, extract the backup ZIP file directly into the content folder to restore your data:
sudo unzip /path/to/backup-from-[backup-name].zip -d content

zip
is a command-line utility used to compress files and directories, making them easier to transfer, store, or email.
In contrast, unzip is used to decompress those files, restoring them to their original form.

If not installed, you might see something like this:
sudo: unzip: command not found.
proceed with
sudo apt install unzip or brew install unzip for macOS users.

Next;

  1. Make sure the files have the right permissions:
    sudo chown -R ghost:ghost content
  2. Restart Ghost:
    ghost restart
  3. Import your content:
    ghost import content/data/content-from-[version#].json
    -This requires your username and password, and can also be done on the labs page in Ghost Admin.



Backing up your website might seem boring, but it's an absolute lifesaver when things go wrong. I can’t stress enough how critical backups are—they’re not just a good habit, they’re a vital part of your overall security strategy.

We hope you found these tips helpful and interesting!