Update from Sync Service
This commit is contained in:
226
实践积累/AI工程/WebDAV挂载指南.md
Executable file
226
实践积累/AI工程/WebDAV挂载指南.md
Executable file
@@ -0,0 +1,226 @@
|
||||
# WebDAV 挂载到 Linux 本地目录
|
||||
|
||||
将远程 WebDAV 服务器(如 Obsidian/Synology NAS)挂载为本地文件夹,像操作本地文件一样管理。
|
||||
|
||||
---
|
||||
|
||||
## 方法一:davfs2(推荐,最稳定)
|
||||
|
||||
### 1. 安装 davfs2
|
||||
|
||||
```bash
|
||||
# Debian/Ubuntu
|
||||
sudo apt-get install davfs2
|
||||
|
||||
# CentOS/RHEL
|
||||
sudo yum install davfs2
|
||||
|
||||
# macOS
|
||||
brew install davfs2
|
||||
```
|
||||
|
||||
### 2. 挂载 WebDAV
|
||||
|
||||
```bash
|
||||
# 创建挂载点
|
||||
sudo mkdir -p /mnt/webdav
|
||||
|
||||
# 挂载(需要输入用户名密码)
|
||||
sudo mount -t davfs http://your-server:port/path /mnt/webdav
|
||||
|
||||
# 或者指定用户名
|
||||
sudo mount -t davfs -o username=admin http://your-server:port/path /mnt/webdav
|
||||
```
|
||||
|
||||
### 3. 开机自动挂载
|
||||
|
||||
```bash
|
||||
# 添加到 /etc/fstab
|
||||
echo "http://your-server:port/path /mnt/webdav davfs defaults,username=admin,password=yourpass 0 0" | sudo tee -a /etc/fstab
|
||||
|
||||
# 或者保存密码到文件(更安全)
|
||||
echo "yourpass" | sudo tee /etc/davfs2/secrets
|
||||
chmod 600 /etc/davfs2/secrets
|
||||
echo "/mnt/webdav username password" | sudo tee -a /etc/davfs2/secrets
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 方法二:rclone(支持云存储,适合 Docker 环境)
|
||||
|
||||
### 1. 安装 rclone
|
||||
|
||||
```bash
|
||||
# 下载最新版本
|
||||
curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
|
||||
unzip rclone-current-linux-amd64.zip
|
||||
sudo mv rclone-*-linux-amd64/rclone /usr/local/bin/
|
||||
sudo chown root:root /usr/local/bin/rclone
|
||||
sudo chmod 755 /usr/local/bin/rclone
|
||||
rm -rf rclone-*-linux-amd64 rclone-current-linux-amd64.zip
|
||||
```
|
||||
|
||||
### 2. 配置 rclone
|
||||
|
||||
```bash
|
||||
# 交互式配置
|
||||
rclone config
|
||||
|
||||
# 选择 n 新建配置
|
||||
# 输入名称,如:obsidian-webdav
|
||||
# 类型选择:webdav
|
||||
# URL:https://your-server:port/path
|
||||
# vendor:other
|
||||
# 输入用户名密码
|
||||
```
|
||||
|
||||
### 3. 挂载为本地目录
|
||||
|
||||
```bash
|
||||
# 创建挂载点
|
||||
mkdir -p ~/obsidian-webdav
|
||||
|
||||
# 前台测试(确认能连接)
|
||||
rclone mount obsidian-webdav: ~/obsidian-webdav --vv
|
||||
|
||||
# 后台运行
|
||||
rclone mount obsidian-webdav: ~/obsidian-webdav \
|
||||
--daemon \
|
||||
--vfs-cache-mode writes \
|
||||
--allow-other
|
||||
|
||||
# 开机自启(systemd)
|
||||
cat > ~/.config/systemd/user/rclone-obsidian.service << 'EOF'
|
||||
[Unit]
|
||||
Description=rclone mount for Obsidian
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/local/bin/rclone mount obsidian-webdav: %h/obsidian-webdav --vfs-cache-mode writes
|
||||
ExecStop=/bin/fusermount -uz %h/obsidian-webdav
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
EOF
|
||||
|
||||
systemctl --user enable rclone-obsidian.service
|
||||
systemctl --user start rclone-obsidian.service
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 方法三:fUSE + davfs2(用户空间挂载,无需 root)
|
||||
|
||||
### 1. 安装 fuse 和 davfs2
|
||||
|
||||
```bash
|
||||
sudo apt-get install fuse davfs2
|
||||
```
|
||||
|
||||
### 2. 配置非 root 用户使用 FUSE
|
||||
|
||||
```bash
|
||||
sudo usermod -a -G fuse $USER
|
||||
# 重新登录生效
|
||||
```
|
||||
|
||||
### 3. 挂载
|
||||
|
||||
```bash
|
||||
mkdir -p ~/webdav
|
||||
fusedav https://your-server:port/path -u username -p password ~/webdav
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 方法四:curlftps(FTP over HTTPS,需要 FUSE)
|
||||
|
||||
```bash
|
||||
# 安装
|
||||
sudo apt-get install curlftpfs
|
||||
|
||||
# 挂载
|
||||
sudo curlftpfs -o allow_other,user=username:password https://your-server/path /mnt/webdav
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 方法五:直接在代码中使用 WebDAV
|
||||
|
||||
### Python 示例
|
||||
|
||||
```python
|
||||
import webdav3.client as wc
|
||||
|
||||
options = {
|
||||
'webdav_hostname': "https://your-server:port/path",
|
||||
'webdav_login': "username",
|
||||
'webdav_password': "password"
|
||||
}
|
||||
|
||||
client = wc.Client(options)
|
||||
|
||||
# 列出文件
|
||||
print(client.list())
|
||||
|
||||
# 下载文件
|
||||
client.download_sync(remote_path="file.md", local_path="file.md")
|
||||
|
||||
# 上传文件
|
||||
client.upload_sync(local_path="new.md", remote_path="new.md")
|
||||
```
|
||||
|
||||
### 安装 webdav3
|
||||
```bash
|
||||
pip install webdav3
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 你的服务器信息
|
||||
|
||||
```
|
||||
服务器: http://8.134.220.229:13803
|
||||
用户名: admin
|
||||
密码: blackhill
|
||||
路径: /实践积累
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 快速测试命令
|
||||
|
||||
```bash
|
||||
# 测试连接(curl)
|
||||
curl -s --digest --user admin:blackhill \
|
||||
"http://8.134.220.229:13803/实践积累/" | head -20
|
||||
|
||||
# 列出文件
|
||||
curl -s --digest --user admin:blackhill \
|
||||
-X PROPFIND "http://8.134.220.229:13803/实践积累/" -H "Depth: 1" | \
|
||||
grep -oE '<d:href>[^<]+</d:href>' | head -10
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Q: 挂载后文件看不到?
|
||||
- 检查网络连接
|
||||
- 确认用户名密码正确
|
||||
- 查看日志:`dmesg | tail`
|
||||
|
||||
### Q: 写入慢或失败?
|
||||
- davfs2 有缓存,可能有延迟
|
||||
- 尝试增加缓存大小
|
||||
- 检查服务器端是否有写入权限限制
|
||||
|
||||
### Q: Docker 容器内无法挂载?
|
||||
- Docker 需要 `--privileged` 模式
|
||||
- 或者使用 rclone 的 `--allow-non-empty`
|
||||
|
||||
### Q: 开机自启失败?
|
||||
- 检查 systemd 服务配置
|
||||
- 确保网络在挂载前已就绪(添加 `After=network.target`)
|
||||
Reference in New Issue
Block a user