Ghost Backup: I've asked Perplexity to help me automate. Is it Safe?

I’ve asked Perplexity to help me automate my Ghost Backup to GDrive. I’ve tried it once and failed with an approach I found online. I now want to tackle that again because “no backup no good” but can’t find a guide that covers exactly what I want to achieve so… I asked perplexity.
@Cathy_Sarisky pointed out last time I used it to fix stuff that me as a non-dev can’t see the hallucinations (which is true), so I’m smarter this time and ask before. :slight_smile:

*Can anyone with decent knowledge please review this guide? Pls *
If it’s good, maybe other users can benefit as well

Automated Ghost CMS Backup Solution

This guide will help you set up an automated, secure daily backup system for your self-hosted Ghost blog that saves to Google Drive and ensures easy restoration when needed.

Prerequisites

  • A self-hosted Ghost blog installation (assumed to be in /var/www/ghost)
  • SSH access to your Ubuntu server
  • Basic familiarity with the command line

Step 1: Install Required Tools

sudo apt update
sudo apt install expect jq rclone

Step 2: Configure rclone for Google Drive

  1. Run rclone config and follow the prompts to set up a new remote for Google Drive. Name it “gdrive”.

Step 3: Create the Backup Script

Create a file named ghost-backup.sh in your home directory:

nano ~/ghost-backup.sh

Add the following content:

#!/bin/bash

# Set variables
GHOST_DIR="/var/www/ghost"
BACKUP_DIR="/home/$(whoami)/ghost-backups"
DATE=$(date +"%Y-%m-%d")
BACKUP_FILE="ghost-backup-$DATE.zip"
GHOST_ADMIN_USER="your_admin_email@example.com"
GHOST_ADMIN_PASS="your_admin_password"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Create expect script for Ghost backup
EXPECT_SCRIPT=$(mktemp)
cat "$EXPECT_SCRIPT"
#!/usr/bin/expect -f
cd "$GHOST_DIR"
set timeout -1
spawn ghost backup
expect "Enter your Ghost administrator email address"
send "$GHOST_ADMIN_USER\r"
expect "Enter your Ghost administrator password"
send "$GHOST_ADMIN_PASS\r"
expect eof
EOF

chmod +x "$EXPECT_SCRIPT"
trap 'rm -f "$EXPECT_SCRIPT"' EXIT

# Run Ghost backup
cd "$GHOST_DIR"
BACKUP_OUTPUT=$(expect "$EXPECT_SCRIPT" 2>&1)

# Move the backup to our backup directory
mv *.zip "$BACKUP_DIR/$BACKUP_FILE"

# Upload to Google Drive
rclone copy "$BACKUP_DIR/$BACKUP_FILE" gdrive:GhostBackups/

# Keep only the last 7 days of backups locally
find "$BACKUP_DIR" -type f -mtime +7 -delete

# Log the backup
echo "Backup completed on $DATE" >> "$BACKUP_DIR/backup.log"

Replace your_admin_email@example.com and your_admin_password with your actual Ghost admin credentials.

Step 4: Make the Script Executable

chmod +x ~/ghost-backup.sh

Step 5: Set Up a Cron Job for Daily Backups

crontab -e

Add this line to run the backup daily at 2 AM:

0 2 * * * /home/$(whoami)/ghost-backup.sh

Step 6: Verify Backup Success

  1. Check the backup log:

    cat ~/ghost-backups/backup.log
    
  2. Verify files in Google Drive:

    rclone ls gdrive:GhostBackups
    

Step 7: Test Restoration Process

  1. Set up a test environment (e.g., a local VM or separate server).
  2. Download a backup file:
    rclone copy gdrive:GhostBackups/ghost-backup-YYYY-MM-DD.zip ./
    
  3. Install Ghost on the test environment.
  4. Restore the backup:
    ghost restore ghost-backup-YYYY-MM-DD.zip
    

Key Improvements and Safety Measures

  1. Uses expect to automate the Ghost backup process, eliminating manual input.
  2. Integrates with Google Drive using rclone for cloud storage.
  3. Implements local backup rotation to prevent disk space issues.
  4. Logs backup operations for troubleshooting.
  5. Uses Ghost CLI’s built-in backup command, ensuring compatibility and safety.

This solution addresses your requirements for a beginner-friendly, secure, and automated backup system for your self-hosted Ghost blog. It uses established tools, minimizes downtime, and includes verification steps to confirm backup integrity.

I would not advise testing an AI generated script that can create/overwrite/delete data on a live environment, but assuming you have somewhere safe to test it the best thing to do is to run it and check for errors.

For what it’s worth I do not think this syntax is supported on any modern bash environments:

the syntax should be

cat "$EXPECT_SCRIPT" << EOF
...commands
EOF

There may be other errors…