Linux 下 OneDrive 文件同步开源软件使用

Linux 下 OneDrive 文件同步开源软件使用

[TOC]

OneDrive 没有提供官方的 Linux 软件, 但是微软有比较好用的接口, 因此第三方开发者提供了一些开源的项目供我们使用, 本文介绍的为使用人数较多的一个.
开源项目地址:https://github.com/abraunegg/onedrive.

基本信息

  • fork from 已经不再维护的 skilion 项目.
  • 基于 D 语言开发.
  • github 5.5k stars.
  • 开发较为活跃,2018/05/17初版 release, 最新版本 v2.4.15,共 release 了 48 个版本.

需要注意的一点的是, 因为是开源系统, 问题的支持度并不高, 我的系统是 Ubuntu 18.04 使用源码安装, 出现了已知的 bug –> onedrive --monitor runs automatically 导致程序无法运行, github issue 页面. 作者认为这是 Ubuntu PPA 的问题, 不进行支持解决, 也就意味着 Ubuntu 下基本上没法使用此软件.

开源之殇.

特点

  • 支持主流的 Linux 系统 FreeBSD 以及 Docker 镜像: Can run on all major Linux distributions, FreeBSD, or as a Docker container.
  • 支持单向/双向同步功能: Supports one-way and two-way sync capabilities.
  • 软件状态监测: State caching.
  • 基于 inotify 的实时文件监控: Real-Time local file monitoring with inotify.
  • 通过钩子实时远端同步: Real-Time syncing of remote updates via webhooks.
  • 保证文件安全: File upload / download validation to ensure data integrity.
  • 支持断点上传: Resumable uploads.
  • 支持 OneDrive for Business, OneDrive for Personal, OneDrive for Office365 and SharePoint.
  • 基于 libnotify 的桌面通知: Desktop notifications via libnotify.
  • 可用于测试的 Dry-run: Dry-run capability to test configuration changes.
  • 防止配置更改后的数据丢失: Prevent major OneDrive accidental data deletion after configuration change.
  • 国内支持度较好: Support for National cloud deployments (Microsoft Cloud for US Government, Microsoft Cloud Germany, Azure and Office 365 operated by 21Vianet in China)
  • 支持多用户: Supports single & multi-tenanted applications .
  • 支持速度配置: Supports rate limiting of traffic.

待开发

  • 加密功能, 现阶段可以通过别的加密软件实现,例如 Cryptomator: Ability to encrypt/decrypt files on-the-fly when uploading/downloading files from OneDrive.
  • 像 Windows 上实现即用即下载打开: Support for Windows ‘On-Demand’ functionality so file is only downloaded when accessed locally,.

安装

本文仅以 Ubuntu 18.04 x86-64 为例子,至于其他系统以及架构请查阅官方手册.

两种方式安装:

  1. package
  2. 源码安装

Ubuntu 下的 Universe packages(例如 apt install) 一般情况下不可用,推荐使用源码安装.
如果希望尝试 package 安装,从 OpenSuSE Build Service 下载安装包.

支持的 package 如下:

源码安装

安装过程:

  1. 安装依赖: Install the platform dependancies for your Linux OS.
  2. 启动 D 编译器: Activate your DMD or LDC compiler.(这一步与第四步,官网要求做,但是我没有做也编译成功了)
  3. 下载源码并编译安装: Clone the GitHub repository, run configure and make, then install.
  4. 解除 D 编译器: Deactivate your DMD or LDC compiler.

安装要求:

DMD version >= 2.087.0 or LDC version >= 1.17.0 is required to compile this application

依赖包:

1
2
3
4
5
6
7
sudo apt install build-essential
sudo apt install libcurl4-openssl-dev
sudo apt install libsqlite3-dev
sudo apt install pkg-config
sudo apt install git
sudo apt install curl
sudo apt install libnotify-dev

D语言编译器(dmd):

1
curl -fsS https://dlang.org/install.sh | bash -s dmd

