如何使用rclone挂载Onedrive国际版云盘

前言

这个东西的用处在于给你的VPS增加1T的空间,当然推荐是非国内VPS使用,因为国内的世纪互联版的Onedrive不支持第三方应用绑定,所以,很烦,只能使用国际版的服务器,这样的话是有一点点卡的,得注意这个缺点,别的都还好,就相当于把一个云盘挂载到了本地使用

用途:

公用/私有云盘
公用/私有图床
等等需要大空间的地方
可以同时挂载多个Onedrive云盘,你要是有多个帐号就相当于可以拓展多个5T的硬盘
搭建教程
当前服务器位centos7,其他环境暂时没测试需自测
首先我们需要安装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.

或者你也可以自己下载安装包安装
Rclone官网:https://rclone.org/downloads/

设置

安装完成后,开始添加挂载
输入rclone config来开始设置
首先添加一个新的挂载

[root@ ~]# rclone config
2019/01/19 08:31:26 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n     #选择n添加一个新的挂载
name> googledrive   #输入新挂载的名字
然后他会让我们选择硬盘类型
OneDrive就选择MicroSoft OneDrive
Google Drive就选择Google Drive
选择谷歌云端硬盘的时候注意不要错选成Google Cloud storage

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 a existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Providers (AWS, Ceph, Dreamhost, IBM COS, Minio)
   \ "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 / Local Disk
   \ "local"
16 / Mega
   \ "mega"
17 / Microsoft Azure Blob Storage
   \ "azureblob"
18 / Microsoft OneDrive
   \ "onedrive"
19 / OpenDrive
   \ "opendrive"
20 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
21 / Pcloud
   \ "pcloud"
22 / QingCloud Object Storage
   \ "qingstor"
23 / SSH/SFTP Connection
   \ "sftp"
24 / Webdav
   \ "webdav"
25 / Yandex Disk
   \ "yandex"
26 / http Connection
   \ "http"
Storage> 12   #根据自己要添加的硬盘类型选择
** See help for drive backend at: https://rclone.org/drive/ **

谷歌云盘

两种的挂载方式不一样,这里就分开写,按需求选择阅读

跳过client的选项,并且给Rclone全局管理

Google Application Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>      #略过不填,直接回车
Google Application Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>      #略过不填
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).
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
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 or Y didn't work
y) Yes
n) No
y/n> n     #不自动设置

这时会给你一个链接,复制到浏览器打开,登陆你想要挂载的账号即可。登陆后会给你一串Token,复制到命令行即可

If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=********************************  #复制此处链接到浏览器打开
Log in and authorize rclone for access
Enter verification code> 4/1w**********************-keP-8j5T-B*************  #输入密钥
Configure this as a team drive?
y) Yes
n) No
y/n> n     #不挂载团队硬盘

确认后就成功挂载

