飘云飘云

漂泊的云,停留的心!人的头脑太复杂,时间过得久,有时候连自己也被自己骗了,记下来才是最真实的……

在Centos/Ubuntu操作系统上使用rclone挂载OneDrive网盘并且2个网盘之间sync数据同步的教程方法

提示:本教程挂载OneDrive for Business和个人版的方法都一样,这里以个人版为参考。

rclone - rsync for cloud storage

Rclone is a command line program to sync files and directories to and from:Amazon Drive、Amazon S3、Backblaze B2、Box、Ceph、DigitalOcean Spaces、Dreamhost、Dropbox、FTP、Google Cloud Storage、Google Drive、HTTP、Hubic、Jottacloud、IBM COS S3、Memset Memstore、Mega、Microsoft Azure Blob Storage、Microsoft OneDrive、Minio、Nextcloud、OVH、OpenDrive、Openstack Swift、Oracle Cloud Storage、ownCloud、pCloud、put.io、QingStor、Rackspace Cloud Files、SFTP、Wasabi、WebDAV、Yandex Disk、The local filesystem

rclone 看样子可行,VPS 安装 rclone,连接 Google Drive 和 OneDrive,通过 VPS 将 Google Drive 中的文件同步到 OneDrive。

说明

本教程只适用CentOS/Ubuntu系统,如果你是CentOS或者其它Linux系统,请使用以下命令安装rclone:

利用Curl来进行安装Rclone:

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

安装成功则提示

rclone v1.45 has successfully installed.
Now run "rclone config" for setup. Check https://rclone.org/docs/ for more details.

客户端授权、初始化配置和挂载方法和下面一样

客户端授权

在本地Windows电脑上下载rclone,下载地址:https://rclone.org/downloads/。然后解压出来,比如我解压到D盘,文件夹命名rclone,此时点击Win+R,然后输入cmd,确定。再输入以下命令:

cd /d d:\rclone
rclone authorize "onedrive"

会出现以下信息:

C:\Users\piaoyun.cc>cd /d d:\rclone

d:\rclone>rclone authorize "onedrive"
2018/01/23 20:28:56 NOTICE: Config file "C:\\Users\\piaoyun.cc\\.config\\rclone\\rclone.conf" not found - using defaults Choose OneDrive account type?
 * Say b for a OneDrive business account
 * Say p for a personal OneDrive account
b) Business
p) Personal
b/p> b  #这里选择Business版,你想挂载个人版本就选择p
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth  #接下来会弹出浏览器,要求你登录账号进行授权
Log in and authorize rclone for access
Waiting for code...
Got code
Paste the following into your remote machine --->
{"access_token":"xxxx"}  #请复制{xx}整个内容,后面需要用到
<---End paste

01.png

02.png

选择Business版本,并按Y,弹出浏览器后登陆,等待返回 access token.
右键标记全部的 access token,按 CTRL + C 进行复制.(就是 {"access_token": ... }, 两个花括号及中间的部分. )
粘贴到记事本中,整理好.
远端运行rclone config 选择Business版本,并按N,粘贴刚刚复制的 access token.
将{"access_token":"**"}到日期那一行完整地整段复制下来。例如:

{"access_token":"eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFDRWZleFh4amFtUWIzT2VHUTRHdWd2em5rNGtBN284R2R4X21QbEt6YV********************************************************************************************************************************************************************************************************************************************************************************************************190Y2R0IjoxNTM4MDE4NzYxfQ.FD-g0WLV12r
-Ak6lxedpYDPsGt_LF7qzIiE3CJNHs5w5vbLosbL9-cfcT0dxRIAOIbiCTw4_cS_6jEHbIQev6zB6eDMsSjzL7x8Md-j6omgy7bhAVfY4AO_iLu2je_JQq6FBhpvSBvLQRSuIuXX4bEDJ-pCBAFFKpgpWRzNH90iN4KiHEJeKwBT-i8x3R87g2bXHwCRFTPBVvi04gcADXKrwam2JZClW3tO6VbjipEg6E1fcvQEBeGJjOVjn4DK7y_7VV5uG3kRUnUvdVRRww_z537a4HToIZh1nrp7Rr-hSHJEkrNOTPLchjgIYenbd_9QNfLJPPdiwLKt1VK-Ykg","token_type":"Bearer","refresh_token":"OAQABAAAAAACEfexXxjamQb3OeGQ4GugvGPBfOhJu5kfwj**************************************************************************************************************************************************************************************************************************************************************-klf_AWWbqj7PdByqu5QYDVJrGP0FbTd_sbHgA3lcgAA","expiry":
"2019-01-19T23:27:37.5265257+08:00"}

