# 备份与恢复(Ubuntu 服务器)

本文档面向 **VPS / Ubuntu**,项目统一放在 **`/opt/crypto_monitor`**,数据备份统一放在 **`/root/backups`**.

| 类型 | 内容 | 存放位置 | 频率 |
|------|------|----------|------|
| **数据库 + 复盘图片** | `crypto.db`,`static/images` | `/root/backups/<实例名>/YYYY-MM-DD/` | 每天北京时间 **0:00**(cron) |
| **`.env` 配置** | API,密码,风控参数等 | 项目目录 `.env.backup.日期`;可选集中拷到 `/root/backups/env/` | **升级 / 改配置前**手动执行 |

> `.env` **不会**被自动备份脚本包含(含密钥,请单独备份).  
> 三个常用实例:`crypto_monitor_binance`,`crypto_monitor_gate`,`crypto_monitor_okx`.

---

## 一,首次安装:三个实例自动备份 + 试跑

整段复制到 SSH 终端执行(需 **root** 或对该目录有写权限):

```bash
apt install -y sqlite3 2>/dev/null || true

for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  cd "/opt/crypto_monitor/${dir}" || exit 1
  chmod +x scripts/backup_data.sh scripts/install_backup_cron.sh
  bash scripts/install_backup_cron.sh
  bash scripts/backup_data.sh
done

echo "=== crontab ==="
crontab -l
echo "=== backup dirs ==="
ls -la /root/backups/*/
```

成功后应有:

- `crontab -l` 含一行 `CRON_TZ=Asia/Shanghai` + 三条 `0 0 * * * .../backup_data.sh`
- `/root/backups/crypto_monitor_binance/2026-05-17/`(日期为当天)等目录,内含 `crypto.db`,`static_images.tar.gz`,`manifest.txt`

日志路径:

- `/var/log/crypto-monitor-backup-crypto_monitor_binance.log`
- `/var/log/crypto-monitor-backup-crypto_monitor_gate.log`
- `/var/log/crypto-monitor-backup-crypto_monitor_okx.log`

---

## 二,仅安装某一个实例的自动备份

把 `INSTANCE` 改成目录名后整段执行:

```bash
INSTANCE=crypto_monitor_binance
cd "/opt/crypto_monitor/${INSTANCE}"
chmod +x scripts/backup_data.sh scripts/install_backup_cron.sh
bash scripts/install_backup_cron.sh
bash scripts/backup_data.sh
```

`INSTANCE` 可选:`crypto_monitor_binance` | `crypto_monitor_gate` | `crypto_monitor_okx`

---

## 三,手动立即备份(数据库 + 图片,三个实例)

不等到 0 点,立刻各备份一次:

```bash
for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  echo ">>> ${dir}"
  bash "/opt/crypto_monitor/${dir}/scripts/backup_data.sh"
done
ls -la /root/backups/*/*/
```

---

## 四,检查定时任务与备份是否正常

```bash
crontab -l
ls -la /root/backups/*/
du -sh /root/backups/*/
tail -n 20 /var/log/crypto-monitor-backup-crypto_monitor_binance.log
tail -n 20 /var/log/crypto-monitor-backup-crypto_monitor_gate.log
tail -n 20 /var/log/crypto-monitor-backup-crypto_monitor_okx.log
```

---

## 五,`.env` 备份(升级 / git pull / 改密钥前)

### 5.1 三个实例一次性备份到各自项目目录

```bash
DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  src="/opt/crypto_monitor/${dir}/.env"
  dst="/opt/crypto_monitor/${dir}/.env.backup.${DATE}"
  if [ -f "$src" ]; then
    cp -a "$src" "$dst"
    echo "ok: $dst"
  else
    echo "skip (no .env): $src"
  fi
done
```

### 5.2 同时集中备份到 `/root/backups/env/`(推荐)

```bash
DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
mkdir -p /root/backups/env
for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  src="/opt/crypto_monitor/${dir}/.env"
  if [ -f "$src" ]; then
    cp -a "$src" "/root/backups/env/${dir}.env.${DATE}"
    echo "ok: /root/backups/env/${dir}.env.${DATE}"
  fi
done
ls -la /root/backups/env/
```

> `/root/backups/env/` 含密钥,勿上传网盘,勿提交 Git.

---

## 六,`.env` 恢复

### 6.1 从项目目录内的备份恢复

把 `INSTANCE` 和 `DATE` 改成实际值(`DATE` 为备份当天的 `YYYYMMDD`):

