#!/usr/bin/env bash # ============================================================ # Restore from Git: Khoi phuc toan bo he thong tren VPS moi # ============================================================ set -euo pipefail REPO_DIR="/opt/ai-os/products/ceo" BACKUP_DIR="${REPO_DIR}/backups" SQL_DIR="${BACKUP_DIR}/sql" echo "=== BẮT ĐẦU QUY TRÌNH KHÔI PHỤC CEO AI OS TRÊN VPS MỚI ===" # 1. Yêu cầu nhập mật khẩu giải mã if [ -z "${ENC_PASS:-}" ]; then echo "Gợi ý mật mã: ngày gặp nhau (DDMMYY)" read -rsp "Nhập mật khẩu giải mã (6 chữ số): " ENC_PASS echo "" fi # 2. Khôi phục .env if [ -f "${REPO_DIR}/.env.enc" ]; then echo "[-] Đang giải mã .env..." mkdir -p /root/.hermes openssl enc -d -aes-256-cbc -pbkdf2 -iter 100000 \ -in "${REPO_DIR}/.env.enc" \ -out "/root/.hermes/.env" \ -pass pass:"$ENC_PASS" chmod 600 "/root/.hermes/.env" ln -sf "/root/.hermes/.env" "${REPO_DIR}/.env" echo "[✓] Đã khôi phục /root/.hermes/.env và symlink .env!" else echo "[!] Không tìm thấy .env.enc!" fi # 3. Khôi phục Secrets & Tokens if [ -f "${BACKUP_DIR}/secrets.enc" ]; then echo "[-] Đang giải mã các tokens và cấu hình Hermes..." TMP_TAR="/tmp/hermes_secrets_restore.tar.gz" openssl enc -d -aes-256-cbc -pbkdf2 -iter 100000 \ -in "${BACKUP_DIR}/secrets.enc" \ -out "$TMP_TAR" \ -pass pass:"$ENC_PASS" tar -xzf "$TMP_TAR" -C "/root/.hermes" rm -f "$TMP_TAR" echo "[✓] Đã khôi phục google_token, auth.json, config.yaml!" fi # 4. Khôi phục cơ sở dữ liệu Kanban if [ -d "$SQL_DIR" ]; then echo "[-] Đang nạp lại dữ liệu Kanban từ các bản SQL dump..." mkdir -p /root/.hermes/kanban/boards for sql_file in "${SQL_DIR}"/kanban_*.sql; do if [ -f "$sql_file" ]; then bname=$(basename "$sql_file" | sed 's/^kanban_//; s/\.sql$//') target_dir="/root/.hermes/kanban/boards/${bname}" mkdir -p "$target_dir" rm -f "${target_dir}/kanban.db" sqlite3 "${target_dir}/kanban.db" < "$sql_file" echo " -> Đã khôi phục board: ${bname}" fi done echo "[✓] Khôi phục toàn bộ Kanban boards thành công!" fi # 5. Khôi phục cronjob và memories if [ -f "${BACKUP_DIR}/cron_jobs.json" ]; then mkdir -p /root/.hermes/cron cp -f "${BACKUP_DIR}/cron_jobs.json" "/root/.hermes/cron/jobs.json" echo "[✓] Đã khôi phục danh sách cronjob!" fi if [ -d "${REPO_DIR}/memories" ]; then mkdir -p /root/.hermes/memories cp -f "${REPO_DIR}/memories"/*.md /root/.hermes/memories/ 2>/dev/null || true echo "[✓] Đã khôi phục trí nhớ Hermes (MEMORY.md, USER.md)!" fi # 6. Thiết lập symlink SOUL.md nếu thiếu if [ -f "${REPO_DIR}/SOUL.md" ] && [ ! -L "/root/.hermes/SOUL.md" ]; then ln -sf "${REPO_DIR}/SOUL.md" "/root/.hermes/SOUL.md" echo "[✓] Đã kết nối symlink SOUL.md!" fi echo "=== HOÀN TẤT KHÔI PHỤC 100% NGUYÊN TRẠNG DỮ LIỆU VÀ CẤU HÌNH ==="