编译与安装

官网要求启动/关闭 DMD 分别在安装前与安装完成后,如下:

1
2
3
Run `source ~/dlang/dmd-2.087.0/activate` in your shell to use dmd-2.087.0.
This will setup PATH, LIBRARY_PATH, LD_LIBRARY_PATH, DMD, DC, and PS1.
Run `deactivate` later on to restore your environment.

但是我下载的是 dmd v2.090.1 版本, 不需要进行上面的动作也实际成功了.

具体过程:

1
2
3
4
5
6
git clone https://github.com/abraunegg/onedrive.git
cd onedrive
./configure
make clean; make;
sudo make install
onedrive --version #检查安装成功与否以及安装版本

编译选项

1
2
3
4
5
--enable-notifications #允许通知
--with-systemdsystemunitdir
--with-systemduserunitdir # 配置 Systemd service files 位置
--enable-debug
--enable-completions #安装完成的信号传给 bash, zsh and fish

卸载

1
2
3
cd onedrive
sudo make uninstall
rm -rf ~/.config/onedrive #删除配置文件

版本升级

如果是像我一样从源码安装的,需要先卸载已安装的然后重新下载新版本源码并重新源码安装一遍.

使用与配置

注意:修改配置文件后都需要执行一遍 onedrive --synchronize --resync,使得配置生效.

首次运行

需要网页登录账号,进行授权,(这里需要科学一下),用浏览器打开 URL, 输入账号密码, 将浏览器返回的URL粘贴到命令行回车即可完成授权.

1
2
3
4
5
6
7
8
9
10
[user@hostname ~]$ onedrive 
Authorize this app visiting:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=22c49a0d-d21c-4792-aed1-8f163c982546&scope=Files.ReadWrite%20Files.ReadWrite.all%20Sites.ReadWrite.All%20offline_access&response_type=code&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient

Enter the response uri: https://login.microsoftonline.com/common/oauth2/nativeclient?code=<redacted>

Application has been successfully authorised, however no additional command switches were provided.

Please use --help for further assistance in regards to running this application.

配置层级

  1. 默认值: the default values
  2. 配置文件读取值: values set in the configuration file
  3. 命令行输入值: values passed in via the command line
1
2
mkdir -p ~/.config/onedrive #或者考虑在 /etc/onedrive 目录下
cp ./config ~/.config/onedrive/config

配置文件支持 * ? | 通配符.

sync_list还支持! or - 放在行首位置排除目标

配置同步规则

1
vim ~/.config/onedrive/config

其内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Configuration for OneDrive Linux Client
# This file contains the list of supported configuration fields
# with their default values.
# All values need to be enclosed in quotes
# When changing a config option below, remove the '#' from the start of the line
# For explanations of all config options below see docs/USAGE.md or the man page.
#
sync_dir = "~/OneDrive" # 同步文件夹位置.
# skip_file = "~*|.~*|*.tmp" # 跳过的文件,这里是双向的,本地和云端都生效(本地的不会上传,云端的不会下载)
# monitor_interval = "300" # 监测周期
# skip_dir = "" # 跳过的文件夹,这里是双向的,本地和云端都生效(本地的不会上传,云端的不会下载)
# log_dir = "/var/log/onedrive/" # log 存储位置
# drive_id = ""
# upload_only = "false" # 是否单向上传
# check_nomount = "false" # 是否检查启动实例
# check_nosync = "false" # 是否检查不同步
# download_only = "false" # 是否单向下载
disable_notifications = "false" # 是否关闭通知
disable_upload_validation = "false" # 是否关闭上传校验
# enable_logging = "false" # 是否开启 logging
# force_http_2 = "false" # 是否强制使用 http2
# local_first = "false" # 本地文件更新优先
# no_remote_delete = "false" # 是否设定不删除云端文件,为了云端数据安全
# skip_symlinks = "false" # 是否跳过文件链接
# debug_https = "false"
skip_dotfiles = "true" # 是否忽略所有隐藏文件和文件夹.
dry_run = "false" # 是否只进行测试而不是实际同步
# min_notify_changes = "5"
# monitor_log_frequency = "5"
# monitor_fullscan_frequency = "10"
# sync_root_files = "false" # 一键失效 sync_dir 与 sync_list 设置
# classify_as_big_delete = "1000"
# user_agent = ""
# remove_source_files = "false"
# skip_dir_strict_match = "false"
# application_id = ""
# resync = "false"
# bypass_data_preservation = "false"
# azure_ad_endpoint = ""
# azure_tenant_id = "common"
# sync_business_shared_folders = "false"
# sync_dir_permissions = "700" # 对同步文件夹权限进行设置
# sync_file_permissions = "600" # 对同步文件权限进行设置
# rate_limit = "131072" # 限制网络传输速度
# webhook_enabled = "false" # webhook 用来获取云端的更新自动更新本地
# webhook_public_url = ""
# webhook_listening_host = ""
# webhook_listening_port = "8888"
# webhook_expiration_interval = "86400"
# webhook_renewal_interval = "43200"
# operation_timeout = "3600" # 限制所有操作步骤加在一起的总时间