```bash
INSTANCE=crypto_monitor_binance
DATE=20260517
cd "/opt/crypto_monitor/${INSTANCE}"
cp -a ".env.backup.${DATE}" .env
echo "restored .env from .env.backup.${DATE}"
```

### 6.2 从 `/root/backups/env/` 恢复

```bash
INSTANCE=crypto_monitor_binance
DATE=20260517
cp -a "/root/backups/env/${INSTANCE}.env.${DATE}" "/opt/crypto_monitor/${INSTANCE}/.env"
echo "restored from /root/backups/env/${INSTANCE}.env.${DATE}"
```

恢复后重启对应 PM2 进程,例如:

```bash
pm2 restart crypto-monitor-binance
pm2 restart crypto-monitor-gate
```

(进程名以你 `pm2 list` 为准.)

---

## 七,数据库 + 复盘图片恢复

从自动备份目录恢复.先停服务再覆盖,避免 SQLite 写入冲突.

把 `INSTANCE`,`DATE`(文件夹名 `YYYY-MM-DD`)改成实际值:

```bash
INSTANCE=crypto_monitor_binance
DATE=2026-05-17
BK="/root/backups/${INSTANCE}/${DATE}"
PROJ="/opt/crypto_monitor/${INSTANCE}"

test -f "${BK}/crypto.db" || { echo "backup not found: ${BK}"; exit 1; }

pm2 stop crypto-monitor-binance 2>/dev/null || true

cp -a "${PROJ}/crypto.db" "${PROJ}/crypto.db.before_restore.$(date +%Y%m%d%H%M)" 2>/dev/null || true
cp -a "${BK}/crypto.db" "${PROJ}/crypto.db"

if [ -f "${BK}/static_images.tar.gz" ]; then
  tar -xzf "${BK}/static_images.tar.gz" -C "${PROJ}"
fi

pm2 start crypto-monitor-binance 2>/dev/null || true
echo "restored ${INSTANCE} from ${BK}"
```

Gate / 将 `INSTANCE`,`pm2` 名称改为对应实例即可.

---

## 八,升级代码推荐顺序(含备份)

```bash
DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
mkdir -p /root/backups/env

for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  PROJ="/opt/crypto_monitor/${dir}"
  [ -f "${PROJ}/.env" ] && cp -a "${PROJ}/.env" "/root/backups/env/${dir}.env.${DATE}"
  bash "${PROJ}/scripts/backup_data.sh" 2>/dev/null || true
done

cd /opt/crypto_monitor
git pull

for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  echo ">>> merge .env.example if needed: ${dir}"
  diff -u "${dir}/.env.example" "${dir}/.env" | head -30 || true
done

pm2 restart all
```

`git pull` 后对照各目录 **`.env.example`**,把**新增变量名**手动补进 `.env`(不会自动合并).

---

## 九,备份目录结构说明

```text
/root/backups/
  env/                                    # .env 集中备份(手动)
    crypto_monitor_binance.env.20260517
    crypto_monitor_gate.env.20260517
    crypto_monitor_gate.env.20260517
  crypto_monitor_binance/
    2026-05-17/
      crypto.db
      static_images.tar.gz
      manifest.txt
  crypto_monitor_gate/
    2026-05-17/
      ...
  crypto_monitor_gate/
    2026-05-17/
      ...
```

- **保留策略**:自动备份目录按日期文件夹保留 **30 天**,超期在下次 `backup_data.sh` 运行时删除.
- **可选 `.env` 变量**(写在各实例 `.env` 中):`BACKUP_ROOT`,`BACKUP_RETENTION_DAYS`,`BACKUP_INSTANCE`(见各目录 `.env.example` 注释).

---

## 十,卸载自动备份定时任务

仅删除三个实例的 backup 行(保留其它 cron):

```bash
for dir in crypto_monitor_binance crypto_monitor_gate crypto_monitor_okx; do
  SCRIPT="/opt/crypto_monitor/${dir}/scripts/backup_data.sh"
  crontab -l 2>/dev/null | grep -vF "$SCRIPT" | crontab -
done
crontab -l
```

---

## 十一,相关文档

| 文档 | 说明 |
|------|------|
| [README.md](./README.md) | 仓库总览 |
| [crypto_monitor_binance/部署文档.md](./crypto_monitor_binance/部署文档.md) | Binance 部署与备份细节 |
| [crypto_monitor_gate/部署文档.md](./crypto_monitor_gate/部署文档.md) | Gate 部署 |
| [crypto_monitor_gate/部署文档.md](./crypto_monitor_gate/部署文档.md) | Gate 部署 |
