ふり返る暇なんて無いね

日々のメモ書きをつらつらと。メインブログに書くほどでもないことを流してます

GCSのバケットをすべてコピーしたい(同期したい)

Google Cloud Storageのバケットのリージョンは作成後に変更できません。 そのため、シングルリージョンで作ったバケットをデュアルリージョンまたはマルチリージョンに変えるためには新規で作成し、オブジェクトを全部新しいバケットにコピーした上で切り替えないといけません。 そのためのオブジェクト同期の方法について考えます。

ドキュメントで紹介されている例は転送ジョブとgcloud storage cp gsutil cp ですが、これだとコピーはできますが、バケットの中身を同じにはできません。ここで別解として gsutil rsync を使います。

コマンド例としては以下の通りになります。

gsutil -m rsync -r -d gs://${転送元バケット名} gs://${転送先バケット名}

The -m option typically will provide a large performance boost if either the source or destination (or both) is a cloud URL. If both source and destination are file URLs the -m option will typically thrash the disk and slow synchronization down.

-mオプションはパフォーマンスの向上のため

The rsync -d option is very useful and commonly used, because it provides a means of making the contents of a destination bucket or directory match those of a source bucket or directory. This is done by copying all data from the source to the destination and deleting all other data in the destination that is not in the source. Please exercise caution when you use this option: It's possible to delete large amounts of data accidentally if, for example, you erroneously reverse source and destination.

-rオプションはディレクトリ再帰

The -R and -r options are synonymous. Causes directories, buckets, and bucket subdirectories to be synchronized recursively. If you neglect to use this option gsutil will make only the top-level directory in the source and destination URLs match, skipping any sub-directories.

-dは削除オプションとなり転送元に存在しないオブジェクトが転送先にある場合削除されてしまいます。これをつけることで転送元と転送先が同期できますが、バケットを間違えると大変なことになるので注意が必要です。