Rclone 连接 Google Drive

转载自https://blog.csdn.net/hzlarm/article/details/107127717

安装和配置 Rclone

官方提供了一键安装脚本:
curl https://rclone.org/install.sh | sudo bash
安装完后,输入 rclone config 命令进入交互式配置选项。

e) Edit existing remote	#编辑现有远程
n) New remote			#新建远程
d) Delete remote		#删除远程
r) Rename remote		#重命名远程
c) Copy remote			#复制远程
s) Set configuration password	#设置配置密码
q) Quit config			#退出
e/n/d/r/c/s/q> n   #这里输入n代表新建

name>sbq		#随便起一个,后面可以通过再次配置重命名

Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
1 / 1Fichier
   \ "fichier"
12 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
13 / Google Drive
   \ "drive"
38 / seafile
   \ "seafile"
Storage>13    #这里列出各种storage,选择对应的。如谷歌driver是13。12对应的是开发人员。区别在下面

** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> 			# 这里直接默认回车

获取网址
https://console.developers.google.com/apis/api/drive.googleapis.com/overview

Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> # 这里直接默认回车
获取网址
https://console.developers.google.com/apis/credentials/oauthclient

Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1 		#选择1  对应权限最大

ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.
Note that if this is blank, the first time rclone runs it will fill it
in with the ID of the root folder.
Enter a string value. Press Enter for the default ("").
root_folder_id>   # 这里直接默认回车

Service Account Credentials JSON file path 
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file>   # 这里直接默认回车

Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n  #n或者直接回车

Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n> n  #n

Please go to the following link: https://accounts.google.com*****balaba*******
Log in and authorize rclone for access
Enter verification code> 4/1g****balabala*******ETSvZU   #浏览器打开这个连接获取验证码粘贴到这里

Configure this as a team drive?
y) Yes
n) No (default)
y/n> n					#这里注意,选择是否是团队盘。默认no

--------------------
[sbq]
type = drive
scope = drive
token = {"access_token":"***balabala****","expiry":"2020-07-04T19:46:55.549375784+08:00"}
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y      		#y默认y就成功了,显示如下

Current remotes:
Name                 Type
====                 ====
Ggdrv                drive
Ggteam               drive
sbq                  drive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q>  #绕一圈转回来了q退出,或者重命名或者删除。

Google Cloud和Google Drive之间的区别

安装 fuse

挂载需要安装 fuse,根据自己的系统来选择安装命令:

# Debian/Ubantu
apt-get update && apt-get install -y fuse
# CentOS
yum install -y fuse

挂载网盘

挂载网盘分为手动挂载和开机自动挂载,根据自己的需求来选择。

手动挂载

#挂载
rclone mount <网盘名称:网盘路径> <本地路径> [参数] --daemon

#取消挂载
fusermount -qzu <本地路径>

网盘名称为配置时填的 name,网盘路径为网盘里的文件夹,留空为整个网盘,本地路径为 VPS 上的本地文件夹。参数可以查看官方文档根据需求进行选择。实际输入时不要有括号,这里只是为了更清楚的区分。--daemon 为进程守护参数,可后台运行。

使用示例
输入命令进行挂载操作:

rclone mount Onedrive:/ /Onedrive --copy-links --allow-other --allow-non-empty --umask 000 --daemon
然后输入 df -h 命令查看挂载情况。

图片

开机自动挂载

下载并编辑自启脚本
wget -N git.io/rcloned && vi rcloned
修改内容:

NAME="Onedrive" #Rclone配置时填写的name
REMOTE=''  #远程文件夹,网盘里的挂载的一个文件夹,留空为整个网盘
LOCAL='/Onedrive'  #挂载地址,VPS本地挂载目录

设置开机自启

mv rcloned /etc/init.d/rcloned
chmod +x /etc/init.d/rcloned
update-rc.d -f rcloned defaults # Debian/Ubuntu
chkconfig rcloned on # CentOS
bash /etc/init.d/rcloned start

看到 [信息] rclone 启动成功 ! 即可。

管理

开始挂载 bash /etc/init.d/rcloned start

停止挂载 bash /etc/init.d/rcloned stop

重新挂载 bash /etc/init.d/rcloned restart

查看日志tail -f /$HOME/.rclone/rcloned.log

卸载自启挂载

bash /etc/init.d/rcloned stop
update-rc.d -f rcloned remove # Debian/Ubuntu
chkconfig rcloned off # CentOS
rm -f /etc/init.d/rcloned

Rclone命令语法

rclone config file - 显示配置文件的路径,一般配置文件在 ~/.config/rclone/rclone.conf
rclone config show - 显示配置文件信息

Rclone命令

# 本地到网盘
rclone [功能选项] <本地路径> <网盘名称:路径> [参数] [参数] ...

# 网盘到本地
rclone [功能选项] <网盘名称:路径> <本地路径> [参数] [参数] ...

# 网盘到网盘
rclone [功能选项] <网盘名称:路径> <网盘名称:路径> [参数] [参数] ...

用法示例
rclone move -v /Download Onedrive:/Download --transfers=1

常用功能选项

rclone copy- 复制
rclone move - 移动,如果要在移动后删除空源目录,请加上 --delete-empty-src-dirs 参数
rclone sync - 同步:将源目录同步到目标目录,只更改目标目录。
rclone size - 查看网盘文件占用大小。
rclone delete - 删除路径下的文件内容。
rclone purge - 删除路径及其所有文件内容。
rclone mkdir - 创建目录。
rclone rmdir - 删除目录。
rclone rmdirs - 删除指定灵境下的空目录。如果加上 --leave-root 参数,则不会删除根目录。
rclone check - 检查源和目的地址数据是否匹配。
rclone ls - 列出指定路径下的所有的文件以及文件大小和路径。
rclone lsl - 比上面多一个显示上传时间。
rclone lsd 列出指定路径下的目录
rclone lsf - 列出指定路径下的目录和文件

