rclone 是一款高效的文件同步、管理工具,支持 Windows、Mac OS、Linux 等主流操作系统。使用 rclone 可以实现本地数据与Google Drive、Onedrive、Dropbox等云盘同步,以达到数据备份的目的。本文详细介绍在 Ubuntu 18.04 上安装 rclone ,并使用 rclone 连接到 Google Drive 进行数据备份。注意,以下操作是在 root 账号下进行的,非 root 账号需提升到 root 权限。

安装 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 配置向导,按照向导,一步一步进行配置:

2018/10/18 13:09:51 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,新建一个remote
name> google-drive  #起个名字,这里以 google-drive 为例,后面同步文件时会用到
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
====                 ====
google-drive         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 进行数据备份。

使用 rclone

rclone 常用命令

  • rclone config - 以控制会话的形式添加rclone的配置,配置保存在.rclone.conf文件中
  • rclone copy - 将文件从源复制到目的地址,跳过已复制完成的
  • rclone sync - 将源数据同步到目的地址,只更新目的地址的数据
  • rclone move - 将源数据移动到目的地址
  • rclone delete - 删除指定路径下的文件内容
  • rclone purge - 清空指定路径下所有文件数据
  • rclone mkdir - 创建一个新目录
  • rclone rmdir - 删除空目录
  • rclone check - 检查源和目的地址数据是否匹配
  • rclone ls - 列出指定路径下所有的文件以及文件大小和路径
  • rclone lsd - 列出指定路径下所有的目录/容器/桶
  • rclone lsl - 列出指定路径下所有文件以及修改时间、文件大小和路径
  • rclone md5sum - 为指定路径下的所有文件产生一个md5sum文件
  • rclone sha1sum - 为指定路径下的所有文件产生一个sha1sum文件
  • rclone size - 获取指定路径下,文件内容的总大小.
  • rclone version - 查看当前版本
  • rclone cleanup - 清空remote
  • rclone dedupe - 交互式查找重复文件,进行删除/重命名操作

复制文件或目录到Google Drive

复制的命令格式如下:

rclone copy /path/to/local/copy remote_name:path/to/files

例如,将本地 /home/download 目录下的 timelate.zip 复制到 Google Drive 的 Sync/backup/ 目录下:

rclone copy /home/download/timelate.zip google-drive:Sync/backup/

例如,将本地 /home/download 目录下的movies目录复制到 Google Drive 的 Sync/backup/ 目录下:

rclone copy /home/download/movies google-drive:Sync/backup/movies

同步文件到Google Drive

同步的命令格式如下:

rclone sync /path/to/local/sync remote_name:path/to/files

例如,将本地 /home/download/movies 目录下的文件同步到到 Google Drive 的 Sync/backup/movies 目录下:

rclone sync /home/download/movies google-drive:Sync/backup/movies

命令执行后默认同时处理4个文件,如果目录下文件较多而你的服务器带宽很大,可以通过 --transfers=N 参数来指定同时处理的文件数,例如:

rclone sync /home/download/movies google-drive:Sync/backup/movies --transfers=8

更多 rclone 命令的用法请参考 rclone 官方文档,文档地址:https://rclone.org/docs/

挂载 Google Drive

挂载远程目录到本地

挂载的命令格式如下:

rclone mount remote_name:path/to/files /path/to/local/mount

例如,将 Google Drive 的 Sync/Videos 目录挂载到本地的 /home/google-drive 目录:

screen -S rclone  #新建一个会话
rclone mount google-drive:Sync/Videos /home/google-drive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
Ctrl+A+D  #退出会话

相关参数解释如下:

  • --copy-links - 显示软链接内容
  • --no-gzip-encoding - 不使用 gzip-encoding
  • --no-check-certificate - 不验证ssl证书
  • --allow-other - 允许其它用户访问
  • --allow-non-empty - 允许挂载目录非空
  • --umask 000 - 覆写文件掩码为 000

更多参数选项请参考官方文档:https://rclone.org/commands/rclone_mount/
挂载完成后使用df -h命令查看挂载情况:

root@timelate:~# df -h
Filesystem                Size  Used Avail Use% Mounted on
udev                      3.9G     0  3.9G   0% /dev
tmpfs                     797M  760K  797M   1% /run
/dev/sda3                 915G  859G   11G  99% /
tmpfs                     3.9G     0  3.9G   0% /dev/shm
tmpfs                     5.0M     0  5.0M   0% /run/lock
tmpfs                     3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1                 189M   63M  113M  36% /boot
tmpfs                     797M     0  797M   0% /run/user/0
google-drive:Sync/Videos  1.0P  1.2T  1.0P   1% /home/google-drive

出现

google-drive:Sync/Videos 1.0P 1.2T 1.0P 1% /home/google-drive

即说明挂载成功。

停止挂载

一般情况下使用Ctrl+C便可停止挂载,如果停止失败,使用如下命令停止挂载:

screen -r rclone  #恢复会话
fusermount -qzu /home/google-drive  #停止挂载

配置服务开机自动挂载

使用服务可以方便的管理 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 ,全文结束。

文章目录