磨米

Ubuntu安装 rclone 并自动挂载 Google Drive
概述Rclone是一款的命令行工具,支持在不同对象存储,网盘间同步,上传,下载数据。支持的主流对象存储:谷歌硬盘亚...
扫描右侧二维码阅读全文
09
2019/01

Ubuntu安装 rclone 并自动挂载 Google Drive

5c1cb2ef855d2.jpg

概述
Rclone是一款的命令行工具,支持在不同对象存储,网盘间同步,上传,下载数据。

支持的主流对象存储:

谷歌硬盘
亚马逊S3
Openstack Swift / Rackspace云文件/ Memset Memstore
Dropbox
谷歌云存储
亚马逊驱动器
Microsoft One Drive
Hubic
Backblaze B2
Yandex磁盘
本地文件系统

本文列举的Ubuntu系统,其他的其他请参考官网教程,下方有列出!
安装方法:

安装 rclone

这里推荐使用官方的脚本安装 rclone 。官方安装说明请参考网址:https://rclone.org/install/

安装依赖软件:

apt update && apt install curl unzip -y

安装 rclone:

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

脚本之行完毕出现以下字样即说明 rclone 已成功安装:

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

配置 rclone

输入以下命令开始配置 rclone 以连接到Google Drive:

rclone config

命令执行后会出现 rclone 配置向导,按照向导,一步一步进行配置:

n) New remote
s) Set configuration password
q) Quit config
n/s/q> n  #输入n,新建一个remote
name> momo  #起个名字,这里以 momo 为例,后面同步文件时会用到
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  #输入12,选择 Google Drive
** See help for drive backend at: https://rclone.org/drive/ **

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,Full access
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 #输入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 #输入n,不使用自动配置
#回车之后会出现一串网址,复制网址到电脑浏览器打开,登陆自己的谷歌账号,获得授权code,复制此code
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=3bnvsdf3514f98710fe9c9ef38770lkmxa
Log in and authorize rclone for access
Enter verification code> 4/fABXKI1n3Q6Go5k28QJWLgruilsmszhRf8w9bRRqFVihPb08lpddbw  #粘贴code,回车
Configure this as a team drive?
y) Yes
n) No
y/n> n  #输入n,不配置为团队硬盘
--------------------
[google-drive]
type = drive
scope = drive
token = {"access_token":"ya29.Gls6Bs6JkTo3HCfXKpeUv-Hg9uq9QXPZ-itJum2cv2xDAeUTUp15qAlh2FVrGtuDhpRyBp-6bx0dMPj","token_type":"Bearer","refresh_token":"1/4zPbnV-XGj3Ld_Rggr6E5_2mzSu5Cu4qkw_OZ8e-pgDulS78OZka","expiry":"2018-10-18T14:13:46.118120025Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y  #输入y,确认
Current remotes:

Name                 Type
====                 ====
momo         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  #输入q,退出配置

通过以上步骤 rclone 便配置好了,已经可以连接到 Google Drive 进行数据备份。

挂载为磁盘:

#新建本地文件夹,路径自己定,即下面的LocalFolder
mkdir /root/momoo
#挂载为磁盘
rclone mount momo: /root/momoo --allow-other --allow-non-empty --vfs-cache-mode writes

挂载成功不会有任何提示,会有卡住的情况,这个时候,直接关掉链接就可以了!重新连接SSH输入df -h

TIM截图20190109015619.png

出现自己设置的name即代表成功!

配置服务开机自动挂载:
使用服务可以方便的管理 rclone 的挂载和卸载以及开机自动挂载,以下为服务脚本样例,供参考。

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="google-drive" # rclone name 名,这里需与自己设置的 remote_name 相一致,本示例填写 google-drive
REMOTE='Sync/Videos' #远程文件夹路径,本示例填写 Sync/Videos
LOCAL='/home/google-drive' #挂载地址,本示例填写 /home/google-drive

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

授予执行权限:

chmod +x /etc/init.d/rcloned

初始化服务:

update-rc.d -f rcloned defaults

使用如下命令启动挂载服务:

/etc/init.d/rcloned start

使用如下命令停止挂载服务:

/etc/init.d/rcloned stop

使用如下命令重启挂载服务:

/etc/init.d/rcloned restart

使用如下命令查看服务状态:

/etc/init.d/rcloned status

通过以上步骤便可在 Ubuntu 18.04 手动安装 rclone 并自动挂载 Google Drive ,全文结束。

5c14b24e53d0c.jpg

Last modification:January 9th, 2019 at 01:58 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment