rcloneでgoogle drive活用

google driveの自分の利用領域へlinuxのフォルダを同期コピーさせる。
rcloneというのはコマンドラインのことで、google drive以外にもawsとかazureとか同期処理ができるらしい。

性能はcyberduckのコピーよりもかなり速い(cyberduckは検算もしてるし丁寧ではあるが・・・)。
手元PCの中に100GBのzipファイルが2つ3つあったとして、9:00に初めたら昼には終わる。

Google Cloudの中からだったらもっと速い。転送レートはローカルの2.5~3倍は出る。
いつか壊れるバックアップ置き場をローカルで数TB確保するより、壊れないし速いしずっといい。

rcloneのインストールとmanの展開

curlで取ってきて/usr/binに展開しmandbに追加

$ cd
$ mkdir rclone
$ cd rclone
$ curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 12.3M  100 12.3M    0     0  8717k      0  0:00:01  0:00:01 --:--:-- 8717k
$ ls
rclone-current-linux-amd64.zip
$ unzip rclone-current-linux-amd64.zip
Archive:  rclone-current-linux-amd64.zip
   creating: rclone-v1.53.3-linux-amd64/
  inflating: rclone-v1.53.3-linux-amd64/README.html
  inflating: rclone-v1.53.3-linux-amd64/rclone.1
  inflating: rclone-v1.53.3-linux-amd64/rclone
  inflating: rclone-v1.53.3-linux-amd64/README.txt
  inflating: rclone-v1.53.3-linux-amd64/git-log.txt
$ ls
rclone-current-linux-amd64.zip  rclone-v1.53.3-linux-amd64
$ cd rclone-v1.53.3-linux-amd64/
$ ls
git-log.txt  rclone  rclone.1  README.html  README.txt
$ ls -l
total 40200
-rw-r--r-- 1 nari docker     1935 Nov 20 02:13 git-log.txt
-rwxr-xr-x 1 nari docker 37654528 Nov 20 02:15 rclone
-rw-r--r-- 1 nari docker  1177819 Nov 20 02:04 rclone.1
-rw-r--r-- 1 nari docker  1282524 Nov 20 02:04 README.html
-rw-r--r-- 1 nari docker  1037677 Nov 20 02:04 README.txt
$ sudo cp rclone -p /usr/bin/
$ sudo chown root:root /usr/bin/rclone
$ sudo chmod 755 /usr/bin/rclone
$ mkdir -p /usr/local/share/man/man1
$ sudo cp rclone.1 /usr/local/share/man/
$ sudo mandb
$ rclone version
rclone v1.53.3
- os/arch: linux/amd64
- go version: go1.15.5
$

rcloneするosユーザの作成

googleアカウントとdriveは事前に作っておく。
ここではgvisDriveとして識別させてる。

ローカルlinuxではgavannitsalesをosユーザとして作って連携させる。
作成したユーザからrcloneを行う。

rcloneの設定は${HOME}/.config/rclone/rclone.confとしてテキストで格納されるのでどんな内容なのかも見える。

途中でgoogleアカウントのURLへアクセスしてAPI許可設定をつける。

実際にcrontabで定期的に処理させるときは、rootからこの作成したユーザにsudoして実行する。
こうしておくと定期スケジュール処理をrootに集約させることができ、他の処理とカチあわないかという考慮もやりやすい。

# mkuseradd gavannitsales
# su - gavannitsales 
$ whoami 
gavannitsales
$ 
$ which rclone 
/usr/bin/rclone
$ rclone config 
2020/12/08 07:16:32 NOTICE: Config file "/home/gavannitsales/.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
name> gvisDrive
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 / 1Fichier
   \ "fichier"
 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, Tencent COS, etc)
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Citrix Sharefile
   \ "sharefile"
 9 / Dropbox
   \ "dropbox"
10 / Encrypt/Decrypt a remote
   \ "crypt"
11 / FTP Connection
   \ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
13 / Google Drive
   \ "drive"
14 / Google Photos
   \ "google photos"
15 / Hubic
   \ "hubic"
16 / In memory object storage system.
   \ "memory"
17 / Jottacloud
   \ "jottacloud"
18 / Koofr
   \ "koofr"
19 / Local Disk
   \ "local"
20 / Mail.ru Cloud
   \ "mailru"
21 / Mega
   \ "mega"
22 / Microsoft Azure Blob Storage
   \ "azureblob"
23 / Microsoft OneDrive
   \ "onedrive"
