Linux OneDrive同步工具
在本文中,我将分享如何在 Debian Linux 服务器上通过命令行方式配置 OneDrive 同步工具,实现:
- 同步 OneDrive 上某个文件夹
- 自动下载(不上传)
- 实时监控同步
- 后台运行
- 随开机自动启动
适用于无桌面的服务器环境,例如通过 SSH 登录的 VPS。
✅ 安装 OneDrive 同步客户端(abraunegg 版)
首先安装依赖并从源码编译:
sudo apt update
sudo apt install -y ldc pkg-config git curl build-essential libcurl4-openssl-dev libsqlite3-dev
git clone https://github.com/abraunegg/onedrive.git
cd onedrive
./configure
make
sudo make install
✅ 授权 OneDrive 登录
首次运行会提示你复制一个授权链接:
onedrive
复制链接到本地浏览器访问,登录并授权后,将跳转到一个以 https://login.microsoftonline.com/common/oauth2/nativeclient?...
开头的地址。把这个完整链接复制回 SSH 终端,回车完成授权。
✅ 设置同步目录配置(可选)
你可以指定本地同步目录和忽略某些临时文件:
编辑配置文件:
mkdir -p ~/.config/onedrive
nano ~/.config/onedrive/config
内容示例:
sync_dir = "/root/qbit" ##根据你自己的路径调整
skip_file = "~*|.tmp"
❗️注意:不能在 config 文件中使用 single_directory
参数,它只能通过命令行传入。
✅ 只同步 OneDrive 某个文件夹
使用 --single-directory
参数:
onedrive --monitor --single-directory "qbit" --download-only
- 只会同步 OneDrive 上的
qbit
(是你同步的) 文件夹 - 不会上传本地文件改动(只下载)
✅ 后台运行 OneDrive 实时同步
使用 nohup
方式运行:
nohup onedrive --monitor --single-directory "qbit" --download-only > /var/log/onedrive.log 2>&1 &
✅ 设置 systemd 用户服务实现开机自启
步骤 1:创建 systemd 服务文件
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/onedrive.service
内容如下:
[Unit]
Description=OneDrive Sync Service (Single Directory)
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/onedrive --monitor --single-directory "qbit" --download-only
Restart=always
RestartSec=10
Environment=XDG_CONFIG_HOME=/root/.config
[Install]
WantedBy=default.target
注意:请根据which onedrive
调整ExecStart
路径
步骤 2:启用并启动服务
loginctl enable-linger $(whoami)
systemctl --user daemon-reexec
systemctl --user daemon-reload
systemctl --user enable onedrive.service
systemctl --user start onedrive.service
检查运行状态:
systemctl --user status onedrive.service
✅ 升级 cURL(可选)
如果出现如下警告:
WARNING: Your cURL/libcurl version (7.88.1) has known HTTP/2 bugs ...
说明你的系统使用了存在兼容性问题的旧版 curl
,但客户端已自动回退使用 HTTP/1.1。为最佳体验,你可以手动升级 curl 到 7.92+,或者忽略该提示继续使用。