Files
Obsidian/工具相关/Gitea部署与Obsidian自动备份.md
T

201 lines
6.1 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.
# Gitea 部署与 Obsidian 自动备份
> 完成时间:2026-11-05
> 服务器:腾讯云新加坡 43.156.118.130(Ubuntu 24.04)
> 用途:私人代码仓库 + Obsidian 笔记自动备份,替代被封的 GitHub 账号
---
## 一、Gitea 是什么
自己服务器上的私人 GitHub:存代码、记版本、能回滚。开源免费(MIT 协议),全部功能不收费。数据在自己手里,没人能封。
---
## 二、部署(Docker,一条命令)
> 注意:NewAPI 已占用 3000 端口,Gitea 改走 **3001**。
SSH 上服务器(`ssh ubuntu@43.156.118.130`,再 `sudo -i`),跑:
```bash
mkdir -p /opt/gitea
docker run -d \
--name gitea \
-p 127.0.0.1:3001:3000 \
-v /opt/gitea:/data \
--restart unless-stopped \
gitea/gitea:latest
```
检查:
```bash
docker ps --filter 'name=gitea' --format '{{.Status}} {{.Ports}}'
```
看到 `Up` 和 `127.0.0.1:3001->3000` 即成功。
**现状**:容器名 `gitea`,数据卷 `/opt/gitea`,数据库 SQLite(默认),开机自启,管理员账号 `admin`。
---
## 三、访问方式:SSH 隧道
Gitea 只绑了 `127.0.0.1`,必须走隧道访问,浏览器开 `http://localhost:3001`。
`tunnel.bat`(位于 `F:\AI\docker\cpa-deploy\tunnel.bat`)已加入 3001 转发,现有 7 个端口:
| 端口 | 用途 |
| --- | --- |
| 8317 | CPA 主 API + 管理面板 |
| 8085 / 1455 / 54545 / 51121 / 11451 | Google OAuth 授权回调端口 |
| **3001** | **Gitea** |
双击 tunnel.bat,保持窗口开着。单独开隧道时用:
```powershell
ssh -N -L 3001:127.0.0.1:3001 ubuntu@43.156.118.130
```
---
## 四、初始化(只做一次)
浏览器开 `http://localhost:3001`,安装页保持默认(数据库 SQLite3),往下翻到**管理员账号**,填用户名、密码、邮箱,点**安装 Gitea**,跳转到登录页即完成。
---
## 五、推代码:第一次踩过的两个坑
### 坑 1:Git 没设用户名,提交失败
现象:`git commit` 后提示 `nothing added to commit but untracked files present`,随后 `git push` 报 `error: src refspec main does not match any`。
原因:新电脑 Git 没配身份,提交被拦,根本没产生提交。
解决(先设身份再提交):
```powershell
git config --global user.name "admin"
git config --global user.email "beyond1008@gmail.com"
git add .
git commit -m "first commit"
```
### 坑 2:本地分支不叫 main
现象:`git push -u origin main` 报 `error: src refspec main does not match any`(提交已成功时)。
原因:本地默认分支是 `master`,不是 `main`。
解决(改名再推):
```powershell
git branch -M main
git push -u origin main
```
### 正常推送流程(以后每次)
```powershell
git add .
git commit -m "改了什么"
git push
```
**现有仓库**:`test`(F:\AI\docker 首次推送)、`Obsidian`(笔记库)。
---
## 六、Obsidian 自动备份(Obsidian Git 插件)
### 1. 建仓库
Gitea 里点右上角 `+` → 新建仓库,名 `Obsidian`。
### 2. 装插件
Obsidian → 设置(左下角齿轮)→ 第三方插件 → 关掉**安全模式** → 浏览 → 搜 `Vinzent` → 点 **Git**(作者 Vinzent,3.4M 下载)→ 安装 → 启用。
### 3. 笔记库初始化并推送
笔记库位置:`F:\AI\Obsidian`。在 PowerShell 里跑(隧道保持开着):
```powershell
cd F:\AI\Obsidian
git init
git add .
git commit -m "first commit"
git remote add origin http://localhost:3001/admin/Obsidian.git
git branch -M main
git push -u origin main
```
推时填 admin 和 Gitea 密码。
### 4. 开自动备份
关掉 Obsidian 再重开 → 设置 → 第三方插件 → Git:
- **Automatic** → **Auto commit-and-sync interval (minutes)** 由 `0` 改成 `10`(0 = 关闭)
- **Sync** 里确认 **Push on commit-and-sync** 和 **Pull on commit-and-sync** 都是开着的
此后每 10 分钟自动提交并推送,写笔记不用管。
> 注意:自动推送走 `localhost:3001`,隧道必须开着。隧道关了推不上去,下次开隧道后会自动补推。
---
## 七、另一台电脑怎么下载笔记
1. 装 Git(git-scm.com)。
2. 开隧道(新建 `gitea-tunnel.bat`,双击运行,窗口保持开着):
```bat
@echo off
ssh -N -L 3001:127.0.0.1:3001 ubuntu@43.156.118.130
pause
```
3. 克隆(进想放笔记的文件夹):
```powershell
git clone http://localhost:3001/admin/Obsidian.git
```
填 admin 和 Gitea 密码。
4. Obsidian 里点"打开文件夹作为仓库",选克隆下来的文件夹。
5. 同样装 Obsidian Git 插件、开 Auto commit-and-sync,即可双向自动备份。
---
## 八、常见问题:打不开 Gitea 怎么办
### 隧道窗口"不动了"是正常的
`ssh -N` 的意思是"只建隧道、不给命令行"。输完密码后窗口停住不动、没有提示符,**就是隧道通了的样子**。窗口保持开着别关,关了隧道就断了。
### 打不开 `http://localhost:3001` 的排查顺序
1. **隧道开着吗?**——tunnel.bat 窗口或单独的 3001 隧道窗口,哪个关了 3001 就断了。重新双击 tunnel.bat(新版已含 3001)。
2. **端口冲突**——如果单独的 3001 隧道窗口还开着,tunnel.bat 里的 3001 就绑不上(`bind: address already in use`)。先把所有隧道窗口都关掉,再只开一个。
3. **容器在跑吗?**——SSH 上服务器跑:
```bash
docker ps --filter 'name=gitea' --format '{{.Status}} {{.Ports}}'
```
看不到 `Up` 就是容器停了,跑 `docker start gitea`。
### 记住一条
**Gitea 只认隧道**:它只绑了服务器本机 `127.0.0.1`,外网连不上,浏览器开 `localhost:3001` 之前,隧道必须先开着。
---
## 九、服务器上现在跑的东西(2026-11-05)
| 服务 | 位置/端口 | 状态 |
| --- | --- | --- |
| Xray + Shadowsocks(梯子) | 8388,systemd 服务 `xray-ss` | 正常 |
| CPA(Gemini Pro 转 API) | Docker,127.0.0.1:8317 | 正常 |
| NewAPI | Docker,127.0.0.1:3000 | 正常 |
| Nginx | 域名 api.trivoo.website | 正常 |
| **Gitea** | **Docker,127.0.0.1:3001** | **正常** |
| 3x-ui | 已停用(`systemctl stop x-ui`),但仍开机自启 | 待处理:`sudo systemctl disable x-ui` |