Linux(CentOS / RHEL)安装 OneDrive CLI 并配置开机自启(只上传模式)
下面给你一份增强版博客内容(已加入常用命令区块),直接可发布,结构更完整,偏实战。
Linux(CentOS / RHEL)部署 OneDrive CLI + 开机自启(生产级完整指南)
一、目标
本地目录 → 自动上传到 OneDrive
不受云端影响(防误删)
开机自动运行
稳定长期运行
二、安装 OneDrive
sudo dnf install epel-release -y
sudo dnf install onedrive -y
验证:
onedrive --version
三、首次授权(必须手动)
onedrive
流程:
-
打开终端给出的 URL
-
登录微软账号
-
同意授权
-
页面跳转(错误页面是正常的)
-
复制浏览器地址栏完整 URL
-
粘贴回终端
成功标志:
The application has been successfully authorised
四、确认授权
ls /root/.config/onedrive/
必须有:
refresh_token
items.sqlite3
五、创建配置文件
nano /root/.config/onedrive/config
推荐生产配置
sync_dir = "/data/onedrive"
upload_only = "true"
threads = "1"
check_nosync = "true"
check_nomount = "true"
skip_file = "~*|.~*|*.tmp|*.temp|*.swp|*.part|*.partial|*.ds_store|Thumbs.db|ehthumbs.db|*.log"
skip_dir = "node_modules|.git|__pycache__|venv|.idea"
六、创建目录
mkdir -p /data/onedrive
七、测试同步
onedrive --sync
八、开机自启
sudo systemctl enable onedrive
sudo systemctl start onedrive
九、常用命令(实战必备)
1)查看服务状态
systemctl status onedrive
2)实时查看日志
journalctl -u onedrive -f
3)重启服务(修改配置后必做)
sudo systemctl restart onedrive
4)停止 / 启动
sudo systemctl stop onedrive
sudo systemctl start onedrive
5)手动触发同步
onedrive --sync
6)强制重建同步(配置变更后)
onedrive --sync --resync
7)重新授权
onedrive --reauth
8)查看当前配置
onedrive --display-config
9)模拟同步(不实际执行)
onedrive --dry-run --sync
10)检查进程
ps aux | grep onedrive
11)强制重新上传文件
touch /data/onedrive/文件名
12)临时禁止目录同步
touch 目录/.nosync
十、配置修改后的坑(必须知道)
如果你修改了:
-
sync_dir
-
upload_only
-
skip_dir / skip_file
会报:
--resync required
处理方法
sudo systemctl stop onedrive
onedrive --sync --resync
sudo systemctl start onedrive
十一、运行机制
systemd → onedrive --monitor
行为:
-
实时监控目录
-
自动上传
-
定时同步(默认300秒)
十二、使用方式
上传数据
cp dump.sql /data/onedrive/
推荐命名方式
dump_20260322.sql
👉 避免覆盖问题
十三、目录结构建议
/data/onedrive/
├── mysql_backup/
├── redis_dump/
├── logs_archive/
├── strategy_results/
十四、同步模式说明
推荐(服务器)
upload_only = "true"
效果:
|
操作 |
行为 |
|---|---|
|
本地新增 |
上传 ✔ |
|
云端删除 |
本地不删 ✔ |
|
本地删除 |
云端不删 ✔ |
👉 安全备份模式
不推荐(双向)
upload_only = "false"
风险:
rm -rf → 云端数据被删
十五、常见问题
无限重启
onedrive@root ❌
onedrive ✔
CPU警告
threads = "1"
GUI 警告
👉 忽略
libcurl 警告
👉 不影响使用
十六、总结
OneDrive CLI 在服务器的正确用法:
upload_only + systemd
= 稳定、安全的云备份方案
最后一条建议
不要把 OneDrive 当同步盘
要把它当备份系统
如果你下一篇要写进阶,我可以帮你扩展:
-
自动 MySQL dump → OneDrive
-
Redis snapshot 自动上传
-
多服务器同步架构
浙公网安备 33010602011771号