Rclone挂载One Drive将文件同步到云存储

rclone.png

1.概述

Rclone官网

Rclone是一个命令行程序,用于管理云存储上的文件。它是云供应商Web存储界面的功能丰富的替代方案。超过40种云存储产品支持rclone,包括S3对象存储,业务和消费者文件存储服务以及标准传输协议。

Rclone只支持国外的一些云存储,因为One Drive 无法在大陆地区访问,如果要使用 Rclone 挂载 One Drive 就需要使用国外的服务器。

本文只介绍同步 One Drive 的方法,其他云存储请自行测试。

2.rclone能为您做什么?

Rclone可帮助您:

  • 将文件备份(和加密)到云存储
  • 从云存储还原(和解密)文件
  • 将云数据镜像到其他云服务或本地
  • 将数据迁移到云,或在云存储供应商之间迁移
  • 将多个加密的,缓存的或多样化的云存储作为磁盘挂载
  • 分析和使用占了云存储中保存的数据LSFljson大小ncdu
  • 文件系统联合在一起以将多个本地和/或云文件系统呈现为一个

特征

  • 转账
    • 始终检查MD5,SHA1哈希是否具有文件完整性
    • 时间戳记保留在文件中
    • 可以随时重启操作
    • 可以往返于网络,例如两个不同的云提供商
    • 可以使用多线程下载到本地磁盘
  • 新文件或更改的文件复制到云存储
  • 同步(单向)以使目录相同
  • 验证后将文件移动到云存储中,删除本地
  • 检查哈希以及丢失/多余的文件
  • 将您的云存储挂载为网络磁盘
  • 通过HTTP /WebDav /FTP /SFTP /dlna服务本地或远程文件
  • 基于实验Web的GUI

3.安装

获得 OneDrive 授权认证

首先在自己的电脑上下载Rclone Windows客户端
下载地址:https://downloads.rclone.org/rclone-current-windows-amd64.zip

解压后里面有个 rclone.exe文件复制到 C:\Windows\System32目录下
打开 cmd命令提示符输入

rclone authorize "onedrive"

输入之后浏览器会弹出 OneDrive 授权认证页面,登录授权。
授权完之后命令提示符会出现一串 token保存好等下要用。
TOKEN.png

授权准备完成

进入linux安装Rclone

先进行挂载依赖 fuse的安装,不然挂载的时候会出错

yum -y install fuse

以下提供两种方法安装选一种就可以

1.官方安装脚本安装,请先自行安装 curl

curl https://rclone.org/install.sh | sudo bash

2.下载压缩包安装,提取并解压缩

curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
cd rclone-*-linux-amd64

复制二进制文件

sudo cp rclone /usr/bin/
sudo chown root:root /usr/bin/rclone
sudo chmod 755 /usr/bin/rclone

安装手册页

sudo mkdir -p /usr/local/share/man/man1
sudo cp rclone.1 /usr/local/share/man/man1/
sudo mandb

安装完成

4.常用选项参数

配置命令

rclone config

配置文件所在目录

~/.config/rclone/rclone.conf

挂载命令

rclone mount <网盘名称>:<网盘路径> <本地路径> <参数> --daemon

卸载命令

fusermount -qzu <本地路径>

一些命令参数

rclone copy source:path dest:path
# 从源文件拷贝到目的,不会删除目的的文件,会跳过未变更的文件,基于大小和修改时间或MD5值判断是否变更过。
rclone sync source:path dest:path
# 将目的文件夹同步成和原文件夹完成相同,会删除目的文件夹中的其他文件,可以先用 --dry-run 参数运行,查看哪些文件会被删除和变更。
rclone move source:path dest:path
# 移动文件夹,对于少量文件移动到有大量文件的目的,可以使用 --no-traverse 参数加速。
rclone purge remote:pat
# 删除 path 及其中的所有内容。
rclone mkdir remote:path [flags]
# 如果路径不存在,则创建
rclone rmdir remote:path [flags]
# 删除空的存储桶,如果存储桶非空,可以使用 rclone purge
rclone check source:path dest:path [flags]
# 根据文件大小以及 hash 值对源和目的进行校验(md5和sha1),--size-only 只对比大小;--download 下载下来对比;--one-way,根据源端的数据列表对比; --checksum ,进行 size, checksum 校验;默认,进行大小和修改时间校验。
rclone ls remote:path [flags]
# 查看对象大小和路径;lsl,包括修改时间;lsd,列出目录;lsjson,以 json 格式输出。
rclone lsd remote:path [flags]
# 列出路径下的目录或存储桶
rclone delete remote:path [flags]
# 删除 path 下符合条件的对象
rclone size remote:path [flags]
# 查看远端的文件数目和总大小
rclone rcat remote:path [flags]
# 将标准输出复制到远程文件中

