linux挂载google Drive

Others 2019-08-28 05:57:07 2019-08-28 05:57:07 2728 次浏览

安装 Rclone

Linux或macOS有多种安装方式,这里选择脚本安装:

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


install.sh

#!/usr/bin/env bash

error codes

0 - exited without problems

1 - parameters not supported were used or some unexpected error occurred

2 - OS not supported by this script

3 - installed version of rclone is up to date

4 - supported unzip tools are not available

set -e

#when adding a tool to the list make sure to also add it's corresponding command further in the script unzip_tools_list=('unzip' '7z' 'busybox')

usage() { echo "Usage: curl https://rclone.org/install.sh | sudo bash [-s beta]" 1>&2; exit 1; }

#check for beta flag if [ -n "$1" ] && [ "$1" != "beta" ]; then usage fi

if [ -n "$1" ]; then install_beta="beta " fi

#create tmp directory and move to it with macOS compatibility fallback tmp_dir=mktemp -d 2>/dev/null || mktemp -d -t 'rclone-install.XXXXXXXXXX'; cd $tmp_dir

#make sure unzip tool is available and choose one to work with set +e for tool in ${unzip_tools_list[*]}; do trash=hash $tool 2>>errors if [ "$?" -eq 0 ]; then unzip_tool="$tool" break fi done
set -e

exit if no unzip tools available

if [ -z "${unzip_tool}" ]; then printf "\nNone of the supported tools for extracting zip archives (${unzip_tools_list[*]}) were found. " printf "Please install one of them and try again.\n\n" exit 4 fi

Make sure we don't create a root owned .config/rclone directory #2127

export XDG_CONFIG_HOME=config

#check installed version of rclone to determine if update is necessary version=rclone --version 2>>errors | head -n 1 if [ -z "${install_beta}" ]; then current_version=curl https://downloads.rclone.org/version.txt else current_version=curl https://beta.rclone.org/version.txt fi

if [ "$version" = "$current_version" ]; then printf "\nThe latest ${install_beta}version of rclone ${version} is already installed.\n\n" exit 3 fi

#detect the platform OS="uname" case $OS in Linux) OS='linux' ;; FreeBSD) OS='freebsd' ;; NetBSD) OS='netbsd' ;; OpenBSD) OS='openbsd' ;;
Darwin) OS='osx' ;; SunOS) OS='solaris' echo 'OS not supported' exit 2 ;; *) echo 'OS not supported' exit 2 ;; esac

OS_type="uname -m" case $OS_type in x86_64|amd64) OS_type='amd64' ;; i?86|x86) OS_type='386' ;; arm*) OS_type='arm' ;; aarch64) OS_type='arm64' ;; *) echo 'OS type not supported' exit 2 ;; esac

#download and unzip if [ -z "${install_beta}" ]; then download_link="https://downloads.rclone.org/rclone-current-$OS-$OS_type.zip" rclone_zip="rclone-current-$OS-$OS_type.zip" else download_link="https://beta.rclone.org/rclone-beta-latest-$OS-$OS_type.zip" rclone_zip="rclone-beta-latest-$OS-$OS_type.zip" fi

curl -O $download_link unzip_dir="tmp_unzip_dir_for_rclone"

there should be an entry in this switch for each element of unzip_tools_list

case $unzip_tool in 'unzip') unzip -a $rclone_zip -d $unzip_dir ;; '7z') 7z x $rclone_zip -o$unzip_dir ;; 'busybox') mkdir -p $unzip_dir busybox unzip $rclone_zip -d $unzip_dir ;; esac

cd $unzip_dir/*

#mounting rclone to environment

case $OS in 'linux') #binary cp rclone /usr/bin/rclone.new chmod 755 /usr/bin/rclone.new chown root:root /usr/bin/rclone.new mv /usr/bin/rclone.new /usr/bin/rclone #manuals if ! [ -x "$(command -v mandb)" ]; then echo 'mandb not found. The rclone man docs will not be installed.' else mkdir -p /usr/local/share/man/man1 cp rclone.1 /usr/local/share/man/man1/ mandb fi ;; 'freebsd'|'openbsd'|'netbsd') #bin cp rclone /usr/bin/rclone.new chown root:wheel /usr/bin/rclone.new mv /usr/bin/rclone.new /usr/bin/rclone #man mkdir -p /usr/local/man/man1 cp rclone.1 /usr/local/man/man1/ makewhatis ;; 'osx') #binary mkdir -p /usr/local/bin cp rclone /usr/local/bin/rclone.new mv /usr/local/bin/rclone.new /usr/local/bin/rclone #manual mkdir -p /usr/local/share/man/man1 cp rclone.1 /usr/local/share/man/man1/
;; *) echo 'OS not supported' exit 2 esac

#update version variable post install version=rclone --version 2>>errors | head -n 1

printf "\n${version} has successfully installed." printf '\nNow run "rclone config" for setup. Check https://rclone.org/docs/ for more details.\n\n' exit 0

配置 Google Drive

rclone config 
  • 运行rclone
No remotes found - make a new one
n) New remote
r) Rename remote
c) Copy remote
s) Set configuration password q) Quit config 
  • 这里选择 n 创建新的remote
name> lala 
  • name 这里写一个自己简单的自己喜欢的名字就好
Type of storage to configure.
Choose a number from below, or type in your own value
... 11 / Google Drive
   \ "drive" ...
Storage> 12 
  • 根据自己的可用类型,支持很多网盘格式,如 AWS、Dropbox,Google Drive等,这里是 11 Google Drive

  • 接着client_id、client_secret、service_account_file都留空直接回车,scope 选 1 Use auto config?这里我们选择n,如下所示:

Google Application Client Id - leave blank normally.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>
Scope that rclone should use when requesting access from drive.
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 ...
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine or Y didnt work
y) Yes
n) No
y/n> n 
  • 这时有一串 OAuth的链接需要再浏览器里登录Google Drive账号并授权,授权后获得一串返回码,粘贴回到命令行里
  • 接下来分别选择 n y q
Configure this as a team drive?  n
y) Yes this is OK   y
q) Quit config    q 

挂载硬盘

apt-get install fuse
mkdir -p /gdrive
rclone mount lala: /gdrive --allow-other --allow-non-empty --vfs-cache-mode writes 

同步某个目录

# 查看网盘目录 rclone lsd lala:/ # 检查哪些文件会被同步 rclone sync source dest:/path --dry-run # 开始同步,注意同步仅仅是2个目录

下的文件会被同步 rclone sync source dest:/path


自启动

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

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

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

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='' #远程文件夹,Google Drive网盘里的挂载的一个文件夹 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启动成功即可。




	

#将后面修改成你上面手动运行命令中,除了rclone的全部参数


vi /usr/lib/systemd/system/rclone.service
[Unit]
Description=Rclone
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/rclone mount rcloneName:/ /root/onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other  --allow-non-empty --umask 000
Restart = on-abort

开始启动:

systemctl start rclone 

设置开机自启:

systemctl enable rclone 

其他命令:

重启:systemctl restart rclone
停止:systemctl stop rclone
状态:systemctl status rclone