EatSmartシステム部ブログ

ウェブサイトの開発や運営に関する情報です。

rcloneによるファイルアーカイブ

以前、以下の記事

eatsmart.hatenablog.com

で、Googleドライブをストレージとして使うためにgoogle-drive-ocamlfuseやrcloneを試したのですが、最終的にrcloneを使うことにしたので、セットアップについて記事にしたいと思います。

インストールについては、以下のページに書いてある通りのコマンドでできました。

rclone.org

$ sudo -v ; curl https://rclone.org/install.sh | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4669  100  4669    0     0   3767      0  0:00:01  0:00:01 --:--:--  3768
Archive:  rclone-current-linux-amd64.zip
   creating: rclone-v1.60.1-linux-amd64/
  inflating: rclone-v1.60.1-linux-amd64/git-log.txt
  inflating: rclone-v1.60.1-linux-amd64/README.txt

Updating index cache for path `/usr/local/man/man1'. Wait...done.
Checking for stray cats under /usr/local/man...
Checking for stray cats under /var/cache/man/oldlocal...
1 man subdirectory contained newer manual pages.
1 manual page was added.
0 stray cats were added.
0 old database entries were purged.

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

そして、設定をします。

$ rclone config
Current remotes:

Name                 Type
====                 ====

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> n

Enter name for new remote.
name> google-drive_backup

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
 2 / Akamai NetStorage
   \ (netstorage)
・
・
・
18 / Google Drive
   \ (drive)
・
・
・
Storage> 18

Option client_id.
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 value. Press Enter to leave empty.

ここでGoogleドライブのクライアントID(とOAuthのsecret)を作ります。 以下のページ

rclone.org

に作り方が載っていますが、以下の方が参考になりました。

www.wave440.com

GoogleCloudConsoleで作ったIDとsecretを入力します。

client_id> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Option scope.
Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value.
Press Enter to leave empty.
 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

Option service_account_file.
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 value. Press Enter to leave empty.
service_account_file>

Edit advanced config?
y) Yes
n) No (default)
y/n> n

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

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
    rclone authorize "drive" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Then paste the result.
Enter a value.

ここで、Googleドライブへのアクセスを認証するtokenを入力します。 OAuth認証が必要なため、上に表示されているrclone authorizeコマンドをサーバーではなくブラウザが使える環境(手元のPCにrcloneを入れるなど)で実行します。

途中でブラウザでのOAuth認証が実行され、以下のようになります。

mypc$ rclone authorize "drive" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
2022/12/16 17:41:12 NOTICE: Config file "/xxx/xxxx/xxx/rclone.conf" not found - using defaults
2022/12/16 17:41:12 NOTICE: Make sure your Redirect URL is set to "http://127.0.0.1:53682/" in your custom config.
2022/12/16 17:41:12 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=XXXXXXXXX
2022/12/16 17:41:12 NOTICE: Log in and authorize rclone for access
2022/12/16 17:41:12 NOTICE: Waiting for code...
2022/12/16 17:41:23 NOTICE: Got code
Paste the following into your remote machine --->
[TOKEN]
<---End paste

上記tokenをサーバーに貼り付けます。

config_token> [TOKEN]

Configure this as a Shared Drive (Team Drive)?

y) Yes
n) No (default)
y/n> n

Configuration complete.
Options:
- type: drive
- client_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- client_secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- scope: drive
- token: {XXXXXXXXXXXXXXXXXXXXXXXXXXXX}
- team_drive:
Keep this "google-drive_backup" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Current remotes:

Name                 Type
====                 ====
google-drive_backup  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

これでrcloneコマンドでGoogleドライブに接続ができます。

さらに、共有ドライブに接続するために、設定ファイル~/.config/rclone/rclone.confの該当項目にドライブID(team_drive)を設定します。 ドライブIDはドライブURLのhttps://drive.google.com/drive/folders/以下の部分です。

$ vi .config/rclone/rclone.conf
[google-drive_backup]
type = drive
client_id = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
client_secret = XXXXXXXXXXXXXXXXXXXXXXX
scope = drive
token = {XXXXXXXXXXXXXXXXXXXXXXXXXXXX}
team_drive = [ドライブID]

以上で、rcloneのコマンドが使えるようになります。

$ rclone lsd google-drive_backup:
          -1 2022-12-16 00:00:00        -1 backup

これで、rclone copyなどでファイルを移してアーカイブできます。