Googleドライブにコマンドラインでアクセスするskickaを試す

Googleドライブにコマンドラインでアクセスできる skicka を少し試してみました．

google/skicka: Command-line utility for working with Google Drive. Join the mailing list at https://groups.google.com/forum/#!forum/skicka-users.

GOPATHを設定していない場合設定

$ GOPATH=~/go
$ PATH=${PATH}:~/go/bin

skickaの導入

$ go get github.com/google/skicka

skickaの設定を行います．
init コマンドで設定ファイルを生成します．
設定ファイルは ~/.skicka.config として作れれます．
次にlsコマンドを発行します．初回実行時にはウェブブラウザが起動して認証処理が走ります．許可すると自動的に認証が完了してls処理が走ります．
認証情報は`~/.skicka.tokencache.json` に保存されます．
ls実行時に次回以降のためにメタデータを作成します．ファイルが多いと時間がかかります．プログレスが表示されるのでしばらく待ちましょう．
メタデータは ~/.skicka.metadata.cache に保存されます．

skickaの初期設定

$ skicka init
$ skicka ls

サーバなどでウェブブラウザを起動するのが難しい場合は -no-browser-auth を ls コマンドの 前 に付けて起動すると認証URLが表示されるのでそれを操作PCなどで開いて認証を行い，表示された認証コードをコマンドラインに貼り付けます．

$ skicka -no-browser-auth ls
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=952283912845-sjotrejbktpinel08hrnspl33d8gho1e.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=st1541320926479298710
Enter verification code: 4/iwBrjQFEvm2p0nQGXJuRQgTZFZZ6BmXkT7Up7Wl3UYS7NWEyiltTc6E

Usage

$ ~/go/bin/skicka
usage: skicka [skicka options] <command> [command options]

Supported commands are:
cat Print the contents of the given file
download Download a file or folder hierarchy from Drive to the local disk
df Display free space on Drive
du Report disk usage for a folder hierarchy on Drive
fsck Check consistency of files in Drive and local metadata cache
genkey Generate a new encryption key
init Create an initial skicka configuration file
ls List the contents of a folder on Google Drive
mkdir Create a new folder or folder hierarchy on Drive
rm Remove a file or folder on Google Drive
upload Upload a local file or directory hierarchy to Drive

'skicka help' prints more detailed documentation.

help

$ ~/go/bin/skicka help [45/2151]
skicka is a tool for working with files and folders on Google Drive.
See http://github.com/google/skicka/README.md for information about getting started.

usage: skicka [common options] <command> [command options]

Commands and their options are:
cat Print the contents of the Google Drive file to standard output.
Arguments: drive_path ...

download Recursively download either a single file, or all files from a
Google Drive folder to a local directory. If the corresponding
local file already exists and has the same contents as the its
Google Drive file, the download is skipped.
Arguments: [-ignore-times] [-download-google-apps-files]
drive_path local_path

df Prints the total space used and amount of available space on
Google Drive.

du Print the space used by the Google Drive folder and its children.
Arguments: [drive_path ...]

fsck [EXPERIMENTAL/NEW] Use at your own risk.
Perform a number of consistency checks on files stored in Google
Drive, including verifying metadata and removing duplicate files
with the same name.
Arguments: [--trash-duplicates] [drive_path]

help Print this help text.

genkey Generate a new key for encrypting files.

init Create an initial ~/.skicka.config configuration file. (You
will need to edit it before using skicka; see comments in the
configuration file for details.)