常用参数

-n = --dry-run - 测试运行,用来查看 rclone 在实际运行中会进行哪些操作。
-P = --progress - 显示实时传输进度,500mS 刷新一次,否则默认 1 分钟刷新一次。
--cache-chunk-size SizeSuffi - 块的大小,默认5M,理论上是越大上传速度越快,同时占用内存也越多。如果设置得太大,可能会导致进程中断。
--cache-chunk-total-size SizeSuffix - 块可以在本地磁盘上占用的总大小,默认10G。
--transfers=N - 并行文件数,默认为4。在比较小的内存的VPS上建议调小这个参数,比如128M的小鸡上使用建议设置为1。
--config string - 指定配置文件路径,string为配置文件路径。
--ignore-errors - 跳过错误。比如 OneDrive 在传了某些特殊文件后会提示Failed to copy: failed to open source object: malwareDetected: Malware detected,这会导致后续的传输任务被终止掉,此时就可以加上这个参数跳过错误。但需要注意 RCLONE 的退出状态码不会为0。

日志

rclone 有 4 个级别的日志记录,ERRORNOTICEINFO和 DEBUG。默认情况下,rclone 将生成 ERROR 和 NOTICE 级别消息。

-q - rclone将仅生成 ERROR 消息。
-v - rclone将生成ERRORNOTICE 和 INFO 消息,推荐此项。
-vv - rclone 将生成 ERRORNOTICEINFODEBUG 消息。
--log-level LEVEL - 标志控制日志级别。

输出日志到文件

使用 --log-file=FILE 选项,rclone 会将 Error,Info 和 Debug 消息以及标准错误重定向到 FILE,这里的 FILE 是你指定的日志文件路径。

另一种方法是使用系统的指向命令,比如:

rclone sync -v Onedrive:/DRIVEX Gdrive:/DRIVEX > "~/DRIVEX.log" 2>&1

文件过滤

--exclude - 排除文件或目录。

--include - 包含文件或目录。

--filter - 文件过滤规则,相当于上面两个选项的其它使用方式。包含规则以 + 开头,排除规则以 - 开头。

文件类型过滤

比如 --exclude "*.bak"--filter "- *.bak",排除所有 bak 文件。也可以写作。

比如--include "*.{png,jpg}"--filter "+ *.{png,jpg}",包含所有 png 和 jpg 文件,排除其他文件。

--delete-excluded 删除排除的文件。需配合过滤参数使用,否则无效。

目录过滤

目录过滤需要在目录名称后面加上 /,否则会被当做文件进行匹配。以 / 开头只会匹配根目录(指定目录下),否则匹配所目录。这同样适用于文件。

--exclude ".git/" 排除所有目录下的.git 目录。

--exclude "/.git/"只排除根目录下的.git 目录。

--exclude "{Video,Software}/" 排除所有目录下的 Video 和 Software 目录。

--exclude "/{Video,Software}/" 只排除根目录下的 Video 和 Software 目录。
--include "/{Video,Software}/**"仅包含根目录下的 Video 和 Software 目录的所有内容。

文件大小过滤

默认大小单位为kBytes,但可以使用 k ,M 或 G 后缀。

--min-size 过滤小于指定大小的文件。比如 --min-size 50 表示不会传输小于 50k 的文件。

--max-size 过滤大于指定大小的文件。比如 --max-size 1G 表示不会传输大于 1G 的文件。

TIPS: 博主在实际使用中发现大小过滤两个选项不能同时使用。

过滤规则文件

--filter-from <规则文件> 从文件添加包含 / 排除规则。比如 --filter-from filter-file.txt

过滤规则文件示例:

- secret*.jpg
+ *.jpg
+ *.png
+ file2.avi
- /dir/Trash/**
+ /dir/**
- *

这里只举例比较常用和简单的一些过滤用法,更复杂和高端的用法可以查看官方文档。

环境变量

rclone 中的每个选项都可以通过环境变量设置。环境变量的名称可以通过长选项名称进行转换,删除--前缀,更改-_,大写并添加前缀 RCLONE_。环境变量的优先级会低于命令行选项,即通过命令行追加相应的选项时会覆盖环境变量设定的值。

比如设置最小上传大小--min-size 50,使用环境变量是 RCLONE_MIN_SIZE=50。当环境变量设置后,在命令行中使用 --min-size 100,那么此时环境变量的值就会被覆盖。

常用环境变量

RCLONE_CONFIG - 自定义配置文件路径
RCLONE_CONFIG_PASS - 若 rclone 进行了加密设置,把此环境变量设置为密码,可自动解密配置文件。
RCLONE_RETRIES - 上传失败重试次数,默认 3 次
RCLONE_RETRIES_SLEEP - 上传失败重试等待时间,默认禁用,单位s、m、h分别代表秒、分钟、小时。
CLONE_TRANSFERS - 并行上传文件数。
RCLONE_CACHE_CHUNK_SIZE - 块的大小,默认5M,理论上是越大上传速度越快,同时占用内存也越多。如果设置得太大,可能会导致进程中断。
RCLONE_CACHE_CHUNK_TOTAL_SIZE - 块可以在本地磁盘上占用的总大小,默认10G。
RCLONE_IGNORE_ERRORS=true - 跳过错误。

参考资料

Rclone 官方文档

THE END
分享
二维码
< <上一篇
下一篇>>