5.开始配置及挂载

输入配置命令

rclone config
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> one

n添加新配置并输入名称

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"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, Tencent COS, etc)
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Citrix Sharefile
   \ "sharefile"
 9 / Dropbox
   \ "dropbox"
10 / Encrypt/Decrypt a remote
   \ "crypt"
11 / FTP Connection
   \ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
13 / Google Drive
   \ "drive"
14 / Google Photos
   \ "google photos"
15 / Hubic
   \ "hubic"
16 / In memory object storage system.
   \ "memory"
17 / Jottacloud
   \ "jottacloud"
18 / Koofr
   \ "koofr"
19 / Local Disk
   \ "local"
20 / Mail.ru Cloud
   \ "mailru"
21 / Mega
   \ "mega"
22 / Microsoft Azure Blob Storage
   \ "azureblob"
23 / Microsoft OneDrive
   \ "onedrive"
24 / OpenDrive
   \ "opendrive"
25 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
26 / Pcloud
   \ "pcloud"
27 / Put.io
   \ "putio"
28 / QingCloud Object Storage
   \ "qingstor"
29 / SSH/SFTP Connection
   \ "sftp"
30 / Sugarsync
   \ "sugarsync"
31 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
32 / Transparently chunk/split large files
   \ "chunker"
33 / Union merges the contents of several upstream fs
   \ "union"
34 / Webdav
   \ "webdav"
35 / Yandex Disk
   \ "yandex"
36 / http Connection
   \ "http"
37 / premiumize.me
   \ "premiumizeme"
38 / seafile
   \ "seafile"
Storage>13

选一个要配置的存储类型我们选 **23** Microsoft OneDrive

client_id>     #可为空直接回车
client_secret    #可为空直接回车
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/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         #这一项我们输入No
For this to work, you will need rclone available on a machine that has
a web browser available.

For more help and alternate methods see: https://rclone.org/remote_setup/

Execute the following on the machine with the web browser (same rclone
version recommended):

        rclone authorize "onedrive"

Then paste the result below:
result>

输入刚才保存的那串token

result>{"access_token":"eyJ0eXAiOiJKVCJ......."expiry":"2020-12-01T12:32:44.1828205+08:00"}
Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
 3 / Type in driveID
   \ "driveid"
 4 / Type in SiteID
   \ "siteid"
 5 / Search a Sharepoint site
   \ "search"

选择1个人

Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!pLoksFAUzkqbocvnloDBAXSLnluli_pPsPDFVtaDDDL6oxT_kK7qQb5Lnhx3J8H0
Chose drive to use:> 0

选择发现的驱动器0

Found drive 'root' of type 'business', URL: https://p5fcw-my.sharepoint.com/personal/lxl_bgp_la/Docu                                                                                                                    ments
Is that okay?
y) Yes (default)
n) No
y/n>

找到类型为business的驱动器y