[googledrive]
type = drive
scope = drive
token = {"access_token":"ya29.G**********************************************************************************","token_type":"Bearer","refresh_token":"1/if9***************************5Mwu7dO-dMxq745g","expiry":"2019-01-19T09:32:31.189404582-05:00"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y   #确认
Current remotes:

Name                 Type
====                 ====
googledrive          drive
OneDrive
前面这几个步骤跟GoogleDrive一样

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

接下来,我们需要下载Windows的Rclone

64位
https://downloads.rclone.org/v1.45/rclone-v1.45-windows-amd64.zip

32位
https://downloads.rclone.org/v1.45/rclone-v1.45-windows-386.zip
选择对应版本下载并解压,然后将文件放入C:\Users\用户名。然后打开CMD,执行下面的命令

rclone authorize "onedrive"

这时会自动打开你的浏览器,登陆你的OneDrive账号即可
成功后浏览器会显示sucess,CMD窗口会有一串Token

将{“access_token”:”****”}到日期那一行完整地整段复制下来。

例如:
{“access_token”:”eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFDRWZleFh4amFtUWIzT2VHUTRHdWd2em5rNGtBN284R2R4X21QbEt6YV****,”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是否有按照我写的规则填写正确
成功则如下,输入1即可

ONEDRIVE

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的挂载就完成了

挂载硬盘
不管你选择了哪种硬盘,添加完毕后,我们都需要将它挂载到磁盘

#新建本地文件夹
mkdir /root/Drive
#挂载为磁盘
rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 2>&1 &

DriveName是你上面填的name,Folder为Google Drive里的文件夹,LocalFolder为VPS上你刚刚创建的文件夹
示例:

rclone mount googledrive:download /root/Drive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 2>&1 &
rclone mount onedrive:attachment /home/wwwroot/bbs.wutan.cn/data/attachment --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 2>&1 &
rclone mount onedrive:images /home/wwwroot/photo.wutan.cn/images --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 2>&1 &

这里有个坑,就是你挂载为磁盘后,他就卡住不动了,其实是已经成功挂载了。但你无法操作,这里建议利用screen在后台进行挂载

安装screen

Centos

yum -y install screen

ubuntu/debian

sudo apt-get install screen

新建窗口

screen -a googledrive
这样就新建了一个名为googledrive的screen窗口,在里面执行挂载硬盘的命令即可

挂载成功后,输入df -h可以查看分区情况

如需卸载磁盘,则使用以下命令

fusermount -qzu LocalFolder

常见问题FAQ
挂载硬盘后提示NOTICE: One drive root ‘test’: poll-interval is not supported by this remote
解决方法:略过即可
挂载硬盘后提示
Fatal error: failed to mount FUSE fs: fusermount: exec: “fusermount”: executable file not found in $PATH
解决方法:安装FUSE即可

# Centos 
yum install fuse #大多数不成功主要是这个问题
# Debian/Ubuntu
apt-get install fuse

mkdir /one_test && chmod -R 777 /one_test
wget https://www.moerats.com/usr/shell/rcloned && vi rcloned

自动启动

修改以下内容:

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

接下来:

mv rcloned /etc/init.d/rcloned
chmod +x /etc/init.d/rcloned
bash /etc/init.d/rcloned start  #理论上也可以使用 service rcloned start 来启动
systemctl enable rcloned

这样就算完成了,就可以测试下你本地往那个目录移动文件是否会同步到Onedrive云盘了

查看挂载

mount | grep rclone

解绑

rclone可以把one drive、google drive、dropbox等云盘挂载为本地磁盘。

挂载命令:rclone mount DriveName:Folder LocalFolder

卸载命令:fusermount -qzu LocalFolder

验证挂载是否成功:

df -h
zip –q –r xahot.zip /home/wwwroot/xahot

设置自启动(Ubuntu18.04)

关于自启动,网上有很多博客,但都存在一点小问题。按照网上的方法来创建自启动服务,不管是systemd还是init.d,自启动时挂载都失败,但是手动启动就可以启动。
这是因为我创建rclone的时候不是使用root账号,rclone的配置文件保存在/.config/rclone/rclone.conf,而开机的时候rclone无法找到这个配置文件,所以导致启动失败。
然而用户登录后,shell就可以获取到用户目录,从而rclone可以获取
/.config/rclone/rclone.conf的配置文件,所以对与开机自启动的脚本中的命令,需要加上–config选项。

init.d方法

使用init服务管理rclone的开机自动挂载。在/etc/init.d目录下创建rcloned文件

sudo vim /etc/init.d/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="msOneDrive" # rclone name 名,这里需与自己设置的 remote_name 相一致,本示例填写 msOneDrive

REMOTE='VPS/remote' #远程文件夹路径,本示例填写 VPS/remote
LOCAL='/VPS' #挂载地址,本示例填写 /VPS
CONFIG='/home/trobr/.config/rclone/rclone.conf' # 配置文件的目录 通过find ~ | grep rclone.conf查找

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
count=1


LOG_FLODER='/home/trobr/.log/rclone'
LOG_FILE='/home/trobr/.log/rclone/init.log'
RUN_LOG='/home/trobr/.log/rclone/run.log'
-mkdir -p $RUN_LOG
-rm -f $LOG_FILE
-rm -f $RUN_LOG
echo -e "init with $1" >> $LOG_FILE

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
        # 这里添加了配置--config
        /usr/bin/rclone mount $NAME:$REMOTE $LOCAL --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 -vv --config $CONFIG >$RUN_LOG 2>&1 &
        sleep 2s
        check_running
        if [[ $? -eq 0 ]]; then
            echo -e "${Info} $NAME_BIN 启动成功 !" >> $LOG_FILE
        else
            # echo -e "${Error} $NAME_BIN 启动失败 !"
            echo -e "启动失败,第$count次重启 !" >> $LOG_FILE
            let 'count += 1'
            do_start
        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

系统服务法

新建一个rclone.server文件,
Ubuntu18中:

vi /lib/systemd/system/rclone.service

写入以下内容:

[Unit]
Description=rclone

[Service]
User=root
ExecStart=/usr/bin/rclone mount DriveName:RemoteFolder LocalFolder --allow-other --allow-non-empty --vfs-cache-mode writes --config /home/br/.config/rclone/rclone.conf
Restart=on-abort

[Install]
WantedBy=multi-user.target
注意修改DriveName、RemoteFolder和LocalFolder的值
然后重载daemon,使新的服务生效:

systemctl daemon-reload
设置开机启动

rclone启动:
systemctl start rclone
开机启动:
systemctl enable rclone

文章作者: 你来我网
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 你来我网 !
 上一篇
17 useful rsync (remote sync) Command Examples in Linux 17 useful rsync (remote sync) Command Examples in Linux
As the name suggests, rsync command is used to sync (or copy) files and directories locally and remotely. One of the imp
2019-11-11 你来我网
下一篇 
加密插件hexo-encrypt 加密插件hexo-encrypt
加密插件hexo-encrypt使用方法首先输入命令: npm install –save hexo-blog-encrypt 等待安装完成后,修改博客配置文件_config.yml。在末尾添加: encrypt: password:
2019-09-13 你来我网
  目录