Files
yunzerwebsiteallinone/go/docs/服务器增量部署说明.md
T
2026-10-31 19:17:37 +08:00

95 lines
4.3 KiB
Markdown
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.
# 服务器增量部署说明(update-server.bat)
本地 `go/` 目录改动后,只把**改动过的文件**推到服务器,不用再一个个找文件替换。
## 一、用法
在 `go/` 目录下双击 `update-server.bat`,或命令行执行:
```bat
update-server.bat :: 比对 -> 确认 -> 上传 -> 询问是否重启服务
update-server.bat dry :: 只比对不上传(先看清单用这个)
update-server.bat restart :: 只重启服务器服务
```
bat 会在 `go/` 目录下自动编译部署工具 `bin/deploy.exe`,然后:
1. SSH 连上服务器,对 `go/` 目录和服务器目录**各算一遍文件 MD5**
2. 比对出「新增」和「内容变了」的文件并打印清单
3. 确认后把这些文件打包成 tar 通过 SSH 管道传到服务器解包(只传差异文件)
4. 回读服务器指纹复核,确认每个文件都传对了
5. 询问是否 `systemctl restart go-api.service` 让改动生效
## 二、配置
bat 顶部「CONFIG」区:
| 变量 | 说明 |
| --- | --- |
| `DEPLOY_HOST` | 服务器地址 |
| `DEPLOY_USER` | SSH 用户名 |
| `DEPLOY_DIR` | 服务器目录 |
| `DEPLOY_KEY` | 私钥路径(默认 `%~dp010.31.100.2_id_ed25519`) |
| `DEPLOY_PASS` | 密码(备用,服务器开了密码认证时才用) |
| `RESTART_CMD` | 重启命令,默认 `systemctl restart go-api.service` |
> **注意**:这台服务器 sshd 关闭了密码认证(只接受 publickey / gssapi),所以默认走**密钥登录**。
> 如果以后在服务器上开了密码认证,把 `DEPLOY_KEY` 置空、填上 `DEPLOY_PASS` 即可,工具会自动切换。
### 关于私钥
- 私钥放在 `go/10.31.100.2_id_ed25519`,**已被同步工具自动排除**,不会传到服务器。
- Windows 上私钥权限过宽会让 OpenSSH 客户端拒绝加载,已用下面的命令收紧过(只需做一次):
```bat
icacls "10.31.100.2_id_ed25519" /inheritance:r /grant:r "%USERNAME%:(F)"
```
- `go/.deploy_known_hosts` 记录服务器主机密钥指纹,换服务器时会提示指纹变化(同样被排除,不会上传)。
## 三、不会同步的内容
工具默认跳过:`.git` / `node_modules` / `bin` / `dist` / `tmp`、`*.exe` / `*.log` / `*.bak`、
`nohup.out`、`output.log`、`go.log`、`test-output-*.txt`、`migrate_*.csv`、
无扩展名的 `server` 二进制、`*_id_ed25519` 私钥、`.deploy_known_hosts`。
服务器上存在、本地没有的文件**默认保留不删**(服务器上还有 `.git`、历史二进制等)。
确实要清理就在 bat 里加 `-delete`,但请先想清楚。
## 四、原理
```
本地 go/ ──MD5──┐
├─ 比对 ──> 只传 新增/变更 的文件 ──> 服务器 /www/wwwroot/api.yunzer.cn
服务器 ──MD5──┘ └── 传完回读远端指纹复核
```
- 服务器是 systemd 的 `go-api.service`,`ExecStart=/usr/local/btgo/bin/go run main.go`,
**直接用目录里的源码编译运行**,所以传 `.go` 源码即可,重启服务就会重新编译生效。
- 传到服务器的文件属主会被改成目标目录的属主(`www:www`),权限规范成 0644(脚本 0755)。
## 五、常见问题
| 现象 | 处理 |
| --- | --- |
| `Permission denied (publickey,gssapi-...)` | 私钥没配好:检查 `DEPLOY_KEY` 路径,或用 icacls 收紧权限 |
| `本地目录不存在` | 别从别处调用 bat,bat 里用 `%~dp0` 定位自身目录 |
| 上传后服务没生效 | 需要重启:`update-server.bat restart`(或看 go.log 是否有编译错误) |
| 清单里出现奇怪的文件 | 用配置区的 `EXTRA_EXCLUDE` 排除,逗号分隔、支持 `*` |
| 远端提示 md5sum 不存在 | 极老系统没 md5sum,用 `cksum` 也能工作,可改工具里的命令 |
## 六、单独使用部署工具
bat 之外的进阶用法(在 `go/` 目录下):
```bat
bin\deploy.exe -host 10.31.100.2 -user root -key 10.31.100.2_id_ed25519 ^
-dir /www/wwwroot/api.yunzer.cn -local . -dry-run :: 只看差异
bin\deploy.exe -host 10.31.100.2 -user root -key 10.31.100.2_id_ed25519 ^
-exec "systemctl status go-api.service" :: 执行远程命令
```
常用参数:`-dry-run` 只比对、`-yes` 跳过确认、`-delete` 同步删除远端多余文件、
`-restart "命令"` 上传后自动重启、`-exclude "a,b"` 额外排除。