Files
42_124/CT124_PocketBase操作手冊_20260622.md
Timmy c36922cf6f 新增 CT124 PocketBase 操作手冊
systemd 服務操作、collections/rules、升級與備份;對外 pb.lotimmy.com。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 21:52:44 +08:00

77 lines
3.4 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
# CT124 PocketBase(自架 BaaS)— 操作手冊
> 主機:CT124192.168.42.124Ubuntu 24.04 LXC | 位置:`/opt/pocketbase`
> 用途:單一 binary 的後端服務(SQLite + REST/realtime + admin UI + 靜態託管)
---
## 一、基本資訊
- 版本:**v0.39.4**(單一 binary,**非 docker**)
- 執行:systemd 服務 `pocketbase.service`(開機自啟、當掉自動重啟 `Restart=always`)
- 監聽:`192.168.42.124:18090`
- 對外:**NPM 反代 `https://pb.lotimmy.com`**(Let's Encrypt,TLS 在 NPM 終結,強制 HTTP→HTTPS)
- 後台:`https://pb.lotimmy.com/_/` | 官方文件:https://pocketbase.io/docs/
## 二、目錄
```
pocketbase 主程式 (binary)
pb_data/ 資料庫(SQLite,所有資料在此;目前 ~1.6M)
pb_public/ 對外靜態網站(放這裡的檔會掛在 https://pb.lotimmy.com/<檔名>)
pb_migrations/ 資料表結構變更紀錄
```
## 三、服務操作
```bash
systemctl status pocketbase # 狀態
systemctl restart pocketbase # 重啟
journalctl -u pocketbase -f # 看 log
```
systemd unit:`/etc/systemd/system/pocketbase.service`
`ExecStart=/opt/pocketbase/pocketbase serve --http 192.168.42.124:18090`
## 四、帳號
- 後台管理員(superuser):`loyangchun@gmail.com`(密碼見密碼管理器,勿寫入本檔)
- 重設管理員密碼:`./pocketbase superuser upsert loyangchun@gmail.com <新密碼>`
- demo 範例使用者(可重建,僅供 `demo.html` 練習):
- `demo@pb.local` / `demo12345` — role=**admin**
- `demo2@pb.local` / `demo12345` — role=**user**(只能改自己的)
## 五、Collections
- **users**(auth,內建):已開放公開註冊;登入者可見彼此 name;有 `role` 欄位(user / editor / admin)。
- **notes**(base,示範待辦):
- 欄位:`title`(text,必填)、`done`(bool)、`priority`(select: high/normal/low)、`owner`(relation→users,必填,cascadeDelete)、`created`/`updated`(autodate)
- API Rules:
- list / view:`@request.auth.id != ""`
- create:`@request.auth.id != "" && @request.body.owner = @request.auth.id`(owner 須為自己,防偽造)
- update / delete:`owner = @request.auth.id || @request.auth.role = "admin"`
## 六、demo.html
對外範例:**https://pb.lotimmy.com/demo.html**(檔案 `pb_public/demo.html`,改它即時生效)。
原生 fetch + EventSource,含登入/註冊、權限控管、搜尋篩選、行內編輯、realtime(SSE)即時同步,無外部套件。
## 七、升級流程(換 binary)
```bash
systemctl stop pocketbase
cp -a pb_data pb_data.preupgrade.$(date +%s) # 先備份資料
cp -a pocketbase pocketbase.<舊版>.bak # 備份舊 binary
curl -fsSL -o /tmp/pb.zip https://github.com/pocketbase/pocketbase/releases/download/v<版本>/pocketbase_<版本>_linux_amd64.zip
unzip -o /tmp/pb.zip pocketbase -d /opt/pocketbase
systemctl start pocketbase # 啟動時自動套用內部 migration
./pocketbase --version
```
升級後驗證資料/規則無誤再刪備份。
## 八、注意事項
- **公開註冊已開**:users collection 可自由註冊,且服務對外(`pb.lotimmy.com`)。不要的話到後台關掉 users 的 create rule。
- **備份**:整個 `pb_data/`(SQLite)即全部資料,停服務或趁低流量 `cp -a pb_data <備份>` 即可;升級流程已含。
- 對外 443 經 NPM → 依賴 docker daemon 與 NPM 容器(見 [`CT124_故障復原_20260529.md`](CT124_故障復原_20260529.md));NPM 掛了 `pb.lotimmy.com` 不通,但內網 `:18090` 仍可用(PocketBase 本身是 systemd,不隨 docker 起落)。