回到SSH,把你复制的Token复制到result中

For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
    rclone authorize "onedrive"
Then paste the result below:
result>           #粘贴你获得的Token

如果失败,会直接退出Rclone,请确认你的Token是否有按照我写的规则填写正确
配置完毕!

Ubuntu/Centos系统安装rclone

本教程只适用Debian/Ubuntu系统,如果你是CentOS,请参考:https://rclone.org/install/,安装rclone。挂载方法和下面差不多。

1、安装rclone

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

2、初始化配置

rclone config

会出现以下信息:

n) New remote
s) Set configuration password
q) Quit config
n/s/q> n  #输入n,创建新的远程连接
name> piaoyun  #给创建的远程连接,取个名字,后面要用到
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 / A stackable unification remote, which can appear to merge the contents of several remotes
   \ "union"
 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, etc)
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Dropbox
   \ "dropbox"
 9 / Encrypt/Decrypt a remote
   \ "crypt"
10 / FTP Connection
   \ "ftp"
11 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
12 / Google Drive
   \ "drive"
13 / Hubic
   \ "hubic"
14 / JottaCloud
   \ "jottacloud"
15 / Koofr
   \ "koofr"
16 / Local Disk
   \ "local"
17 / Mega
   \ "mega"
18 / Microsoft Azure Blob Storage
   \ "azureblob"
19 / Microsoft OneDrive
   \ "onedrive"
20 / OpenDrive
   \ "opendrive"
21 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
22 / Pcloud
   \ "pcloud"
23 / QingCloud Object Storage
   \ "qingstor"
24 / SSH/SFTP Connection
   \ "sftp"
25 / Webdav
   \ "webdav"
26 / Yandex Disk
   \ "yandex"
27 / http Connection
   \ "http"
Storage> 19   #选择上面所列出来的网盘名称,Microsoft OneDrive 是19  我们这里就输入19
** See help for onedrive backend at: https://rclone.org/onedrive/ **

Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>    #留空
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>    #留空
Edit advanced config? (y/n)
y) Yes
n) No
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
n) No
y/n> n  #选择n
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
    rclone authorize "onedrive"
Then paste the result below:
result> {"access_token":""}  #输入之前在客户端授权的内容
--------------------
[Rats]
client_id = 
client_secret = 
token = {"access_token":""}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y  选择y
Current remotes:

Name                 Type
====                 ====
piaoyun                 onedrive

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退出

回到SSH,把你复制的Token复制到result中

For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
    rclone authorize "onedrive"
Then paste the result below:
result>           #粘贴你获得的Token

如果失败,会直接退出Rclone,请确认你的Token是否有按照我写的规则填写正确
成功则如下,输入1即可

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"
Your choice> 1     #输入1

然后会让你选择你想使用的账户的硬盘,选择你想要的即可,我只有一个,直接选择0

Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=*********************************************
Chose drive to use:> 0     #选择你想要挂载的盘

随后会给你一个链接,这个链接重定向到你的OneDrive网页版,复制到浏览器看看是不是你想要添加的即可
然后会让你确认,全选yes即可(y)

Found drive 'root' of type 'business', URL: https://***************************/Documents
Is that okay?
y) Yes
n) No
y/n> y
--------------------
[onedrive]
type = onedrive
token = {"access_token":}
drive_id = *********************************************************
drive_type = business
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

到这里,OneDrive的挂载就完成了

#列出顶级文件夹,(piaoyun 是前面第一步设置的名字,如果配置设置了密码会提示输入密码)
rclone lsd piaoyun:

#列出所有文件
rclone ls piaoyun:

错误提示:

Fatal error: failed to mount FUSE fs: fusermount: exec: "fusermount": executable file not found in $PATH

出现原因:

没有安装 fuse ,自行安装就可以了. 解决办法:

# Debian/Ubuntu
apt-get install fuse
# CentOS
yum install fuse

挂载
1、挂载为磁盘

#新建本地文件夹,路径自己定,即下面的LocalFolder
mkdir /root/OneDrive
#挂载为磁盘
rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

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

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

