> ## Content Index
> Fetch the complete content index at: https://taiwanding.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Ghost CMS 自動備份：Cron + Rclone 上傳 Google Drive 完整教學
- URL: https://taiwanding.com/ghost-cms-zi-dong-bei-fen-cron-rclone-shang-chuan-google-drive-wan-zheng-jiao-xue/
- Published: 2026-03-01T12:15:41.000Z
- Updated: 2026-07-15T02:50:27.000Z
- Author: Kevin Chen
- Tags: #google cloud, vps, #ghost cms

## 前言

上一篇把 Ghost CMS 從 KVM 4 搬到 KVM 2 之後，馬上想到一個問題：**備份怎麼辦？**

Hostinger 的 Daily Backup 服務要額外加 $6/月，24 個月方案下來就是 $144，為了省錢搬家結果又花錢買備份，本末倒置了，所以決定自己寫 script，用 cron 定時備份 + rclone 同步到 Google Drive，零成本搞定(其實有雲端成本\~\~)。

## 備份策略

| 項目              | 設定                                     |
| --------------- | -------------------------------------- |
| 備份頻率            | 每 12 小時                                |
| 本地保留            | 7 天                                    |
| Google Drive 保留 | 90 天                                   |
| 備份內容            | MySQL 資料庫 + Ghost content 資料夾 + config |
| 單次備份大小          | 約 160MB（DB 15MB + Content 145MB）       |

## Step 1：建立備份目錄

```bash
sudo mkdir -p /var/backups/ghost
sudo chown youruser:youruser /var/backups/ghost

```

## Step 2：撰寫備份 Script

```bash
cat > ~/ghost-backup.sh << 'EOF'
#!/bin/bash
# Ghost CMS Auto Backup Script
# 本地保留 7 天，Google Drive 保留 90 天

BACKUP_DIR="/var/backups/ghost"
DATE=$(date +%Y%m%d_%H%M%S)
GHOST_DIR="/var/www/ghost"

echo "[$(date)] Starting Ghost backup..."

# 備份資料庫
mysqldump --no-tablespaces -u ghost_user -p'你的密碼' ghost_db > "$BACKUP_DIR/ghost-db-$DATE.sql"

# 備份 content 資料夾（含主題、圖片、設定檔）
sudo tar czf "$BACKUP_DIR/ghost-content-$DATE.tar.gz" -C "$GHOST_DIR" content/ config.production.json

# 刪除本地 7 天前的備份
find "$BACKUP_DIR" -name "ghost-*" -mtime +7 -delete

echo "[$(date)] Backup complete: $BACKUP_DIR/ghost-db-$DATE.sql + ghost-content-$DATE.tar.gz"

# 上傳到 Google Drive
rclone copy "$BACKUP_DIR/ghost-db-$DATE.sql" gdrive:ghost-backups/
rclone copy "$BACKUP_DIR/ghost-content-$DATE.tar.gz" gdrive:ghost-backups/

# 刪除 Google Drive 上 90 天前的備份
rclone delete gdrive:ghost-backups/ --min-age 90d

echo "[$(date)] Uploaded to Google Drive"
EOF

chmod +x ~/ghost-backup.sh

```

## Step 3：解決 Content 資料夾權限問題

Ghost 的 content 資料夾擁有者是 `ghost:ghost`，一般用戶 tar 會遇到 `Permission denied`。設定免密碼 sudo tar：

```bash
echo "youruser ALL=(ALL) NOPASSWD: /usr/bin/tar" | sudo tee /etc/sudoers.d/ghost-backup

```

這樣 script 裡的 `sudo tar` 就不會卡在密碼輸入了。

## Step 4：安裝與設定 Rclone

### 安裝 Rclone

```bash
sudo apt install -y rclone

```

如果 apt 裝不到，用官方安裝腳本：

```bash
curl https://rclone.org/install.sh | sudo bash

```

### 設定 Google Drive 連線

VPS 沒有瀏覽器，需要用 headless 模式授權：

```bash
rclone config

```

依序操作：

1. `n`（New remote）
2. Name 輸入 `gdrive`
3. Storage 選 `drive`（Google Drive）
4. client\_id → 直接 Enter（用內建 key）
5. client\_secret → 直接 Enter
6. scope → 選 `1`（Full access）
7. service\_account\_file → Enter 跳過
8. Edit advanced config → `n`
9. Use auto config → **`n`**（重要！VPS 要選 No）

此時會出現一串授權指令：

```
rclone authorize "drive" "eyJzY29wZSI6ImRyaXZlIn0"

```

### 在本地機器完成授權

在有瀏覽器的電腦（你的 Kali）上執行上面那串指令：

```bash
# 先安裝 rclone（如果還沒裝）
curl https://rclone.org/install.sh | sudo bash

# 執行授權
rclone authorize "drive" "eyJzY29wZSI6ImRyaXZlIn0"

```

瀏覽器會自動打開 Google 登入頁面，授權完成後終端會輸出一串 token。把整串 token 複製貼回 VPS 的 `config_token>` 提示。

最後：

- Configure as Shared Drive → `n`
- Keep this remote → `y`
- `q` 退出

### 測試連線

```bash
# 在 Google Drive 上建立備份資料夾
rclone mkdir gdrive:ghost-backups

# 確認資料夾存在
rclone lsd gdrive:

```

## Step 5：設定 Cron 排程

每 12 小時自動執行備份，log 輸出到 `/var/log/ghost-backup.log`：

```bash
(crontab -l 2>/dev/null; echo "0 */12 * * * /home/youruser/ghost-backup.sh >> /var/log/ghost-backup.log 2>&1") | crontab -

```

確認 cron 有加成功：

```bash
crontab -l
# 應該看到：
# 0 */12 * * * /home/youruser/ghost-backup.sh >> /var/log/ghost-backup.log 2>&1

```

## Step 6：完整測試

```bash
bash ~/ghost-backup.sh

```

預期輸出：

```
[Sun Mar  1 11:57:06 UTC 2026] Starting Ghost backup...
[Sun Mar  1 11:57:12 UTC 2026] Backup complete: /var/backups/ghost/ghost-db-20260301_115706.sql + ghost-content-20260301_115706.tar.gz
[Sun Mar  1 11:57:31 UTC 2026] Uploaded to Google Drive

```

確認 Google Drive 上有檔案：

```bash
rclone ls gdrive:ghost-backups/
# 151230801 ghost-content-20260301_115706.tar.gz
#  15515331 ghost-db-20260301_115706.sql

```

## 還原步驟（以備不時之需）

哪天真的需要還原，從 Google Drive 拉回來就好：

```bash
# 列出可用備份
rclone ls gdrive:ghost-backups/

# 下載指定日期的備份
rclone copy gdrive:ghost-backups/ghost-db-20260301_115706.sql /tmp/
rclone copy gdrive:ghost-backups/ghost-content-20260301_115706.tar.gz /tmp/

# 還原資料庫
mysql -u ghost_user -p ghost_db < /tmp/ghost-db-20260301_115706.sql

# 還原 content
cd /var/www/ghost
sudo tar xzf /tmp/ghost-content-20260301_115706.tar.gz

# 修正權限
sudo chown -R ghost:ghost /var/www/ghost/content

# 重啟 Ghost
ghost restart

```

## 成本比較

| 方案                              | 兩年成本                 |
| ------------------------------- | -------------------- |
| Hostinger Daily Backup          | $6/月 × 24 = **$144** |
| 自建 Cron + Rclone + Google Drive | **$0**               |

同樣的效果，省了 $144，而且備份頻率（12 小時）比 Hostinger 的 Daily Backup 還高，Google Drive 免費 15GB 空間，以每次 160MB 來算，90 天的備份量約 **28.8GB**... 呃，其實會超過 15GB。

> ⚠️ **空間試算**：160MB × 2 次/天 × 90 天 = 28.8GB，超過 Google Drive 免費 15GB。實際上因為每 12 小時備份一次，90 天後開始刪除舊的，穩態大約會維持在 28.8GB 左右，如果空間不夠，可以把 Google Drive 保留天數降到 **45 天**（約 14.4GB），或升級 Google One 100GB（$1.99/月）。

## 結語

自己寫備份比想像中簡單，整個設定大概 10 分鐘就搞定。重點是：

1. **mysqldump** 備份資料庫
2. **tar** 打包 content 和 config
3. **rclone** 丟上 Google Drive
4. **cron** 定時執行
5. **find / rclone delete** 自動清理舊備份

下次 VPS 出問題，15 分鐘內就能還原，安心。