24 / OpenDrive
   \ "opendrive"
25 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
26 / Pcloud
   \ "pcloud"
27 / Put.io
   \ "putio"
28 / QingCloud Object Storage
   \ "qingstor"
29 / SSH/SFTP Connection
   \ "sftp"
30 / Sugarsync
   \ "sugarsync"
31 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
32 / Transparently chunk/split large files
   \ "chunker"
33 / Union merges the contents of several upstream fs
   \ "union"
34 / Webdav
   \ "webdav"
35 / Yandex Disk
   \ "yandex"
36 / http Connection
   \ "http"
37 / premiumize.me
   \ "premiumizeme"
38 / seafile
   \ "seafile"
Storage> 13
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> 372924770615-XXXXXXXXXXXXXXX.apps.googleusercontent.com
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> XXXXXXXXXXXXXXXXXXXXXXXXXX
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
ID of the root folder
Leave blank normally.

Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.

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.

Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.

Enter a string value. Press Enter for the default ("").
service_account_file> 
Edit advanced config? (y/n)
y) Yes
n) No (default)
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
y) Yes (default)
n) No
y/n> n
Please go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Log in and authorize rclone for access
Enter verification code> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Configure this as a team drive?
y) Yes
n) No (default)
y/n> n
--------------------
[gvisDrive]
type = drive
client_id = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
client_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
scope = drive
token = {"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","token_type":"Bearer","refresh_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","expiry":"2020-12-08T08:20:02.666183118+09:00"}
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name                 Type
====                 ====
gvisDrive            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
$ 

ローカルlinuxからrcloneでGoogle Driveをls

00_vmTool.pngという画像ファイルが入っているだけのgoogle driveがあるとすると、
rcloneでlsしたときはこう見える。

$ rclone ls gvisDrive :
    17329 00_vmTool.png

google driveのリスト表示

ファイル一覧を表示するスクリプト。
rcloneの出力結果は、サイズ・日時・パスとファイル名って出力される。

なんとなく読みにくいので、パスとファイル名・日時・サイズってawkとsortで加工。

フォルダ同期の差分リストを得るためにリスト出力している。
googleの調子が悪いとTMP_LSTが変に残ることがあるが、2020年に入ってからは安定。

$ cat 001_DirList.sh
## -------------------------------------------------------------------------
## Script Name  :
##  Created by  : T.Naritomi
##          on  : 2020.12.08
##  Updated by  :
##          on  :
##  Parameters  :
##  Return Code : 0=Normal End
##     Comments : https://rclone.org/commands/rclone_ls/
## -------------------------------------------------------------------------

## ---detail----------------------------------------------------------------

HOME=/gvis/script/gdr-gavannitsales/
CONF=/home/gavannitsales/.config/rclone/rclone.conf

TMP_LST=${HOME}$$.txt
OUT_LST=${HOME}`basename $0.txt`
ORG_LST=${HOME}100_CHKdiff.sh.txt.ORG

rclone --config ${CONF} lsl gvisDrive:/ > $TMP_LST
RC=$?

if [ $RC -eq 0 ]; then
      cat $TMP_LST | awk '{print $4, $2"_"$3, $1}' | sort -f -k 1,1 >> $OUT_LST
  else
        cp -pf $ORG_LST $OUT_LST
    fi

    rm $TMP_LST

    exit $RC
$ ls
001_DirList.sh  001_DirList.sh.txt
$ cat 001_DirList.sh.txt 
00_vmTool.png 2020-12-06_11:31:37.521000000 17329

google driveへのフォルダ同期(呼ばれる側)

ローカルのフォルダをgoogle driveへ同期するスクリプト。
以前に実験的に作ったものを毎日の利用に耐えるよう書き換え。

後続の処理から呼ばれるようになってる。
HideFlagは同期したくないフォルダがあったのでテンポラリ処理に使ってた。
何でもかんでもGoogleに預けるのはどうかと思うので、フォルダ構成を見直して今は未使用。

今ではローカルlinuxだけでなくクラウドの中のlinuxでも基礎処理の一部として使ってる。

おかげでクラウドで作ったdockerの永続化領域をそのままローカルlinuxに持ってきて、mariadbとファイル類をそのまま使えるようになった。

$ cat 002_SyncFolder.sh
#!/bin/sh

