Files
Enterprise-Knowledge-Base/scripts/cloud-image/cleanup.sh
T
wizardchen cdfbf05524 fix(cloud-image): make firstboot idempotent and pin image versions
Address review feedback on PR #1249:

- prepare.sh: when WEKNORA_REF looks like a version tag (v*), write the
  matching WEKNORA_VERSION into .env so docker compose pulls images that
  match the compose YAML's git ref (previously stuck on :latest).
- prepare.sh: detect docker binary path via `command -v docker` and
  template it into weknora.service (replacing hardcoded /usr/bin/docker
  that fails when docker lives in /usr/local/bin).
- firstboot.sh: write a /opt/WeKnora/.firstboot.done marker immediately
  after rewriting .env, before `docker compose up -d`. If compose fails
  mid-run, the next boot is gated by ConditionPathExists=!marker so we
  never regenerate DB_PASSWORD against an already-initialized postgres
  volume (which previously bricked the database).
- firstboot.sh: stop deleting its own unit file / script while the
  oneshot is still executing; rely on the marker + `systemctl disable`
  instead, avoiding "job failed" markings from systemd.
- firstboot.sh: use detected docker path instead of /usr/bin/docker;
  add note in credentials file that .env is the source of truth.
- weknora-firstboot.service: add ConditionPathExists=!.firstboot.done.
- cleanup.sh: scope docker volume deletion to compose project label
  (com.docker.compose.project=<name>) instead of fuzzy substring match
  that could nuke unrelated postgres/redis volumes.
- cleanup.sh: also remove .firstboot.done marker, firstboot log, and
  any leftover /root/weknora-credentials.txt so the image is clean.
- README.md: clarify how to actually disable registration (edit the
  `replace` call list in firstboot.sh, not run that command in shell).
2026-05-11 12:25:19 +08:00

85 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# cleanup.sh - 在制作云镜像前清理私密数据。
# 警告: 本脚本会删除 SSH 公钥、清空数据库与日志,最后会自动关机。
# 执行后请直接在云控制台「制作镜像 / 创建快照 / 创建 AMI」,不要再 SSH 进来。
set -euo pipefail
WEKNORA_DIR="${WEKNORA_DIR:-/opt/WeKnora}"
if [[ "${EUID}" -ne 0 ]]; then
echo "[cleanup] 请使用 sudo 或 root 运行" >&2
exit 1
fi
read -r -p "[cleanup] 该操作不可逆,确认继续? 输入 YES 继续: " ans
if [[ "${ans}" != "YES" ]]; then
echo "[cleanup] 已取消"
exit 0
fi
echo "[cleanup] 1/8 停止 WeKnora 容器"
COMPOSE_PROJECT=""
if [[ -d "${WEKNORA_DIR}" ]]; then
cd "${WEKNORA_DIR}"
# 优先用 compose ls 拿到真实 project 名 (默认是目录名小写, 如 weknora)
COMPOSE_PROJECT="$(docker compose ls --format json 2>/dev/null \
| grep -oE '"Name":"[^"]+"' | head -1 | cut -d'"' -f4 || true)"
docker compose down -v --remove-orphans || true
fi
echo "[cleanup] 2/8 清空 WeKnora 业务数据 + 首启 marker / 日志"
if [[ -d "${WEKNORA_DIR}" ]]; then
rm -rf "${WEKNORA_DIR}/data"/* "${WEKNORA_DIR}/logs"/* 2>/dev/null || true
rm -f "${WEKNORA_DIR}/.env" "${WEKNORA_DIR}/.firstboot.done"
cp "${WEKNORA_DIR}/.env.example" "${WEKNORA_DIR}/.env"
fi
rm -f /root/weknora-credentials.txt /var/log/weknora-firstboot.log
echo "[cleanup] 3/8 清理残留 docker 卷与构建缓存"
# 严格按 compose project 名前缀匹配, 避免误伤同宿主上其它 postgres/redis 卷。
if [[ -n "${COMPOSE_PROJECT}" ]]; then
docker volume ls -q --filter "label=com.docker.compose.project=${COMPOSE_PROJECT}" \
| xargs -r docker volume rm -f || true
fi
docker system prune -af --volumes || true
echo "[cleanup] 4/8 清空系统日志"
journalctl --rotate || true
journalctl --vacuum-time=1s || true
find /var/log -type f \( -name '*.log' -o -name '*.gz' -o -name '*.[0-9]' \) -print0 \
| xargs -0 -r truncate -s 0 || true
find /var/log -type f \( -name '*.gz' -o -name '*.[0-9]' \) -print0 \
| xargs -0 -r rm -f || true
echo "[cleanup] 5/8 清理 SSH 历史与授权 key(执行后将无法 SSH 进来)"
rm -f /root/.ssh/authorized_keys /root/.ssh/known_hosts /root/.bash_history
for d in /home/*; do
[[ -d "$d" ]] || continue
rm -f "$d/.ssh/authorized_keys" "$d/.ssh/known_hosts" "$d/.bash_history"
done
find / -xdev -type f \( -name 'id_rsa*' -o -name '*.pem' -o -name '*.key' \) \
-not -path '/etc/ssl/*' -not -path '/usr/*' -not -path '/var/lib/docker/*' 2>/dev/null \
| tee /tmp/cleanup-secrets-found.txt || true
echo "[cleanup] ↑ 上面是疑似遗留的密钥文件,必要时人工再核对"
echo "[cleanup] 6/8 重置 cloud-init / machine-id(让新实例拿到新 ID)"
cloud-init clean --logs --seed 2>/dev/null || true
truncate -s 0 /etc/machine-id || true
rm -f /var/lib/dbus/machine-id || true
echo "[cleanup] 7/8 清理 apt / tmp"
if command -v apt-get >/dev/null 2>&1; then
apt-get clean
rm -rf /var/lib/apt/lists/*
fi
rm -rf /tmp/* /var/tmp/* /root/.cache /home/*/.cache 2>/dev/null || true
echo "[cleanup] 8/8 同步磁盘并关机"
history -c || true
sync
echo
echo " 即将关机。关机完成后请到云控制台执行「制作镜像 / 创建快照 / 创建 AMI」。"
echo
sleep 3
poweroff