DriveName为初始化配置填的name,FolderOneDrive里的文件夹,LocalFolder为VPS上的本地文件夹。

如果挂载过程中出现NOTICE: One drive root 'test': poll-interval is not supported by this remote错误,可以无视该错误。

挂载成功后,输入df -h命令查看即可!
rclone_OD.png

2、卸载磁盘
fusermount -qzu LocalFolder

定时执行脚本
如果用来备份或者同步文件,用到的命令可能就两个:rclone_copyrclone_sync

rclone_copy 是复制,配合附加参数 --ignore-existing 可以假装增量备份;

rclone_sync 是源目录同步到目标目录,使两者一致,会删除目标目录多余内容,测试时可以加上 --dry-run,检查会复制和删除那些文件。

yum install screen
screen -S rclone #第一次同步大量数据使用 screen 会话尽可能保护 rclone 执行不被中断
rclone copy gdrive:piaoyun onedrive:piaoyun
rclone sync OneDrive:piaoyun OneDrive1:piaoyun

人工粗略检查了一下,看样子是同步成功了。第一次同步因为数据量可能比较大,需要时间比较长,VPS 流量也会耗掉不少。我实测同步 166G,VPS 耗掉的流量大概是这个两倍。数据量大的话最好在流量充裕的 VPS 上进行。

第一次同步完成,就可以 VPS 上(每天/周/月,按需选择)定时执行脚本自动同步。可以选择使用 rclone_copy --ignore-existing 进行增量备份(没有实测,按官方说明是可以实现),或者使用 rclone sync 同步。

我希望 OneDrive 上的备份与 Google Drive 一致,所以每天定时执行 rclone sync gdrive:piaoyun onedrive:piaoyun

/usr/bin/rclone sync gdrive:piaoyun onedrive:piaoyun >> /root/rclone.log 2>&1

配合 crontab 定时执行,比如每天 4:44 执行一次,日志写到 /root/rclone.log。执行 crontab -e,i,添加下面代码进去,按一下 Esc,然后输入 :wq 回车保存退出。

44 4 * * * /usr/bin/rclone sync gdrive:piaoyun onedrive:piaoyun >> /root/rclone.log 2>&1

注:不需要日志的话把 >> /root/rclone.log 2>&1 去掉。

然后重启一下 crond 服务即可。(service crond restart)

注意:最好执行 service crond status 检查 crond 服务是否启动,service crond start 可手动启动,或者设置开机启动:chkconfig --level 35 crond on。Centos 7 x86_64 实测通过。

自启动
1、下载并编辑脚本
使用命令:

wget https://www.moerats.com/usr/shell/rcloned && nano rcloned

脚本内容如下:

#!/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="" #rclone name名
REMOTE='' #远程文件夹
LOCAL='' #挂载地址

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=""  #rclone name名,及配置时输入的Name
REMOTE=''  #远程文件夹,OneDrive网盘里的挂载的一个文件夹
LOCAL=''  #挂载地址,VPS本地挂载目录

2、设置自启
使用命令:

#Debian系统
apt-get install sudo -y

#设置自启
mv rcloned /etc/init.d/rcloned
chmod +x /etc/init.d/rcloned
update-rc.d -f rcloned defaults
bash /etc/init.d/rcloned start

rclone_GD(2).png
检测信息显示rclone启动成功即可。

最后我们就可以利用OneDrive for Business为Linux VPS增加5TB的空间了。

相关阅读:
使用软件rclone在Windows操作系统上挂载OneDrive为本地硬盘的操作方法 - 飘云 - http://piaoyun.cc/1290.html

参考资料:
利用Rclone在VPS上挂载OneDrive/GoogleDrive硬盘 - 風切り - https://zoi.red/archives/52.html
使用 rclone 将 Google Drive 文件同步至 OneDrive | 常阳时光 - https://cyhour.com/825/
Rclone使用指南 | Lain's Blog - https://softlns.github.io/2016/11/28/rclone-guide/
在Debian/Ubuntu上使用rclone挂载OneDrive网盘 - Rat's Blog - https://www.moerats.com/archives/491/
[ 教程 ] 在 Linux VPS 上利用 rclone 挂载 OneDrive 网盘 | 萌咖 - https://moeclub.org/2018/01/23/545/?spm=55.8

当前页面:飘云 » 在Centos/Ubuntu操作系统上使用rclone挂载OneDrive网盘并且2个网盘之间sync数据同步的教程方法

评论