[one]
type = onedrive
token = {"access_token":"eyJ0eXAiOiJKVCJ......."expiry":"2020-12-01T12:32:44.1828205+08:00"}
drive_id = b!pLoksFAUzkqbocvnloXXXXXLnluli_pPsPDFVtaDSSL6oxT_KK7qQbSCSA3J8H0
drive_type = business
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d>
Current remotes:

Name                 Type
====                 ====
one                  onedrive

y确定配置

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

q退出配置完成

挂载

创建一个用来挂载的目录

mkdir /one

挂载的命令

rclone mount name: /one --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

name是配置名称 /one 是我们刚才创建用来挂载的目录

那我们就输入以下命令来挂载

rclone mount one: /one --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

执行上面的命令会把整个网盘都挂载过来,那有时候误操作把网盘其他文件误删掉就不好了,那我们就先在网盘里创建一个文件夹,挂载到创建的文件夹里这样就不会影响到网盘其他文件

先自行在网盘里创建一个文件夹如,data然后执行如下命令

rclone mount one:data /one --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

这样就只挂载网盘里的 data目录了

执行了挂载命令行终端可能会卡住,关掉重新打开

输入 df -h 来确认

df -h | grep one
one:data            5T  56M   5T   1% /one

5个T有木有,直接成大盘鸡

挂载后就可以像操作本地目录一样操作 One Drive

挂载之后网盘里的文件会同步到挂载目录,要是服务器挂载目录里有文件的话就会被删掉,只会存在网盘里有的文件。 请先确保挂载目录为空,或转移目录里的重要文件。

服务器与One Drive各自生成或创建的文件都会同步
但One Drive删掉文件,服务器里文件还会继续存在
服务器删掉文件,One Drive对应文件也会被删掉

卸载挂载

fusermount -qzu /one

6.开机自动挂载

脚本来源:https://raw.githubusercontent.com/x91270/Centos/master/rcloned

新建脚本文件vim one.sh

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
NAME_BIN="rclone"
### BEGIN INIT INFO
# Provides:          rclone
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start rclone at boot time
# Description:       Enable rclone by daemon.
### END INIT INFO

NAME="OneDrive" #rclone name名
REMOTE='Download' #远程文件夹
LOCAL='/home/OneDrive' #挂载地址

Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
RETVAL=0

check_running(){
	PID="$(ps -C $NAME_BIN -o pid= |head -n1 |grep -o '[0-9]\{1,\}')"
	if [[ ! -z ${PID} ]]; then
		return 0
	else
		return 1
	fi
}
do_start(){
	check_running
	if [[ $? -eq 0 ]]; then
		echo -e "${Info} $NAME_BIN (PID ${PID}) 正在运行..." && exit 0
	else
		fusermount -zuq $LOCAL >/dev/null 2>&1
		mkdir -p $LOCAL
		sudo /usr/bin/rclone mount $NAME:$REMOTE $LOCAL --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 >/dev/null 2>&1 &
		sleep 2s
		check_running
		if [[ $? -eq 0 ]]; then
			echo -e "${Info} $NAME_BIN 启动成功 !"
		else
			echo -e "${Error} $NAME_BIN 启动失败 !"
		fi
	fi
}
do_stop(){
	check_running
	if [[ $? -eq 0 ]]; then
		kill -9 ${PID}
		RETVAL=$?
		if [[ $RETVAL -eq 0 ]]; then
			echo -e "${Info} $NAME_BIN 停止成功 !"
		else
			echo -e "${Error} $NAME_BIN 停止失败 !"
		fi
	else
		echo -e "${Info} $NAME_BIN 未运行"
		RETVAL=1
	fi
	fusermount -zuq $LOCAL >/dev/null 2>&1
}
do_status(){
	check_running
	if [[ $? -eq 0 ]]; then
		echo -e "${Info} $NAME_BIN (PID $(echo ${PID})) 正在运行..."
	else
		echo -e "${Info} $NAME_BIN 未运行 !"
		RETVAL=1
	fi
}
do_restart(){
	do_stop
	do_start
}
case "$1" in
	start|stop|restart|status)
	do_$1
	;;
	*)
	echo "使用方法: $0 { start | stop | restart | status }"
	RETVAL=1
	;;
esac
exit $RETVAL

修改这一处

NAME="OneDrive" #rclone name名
REMOTE='Download' #远程文件夹
LOCAL='/home/OneDrive' #挂载地址

改为

NAME="one" #创建的配置名
REMOTE='data' #网盘里的文件夹 
LOCAL='/one' #服务器本地挂载目录

添加执行权限,并写入开机自启动配置文件

chmod +x one.sh
vim /etc/rc.local
/bin/bash /root/one.sh start

重启reboot再查看挂载上没有

df -h | grep one
one:data        5T   66M   5T   1% /one

设置完成

参考文章:
https://www.misterma.com/archives/860/
https://www.ioiox.com/archives/108.html
https://ccav.me/centos7-installing-rclone-to-mount-onedrive.html

# Rclone 

本文由:星际难民
实践,测试,整理发布.如需转载请注明地址 本文标题:Rclone挂载One Drive将文件同步到云存储
地址:https://530503.xyz/articles/2020/11/28/1606808552061.html

评论

取消