ls List the files and directories in the given Google Drive folder. [8/2151]
Arguments: [-d, -l, -ll, -r] [drive_path ...],
where -l and -ll specify long (including sizes and update
times) and really long output (also including MD5 checksums),
respectively. The -r argument causes ls to recursively list
all files in the hierarchy rooted at the base directory, and
-d causes directories specified on the command line to be
listed as files (i.e., their contents aren't listed.)

mkdir Create a new directory (folder) at the given Google Drive path.
Arguments: [-p] drive_path ...,
where intermediate directories in the path are created if -p is
specified.

rm Remove a file or directory at the given Google Drive path.
Arguments: [-r, -s] drive_path ...,
where files and directories are recursively removed if -r is
specified and the google drive trash is skipped if -s is
specified. The default behavior is to fail if the drive path
specified is a directory and -r is not specified, and to send
files to the trash instead of permanently deleting them.

upload Uploads all files in the local directory and its children to the
given Google Drive path. Skips files that have already been
uploaded.
Arguments: [-ignore-times] [-encrypt] [-follow-symlinks <maxdepth>]
local_path drive_path

Options valid for both "upload" and "download":
-dry-run Don't actually upload or download, but print the paths of
all files that would be transferred.
-ignore-times Normally, skicka assumes that if the timestamp of a local
file matches the timestamp of the file on Drive and the
files have the same size, then it isn't necessary to
confirm that the file contents match. The -ignore-times
flag can be used to force checking file contents in this
case.

General options valid for all commands:
-config <filename> General skicka configuration file. Default: ~/.skicka.config.
-debug Enable debugging output.
-dump-http Dump http traffic.
-metadata-cache-file <filename>
File to store metadata about Google Drive contents.
Default: ~/.skicka.metadata.cache
-no-browser-auth Disables attempting to open the authorization URL in a web
browser when initially authorizing skicka to access Google Drive.
-quiet Suppress non-error messages.
-tokencache <filename> OAuth2 token cache file. Default: ~/.skicka.tokencache.json.
-verbose Enable verbose output.

ファイルのアップロード

$ skicka upload ~/Pictures/$(ls -tr1 ~/Pictures/|tail -1) /
Files: 14.41 KiB / 14.41 KiB [====================================================================================================================] 100.00% 3s
2018/11/04 17:45:32 Preparation time 1s, sync time 3s
2018/11/04 17:45:32 Updated 1 Drive files, 0 local files
2018/11/04 17:45:32 14.41 kiB read from disk, 0 B written to disk
2018/11/04 17:45:32 14.41 kiB uploaded (3.98 kiB/s), 0 B downloaded (0 B/s)
2018/11/04 17:45:32 52.08 MiB peak memory used

ファイルを確認

$ skicka ls|grep jpg
20180307_23:03:21-10889.jpg
20180307_23:03:21-10889.jpg
20181104_17:11:45-30082.jpg

ファイルのダウンロード

$ skicka rm 20181104_17:11:45-30082.jpg
$ diff ~/Pictures/20181104_17:11:45-30082.jpg /tmp/20181104_17:11:45-30082.jpg
$ ls -l ~/Pictures/20181104_17:11:45-30082.jpg /tmp/20181104_17:11:45-30082.jpg
-rw-r--r-- 1 matoken matoken 14760 11月 4 17:41 /home/matoken/Pictures/20181104_17:11:45-30082.jpg
-rw-r--r-- 1 matoken matoken 14760 11月 4 17:41 /tmp/20181104_17:11:45-30082.jpg

ファイルの削除

$ skicka rm /20181104_17:11:45-30082.jpg

skickaには暗号化機能があります．upload時に -encrypt オプションを付与することによりファイルを暗号化してGoogleドライブに保存できます．そのためには準備が必要です．
SKICKA_PASSPHRASE という環境変数にパスフレーズを設定した状態で，skicka genkey を実行して生成された情報を設定ファイルの ~/.skicka.config に書き込みます．

暗号化のためのkey等を生成する(環境変数はexportしないと認識しなかった)

$ read -sp "skicka pass: " SKICKA_PASSPHRASE && export SKICKA_PASSPHRASE && skicka genkey
skicka pass: ; Add the following lines to the [encryption] section
; of your ~/.skicka.config file.
salt=62cd4c08693c935f35c45be5261907127ef14055f197bb19bae36616fedecb54
passphrase-hash=23f702efce4fe7bf775d3bc80f07e2a765589016b537bf322ed5e0c8f2f6ce90
encrypted-key=463c9c75b3aa98e5401bb5fde6d681f8053d39d3e5ce49ef22bcc58977736162
encrypted-key-iv=d2533e58f6c9bb976a66d499280ce8c2

設定ファイルに暗号化情報を書き込む(コメントを外すのを忘れずに)

$ vi ~/.skicka.config
$ grep -A5 encryption ~/.skicka.config
[encryption]
; Run 'skicka genkey' to generate an encyption key.
salt=62cd4c08693c935f35c45be5261907127ef14055f197bb19bae36616fedecb54
passphrase-hash=23f702efce4fe7bf775d3bc80f07e2a765589016b537bf322ed5e0c8f2f6ce90
encrypted-key=463c9c75b3aa98e5401bb5fde6d681f8053d39d3e5ce49ef22bcc58977736162
encrypted-key-iv=d2533e58f6c9bb976a66d499280ce8c2

upload時に -encrypt を指定すると暗号化され，.aes256 という拡張子付きで保存されます．

暗号化しながら保存する

$ skicka mkdir encryption
$ skicka upload -encrypt ~/Pictures/20181105_19\:11\:18-8911.jpg /encryption/
$ skicka ls /encryption/
20181105_19:11:18-8911.jpg.aes256

元のファイル名ではダウンロードできず，.aes256 拡張子付きのファイル名ではダウンロードできた．このファイルは .aes256 の拡張子付きのままだが，復号されている．

ダウンロード

$ skicka download /encryption/20181105_19:11:18-8911.jpg ./
skicka: /encryption/20181105_19:11:18-8911.jpg: not found on Drive
$ skicka download /encryption/20181105_19:11:18-8911.jpg.aes256 ./
$ file ./20181105_19\:11\:18-8911.jpg.aes256
./20181105_19:11:18-8911.jpg.aes256: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 933x600, frames
3
$ identify ./20181105_19\:11\:18-8911.jpg.aes256
./20181105_19:11:18-8911.jpg.aes256 JPEG 933x600 933x600+0+0 8-bit sRGB 92988B 0.000u 0:00.000
$ display ./20181105_19\:11\:18-8911.jpg.aes256

ウェブブラウザからダウンロードしたファイルは暗号化されている．データ量は少し増えている．

$ file 20181105_19_11_18-8911.jpg.aes256.jpg.aes256
20181105_19_11_18-8911.jpg.aes256.jpg.aes256: data
$ ls -la 201811*
-rw-r--r-- 1 matoken matoken 92988 11月 5 19:30 20181105_19:11:18-8911.jpg.aes256
-rw-r--r-- 1 matoken matoken 93004 11月 5 21:46 20181105_19_11_18-8911.jpg.aes256.jpg.aes256

環境

$ dpkg-query -W golang git bash
bash 4.4.18-3.1
git 1:2.19.1-1
golang 2:1.10~5
$ lsb_release -d
Description: Debian GNU/Linux unstable (sid)
$ uname -m
x86_64