Tips

  1. 修改默认同步文件夹.
1
sync_dir="~/MyDirToSync" # 配置同步文件夹

注意更改默认同步文件夹没有成功时(例如未使用 onedrive --synchronize --resync同步),可能会导致系统误认为默认的 ~/OneDrive生效并且为空从而把云端的数据全部清除.同时也要注意sync_dir如果指向 network mount point,例如NFS, Windows Network Share, Samba Network Share,因为它们不支持 inotify 所以没办法实时通知你改变,需要多加小心.

  1. 权限

对于高于如下权限的文件/文件夹进行同步:

  • Directories: 700 - This provides the following permissions: drwx------
  • Files: 600 - This provides the following permissions: -rw-------

也可以进行配置:

1
2
sync_dir_permissions = "700"
sync_file_permissions = "600"
  1. 可以参考win10下的方法, 通过设置软链接, 避免改动本地文件的位置, 具体操作:
1
2
# ln -s 想要同步的本地文件位置 Onedrive同步文件夹位置
ln -s /home/work/work_dir /home/work/Onedrive
  1. 也可以使用专门配置文件的形式设置需要同步的文件/文件夹:

~/.config/onedrive 下创建文件sync_list .

1
2
3
4
sync_list:
Root
test
1.txt

测试配置文件

使用onedrive --synchronize --verbose --dry-run可以测试配置文件.

--dry-run不会改变本地或云端的任何文件, 终端输出的是”将要发生的情况”.

运行

手动运行

1
2
onedrive [options] --synchronize
onedrive [options] --resync

下面的动作会触发 resync:

  • sync_dir
  • skip_dir
  • skip_file
  • drive_id
  • Modifying sync_list
  • Modifying business_shared_folders

自动运行

1
2
systemctl --user enable onedrive
systemctl --user start onedrive

监测错误

1
onedrive [options] --monitor

可能会报 2 种错误(本地环境错误):

  • Intialisation failure
  • Unable to add a new inotify watch

修复方法参考官网.

查看状态

1
2
3
4
5
6
# 同步状态
onedrive [options] --display-sync-status
# 日志
journalctl --user-unit onedrive -f
# 当前配置情况
onedrive [options] --display-config

GUI使用

还没有 GUI, 只有几个第三方开发的 GUI 工具:

附录

命令参数列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Usage:
onedrive [options] --synchronize
Do a one time synchronization
onedrive [options] --monitor
Monitor filesystem and sync regularly
onedrive [options] --display-config
Display the currently used configuration
onedrive [options] --display-sync-status
Query OneDrive service and report on pending changes
onedrive -h | --help
Show this help screen
onedrive --version
Show version

Options:

