Goを書いていてsshでファイル転送したかったが、Goでのいい感じのクライアントが見つからなかったので普通にscpコマンドを実行させることにしたが、
scpコマンドの挙動でハマった。
雑にメモ。
やりたいこと
remoteの/var/data/ディレクトリの中身を localの/download/以下にダウンロードしたい。
remote └── var └── data <- ここ以下のファイル群を ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt local └── download └── 20200401 <-こんな感じでダウンロードしたい(ディレクトリ名はダウンロード日) ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt
scpコマンド検討
/downloads/20200401ディレクトリを作っていない状態で
以下のように、事前に20200401ディレクトリを作ってない状態で
local └── download
以下のコマンドだと、、
scp -r remote:/var/data/ /downloads/20200401
bashで実行した場合、意図通りになる
local └── download └── 20200401 ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt
Goで実行した場合、意図通りになる
exec.Command( "scp", "-r", "remote:/var/data/", /downloads/20200401, ).CombinedOutput() local └── download └── 20200401 ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt
/downloads/20200401ディレクトリを作った状態で
以下のように、事前に20200401ディレクトリを作った状態で
local └── download └── 20200401 <- こいつを事前に用意しておく
以下のコマンドだと、、
scp -r remote:/var/data/ /downloads/20200401
bashで実行した場合、意図通りにならない
local └── download └── 20200401 └── data <- これいらない ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt
Goで実行した場合、意図通りにならない
out, err := exec.Command( "scp", "-r", "remote:/var/data/", /downloads/20200401, ).CombinedOutput() local └── download └── 20200401 └── data <- これいらない ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt
*を使った場合
scp -r remote:/var/data/* /downloads/20200401/
bash実行だとエラーになる
no matches found: remote:/var/data/*
Go実行だと意図通りになる
out, err := exec.Command( "scp", "-r", "remote:/var/data/*", /downloads/20200401/, ).CombinedOutput() local └── download └── 20200401 ├── a │ └── 1.txt ├── b │ └── 2.txt └── c └── 3.txt