rcloneでGoogle Driveへのリモートバックアップを自動化する【rsyncみたいなやつ】

孤独のIT

こんにちは。初めて自分で買ったHDDは500MBのPC-9800シリーズ用の外付SCSIだったけーのです。あれ、300MBだったかな…まあいいや。

今回はリモートバックアップ先としてGoogle Driveを使い、でっかいバックアップファイルを安全にクラウドに保存してDR(Disaster Recovery)に備えましょう、という話です。

前提: インターネット回線とトラフィック量に注意

でっかいバックアップファイルをクラウドに送るため、オンプレミス環境の場合はアップストリーム方向への制限がゆるいインターネット回線を用意しましょう。こちらのエントリでも書いています。

【できなくなりました】Google Driveを容量無制限で使う方法【1360円/月/人で5人以上】【クラウドストレージ】
2020-10-08追記: G Suite のリブランドとして Google Workspace が発表されました。新しいプランでは、Enterprise以外は無制限ストレージではなくなります。つまり、本記事の方法で無制限ストレージを使う...

バックアップ元データが既にクラウド側にある場合は発生するトラフィック量に注意しましょう。というか、クラウドの場合はそれぞれの環境で適したバックアップ手段が用意されていると思うので、そちらを使う方が良いかも知れません。データ量やかけられるコストを考慮して検討してください。

rclone とは

rcloneはGoで実装されているMITライセンスのオープンソースソフトウェアです。

みなさんrsyncは好きですよね。僕も好きです。

rcloneとかいうステキプロダクトを使うと、Google Driveや他のクラウドストレージサービスへのアップロード・ダウンロードがコマンドベースで、rsync感覚でできるようになります。ひゃっほい。

対応しているクラウドストレージがたくさんあるのでGoogle Driveでなくても使えますし、使っているサービスが使えなくなっても別のクラウドストレージへの乗り換えが簡単そうなところも良いですね。

rclone をインストールする

rcloneのドキュメント に従ってインストールします。一瞬だぜ。

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

いきなり sudo bash でダウンロードしてきたスクリプトを実行すんの怖えなあと思いながらついついやってしまいますね。いや、本当は一回 less で確認しました。本当ですよ。

rclone をセットアップする

初期設定が必要ですが、 rclone config を実行すると親切に色々聞いてくるので答えていくだけで完了します。カンタン。

今回はチームドライブを使う設定を作りましたが、特に難しいところはありませんでした。

$ rclone config
2018/08/09 11:16:28 NOTICE: Config file "/home/kno/.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> gdrive-mis-backup
Type of storage to configure.
Choose a number from below, or type in your own value
1 / Alias for a existing remote
\ "alias"
2 / Amazon Drive
\ "amazon cloud drive"
3 / Amazon S3 Compliant Storage Providers (AWS, Ceph, Dreamhost, IBM COS, Minio)
\ "s3"
4 / Backblaze B2
\ "b2"
5 / Box
\ "box"
6 / Cache a remote
\ "cache"
7 / Dropbox
\ "dropbox"
8 / Encrypt/Decrypt a remote
\ "crypt"
9 / FTP Connection
\ "ftp"
10 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
11 / Google Drive
\ "drive"
12 / Hubic
\ "hubic"
13 / Local Disk
\ "local"
14 / Mega
\ "mega"
15 / Microsoft Azure Blob Storage
\ "azureblob"
16 / Microsoft OneDrive
\ "onedrive"
17 / OpenDrive
\ "opendrive"
18 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
19 / Pcloud
\ "pcloud"
20 / QingCloud Object Storage
\ "qingstor"
21 / SSH/SFTP Connection
\ "sftp"
22 / Webdav
\ "webdav"
23 / Yandex Disk
\ "yandex"
24 / http Connection
\ "http"
Storage> 11
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> drive
ID of the root folder - leave blank normally. Fill in to access "Computers" folders. (see docs).
root_folder_id>
Service Account Credentials JSON file path - leave blank normally.
Needed only if you want use SA instead of interactive login.
service_account_file>
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
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=999999999999.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=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Log in and authorize rclone for access
Enter verification code> 9/XxXxxxx9XxXxXXxxX9xXxXX9xxxXxxxx9XXx9x-9X9XxX-XxxXXXx99
Configure this as a team drive?
y) Yes
n) No
y/n> y
Fetching team drive list...
Choose a number from below, or type in your own value
1 / mis_install
\ "XXXXXXXXXXXXXXXXXXX"
2 / mis-backup
\ "XXXXXXXXXXXXXXXXXXX"
3 / test
\ "XXXXXXXXXXXXXXXXXXX"
4 / 情報システム
\ "XXXXXXXXXXXXXXXXXXX"
Enter a Team Drive ID> 2
--------------------
[gdrive-mis-backup]
type = drive
client_id =
client_secret =
scope = drive
root_folder_id =
service_account_file =
token = {"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","token_type":"Bearer","refresh_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","expiry":"2018-08-09T12:20:04.894937539+09:00"}
team_drive = XXXXXXXXXXXXXXXXXXX
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name Type
==== ====
gdrive-mis-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

今回はheadlessな環境だったので、途中で手元のPCのブラウザでログインして verification code をゲットし、コピペしました。

rclone を許可するためにGoogleへログインする時の画面

rclone を許可するためにGoogleへログインする時の画面

rclone の使い方

こんな感じで使います。

$ rclone ls gdrive-mis-backup:/
$ rclone copy /path/to/dir/ gdrive-mis-backup:/dir/
$ rclone copy gdrive-mis-backup:/dir/ /path/to/dir/

単体のファイルをコピーした時、同名のファイルが存在すると上書きせずにエラーになるなど、rsyncとは異なる挙動もあります。詳しくはrcloneのドキュメントrclone --help を読みつつ試してみましょう。

バックアップは必ず自動化する

挙動を把握したら、適当にバックアップスクリプトに組み込んで cron などでリモートバックアップを自動実行させましょう。手動でのバックアップにすると、人間は絶対にサボります。そしてサボっている時に限って壊れて酷い目に遭います。バックアップは必ず自動化しましょう。

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