## -------------------------------------------------------------------------
## Script Name  :
##  Created by  : T.Naritomi
##          on  : 2019.02.27
##  Updated by  :
##          on  :
##  Parameters  : ex) $1 = HideFlag( None=0 Hide=1 )
##                    $2 = /nari/nariHTTP/configBackup/_stock
##                    $3 = /configBackup/_stock
##  Return Code : 0=Normal End
##     Comments : https://rclone.org/commands/rclone_sync/
## -------------------------------------------------------------------------

## ---detail----------------------------------------------------------------

HOME=/gvis/script/gdr-gavannitsales/
CONF=/home/gavannitsales/.config/rclone/rclone.conf
LOG=/gvis/log/004_gdr.log

TMP_LST=${HOME}$$.txt

###if [ $1 -eq 1 ]; then
###  /bin/sh ${HOME}011_TempMove.sh
###fi

rclone --config ${CONF} \
           sync $2 \
                  gvisDrive:$3 > $TMP_LST
RC=$?

if [ $RC -eq 0 ]; then
  echo $3 ' Sync Success...' `date +%F_%T` >> $LOG
else
  echo $3 ' Sync Failed ...' $RC `date +%F_%T` >> $LOG
fi

cat $TMP_LST
cat $TMP_LST >> $LOG

###if [ $1 -eq 1 ]; then
###  /bin/sh ${HOME}012_TempBack.sh
###fi

rm -f $TMP_LST

exit $RC

google driveへのフォルダ同期(呼ぶ側)

ローカルのフォルダをgoogle driveへ同期するスクリプト。
crontabに書いて定期呼び出したり、tera termのマクロにコマンドライン呼び出しするように書いておいて、shutdown前に動くのはこの処理。

google drive上にGCPってフォルダを作っておき、ローカルlinuxに作ったzipファイルをアップロードする。

このzipフォルダは2つの処理を経てからアップロードする。

  1. ローカルlinuxのバックアップ対象をrsyncで同期
  2. 暗号化パスワードつきzipで簡単に開けないように圧縮(基礎設定ファイル、基礎スクリプト、ログ、業務ファイル、dockerの永続化領域)
$ cat 003_SyncGavannitsales.sh
#!/bin/sh

## -------------------------------------------------------------------------
## Script Name  :
##  Created by  : T.Naritomi
##          on  : 2020.12.08
##  Updated by  :
##          on  :
##  Parameters  :
##  Return Code : 0=Normal End
##     Comments :
## -------------------------------------------------------------------------

## ---detail----------------------------------------------------------------
LOG=/gvis/log/004_gdr.log
chmod 666 ${LOG}

echo ----Start Sync `date +%F_%T` ----- >> $LOG

sudo -u gavannitsales /bin/sh /gvis/script/gdr-gavannitsales/002_SyncFolder.sh 1 \
    /gvis/nari/nariDocs/Docker/nariDockerDat/cl_ubun18/backup/gvis_conf.zip \
    /GCP

sudo -u gavannitsales /bin/sh /gvis/script/gdr-gavannitsales/002_SyncFolder.sh 1 \
    /gvis/nari/nariDocs/Docker/nariDockerDat/cl_ubun18/backup/log.zip \
    /GCP

sudo -u gavannitsales /bin/sh /gvis/script/gdr-gavannitsales/002_SyncFolder.sh 1 \
    /gvis/nari/nariDocs/Docker/nariDockerDat/cl_ubun18/backup/script.zip \
    /GCP

sudo -u gavannitsales /bin/sh /gvis/script/gdr-gavannitsales/002_SyncFolder.sh 1 \
    /gvis/nari/nariDocs/Docker/nariDockerDat/cl_ubun18/backup/nariDocs.zip \
    /GCP

sudo -u gavannitsales /bin/sh /gvis/script/gdr-gavannitsales/002_SyncFolder.sh 1 \
    /gvis/nari/nariDocs/Docker/nariDockerDat/cl_ubun18/backup/Docker.zip \
    /GCP

echo ----End Sync  `date +%F_%T` ----- >> $LOG

その後

Googleから「Oauthは使えへんようになるで」って案内が来た。

September 5, 2022: A user-facing warning message may be displayed to non-compliant OAuth requests
October 3, 2022: The OOB flow is blocked for all clients and users will see the error page.

Please check out our recent blog post about Making Google OAuth interactions safer for more information.

OAuth便利やったのに。

しゃぁないから、ubuntu22に切り替えるとき、clientIDは何も入力せずに使うことにしたけど、思ったほどパフォーマンス下がってへんかった。

タイトルとURLをコピーしました