--auth-files
Perform authentication not via interactive dialog but via files read/writes to these files.
--auth-response
Perform authentication not via interactive dialog but via providing the reponse url directly.
--check-for-nomount
Check for the presence of .nosync in the syncdir root. If found, do not perform sync.
--check-for-nosync
Check for the presence of .nosync in each directory. If found, skip directory from sync.
--classify-as-big-delete
Number of children in a path that is locally removed which will be classified as a 'big data delete'
--confdir ARG
Set the directory used to store the configuration files
--create-directory ARG
Create a directory on OneDrive - no sync will be performed.
--create-share-link ARG
Create a shareable link for an existing file on OneDrive
--debug-https
Debug OneDrive HTTPS communication.
--destination-directory ARG
Destination directory for renamed or move on OneDrive - no sync will be performed.
--disable-download-validation
Disable download validation when downloading from OneDrive
--disable-notifications
Do not use desktop notifications in monitor mode.
--disable-upload-validation
Disable upload validation when uploading to OneDrive
--display-config
Display what options the client will use as currently configured - no sync will be performed.
--display-sync-status
Display the sync status of the client - no sync will be performed.
--download-only
Replicate the OneDrive online state locally, by only downloading changes from OneDrive. Do not upload local changes to OneDrive.
--dry-run
Perform a trial sync with no changes made
--enable-logging
Enable client activity to a separate log file
--force
Force the deletion of data when a 'big delete' is detected
--force-http-2
Force the use of HTTP/2 for all operations where applicable
--get-O365-drive-id ARG
Query and return the Office 365 Drive ID for a given Office 365 SharePoint Shared Library
--get-file-link ARG
Display the file link of a synced file
--help -h
This help information.
--list-shared-folders
List OneDrive Business Shared Folders
--local-first
Synchronize from the local directory source first, before downloading changes from OneDrive.
--log-dir ARG
Directory where logging output is saved to, needs to end with a slash.
--logout
Logout the current user
--min-notify-changes ARG
Minimum number of pending incoming changes necessary to trigger a desktop notification
--monitor -m
Keep monitoring for local and remote changes
--monitor-fullscan-frequency ARG
Number of sync runs before performing a full local scan of the synced directory
--monitor-interval ARG
Number of seconds by which each sync operation is undertaken when idle under monitor mode.
--monitor-log-frequency ARG
Frequency of logging in monitor mode
--no-remote-delete
Do not delete local file 'deletes' from OneDrive when using --upload-only
--operation-timeout
Maximum amount of time (in seconds) an operation is allowed to take
--print-token
Print the access token, useful for debugging
--remove-directory ARG
Remove a directory on OneDrive - no sync will be performed.
--remove-source-files
Remove source file after successful transfer to OneDrive when using --upload-only
--resync
Forget the last saved state, perform a full sync
--single-directory ARG
Specify a single local directory within the OneDrive root to sync.
--skip-dir
Skip any directories that match this pattern from syncing
--skip-dir-strict-match
When matching skip_dir directories, only match explicit matches
--skip-dot-files
Skip dot files and folders from syncing
--skip-file ARG
Skip any files that match this pattern from syncing
--skip-size
Skip new files larger than this size (in MB)
--skip-symlinks
Skip syncing of symlinks
--source-directory ARG
Source directory to rename or move on OneDrive - no sync will be performed.
--sync-root-files
Sync all files in sync_dir root when using sync_list.
--sync-shared-folders
Sync OneDrive Business Shared Folders
--syncdir ARG
Specify the local directory used for synchronization to OneDrive
--synchronize
Perform a synchronization
--upload-only
Replicate the locally configured sync_dir state to OneDrive, by only uploading local changes to OneDrive. Do not download changes from OneDrive.
--user-agent ARG
Specify a User Agent string to the http client
--verbose -v+
Print more details, useful for debugging (repeat for extra debugging)
--version
Print the version and exit

参考链接:https://zhuanlan.zhihu.com/p/372355859

作者

cx

发布于

2022-02-27

更新于

2022-07-16

许可协议