ssh経由でのtarによるフォルダの圧縮・解凍方法

LinuxやUNIXを利用していると、sshを経由して、ローカルのフォルダにあるファイルをアーカイブファイルとしてリモートサーバに配布したり、逆にリモート側のフォルダをアーカイブ化してローカルに配置したいときもあるだろう。 今回は、ローカル→リモートとリモート→ローカルでのtarを用いたフォルダの圧縮・解凍方法について記述する。 なお、ここではアーカイブファイルの形式は「tar.gz」とし、リモートサーバのIPアドレスを「192.168.0.240」としている。

1.【ローカル→リモート】ローカルでファイル・フォルダを圧縮し、リモートでアーカイブファイルとして保持させる

コマンドを実行しているローカルでファイルやフォルダをアーカイブ化し、それをリモート側に転送する場合は、以下のようにコマンドを実行する。

bash
tar zcvf - /アーカイブ化したいディレクトリのパス | ssh ユーザ名@リモートホスト "cat > /リモート側で作成するアーカイブ・ファイルのパス

shell
# ls -la /wort_test/ 合計 8 drwxr-xr-x. 2 root root 4096 2月 4 23:11 2015 . dr-xr-xr-x. 27 root root 4096 2月 4 23:11 2015 .. -rw-r--r--. 1 root root 0 2月 4 23:11 2015 1111 -rw-r--r--. 1 root root 0 2月 4 23:11 2015 2222 -rw-r--r--. 1 root root 0 2月 4 23:11 2015 3333 # tar zcvf - /wort_test | ssh root@192.168.0.240 "cat > /wort_test.tar.gz" tar: メンバ名から先頭の `/' を取り除きます /wort_test/ /wort_test/2222 /wort_test/3333 /wort_test/1111 root@192.168.0.240's password: # ssh root@192.168.0.240 "ls -la /wort_test.tar.gz" root@192.168.0.240's password: -rw-r--r-- 1 root root 168 2月 4 23:13 2015 /wort_test.tar.gz

2.【ローカル→リモート】ローカルのアーカイブファイルをリモートで解凍する

コマンドを実行しているローカル側にあるアーカイブファイルを、リモート側で解凍する場合、以下のようにコマンドを実行する。

bash
ssh ユーザ名@リモートホスト "tar zxvf -C リモート側で展開させたいフォルダ -" < /リモート側で解凍させたいアーカイブファイルのパス

shell
# ssh root@192.168.0.240 "ls -la /test1234" root@192.168.0.240's password: 合計 8 drwxr-xr-x 2 root root 4096 2月 4 23:31 2015 . dr-xr-xr-x. 29 root root 4096 2月 4 23:31 2015 .. # ssh root@192.168.0.240 "tar zxvf - -C /test1234" < test.tar.gz root@192.168.0.240's password: work/test/ work/test/test.txt work/test/20141207/ work/test/20141207/messages work/test/test-20141207/ work/test/test.sh work/test/test2.txt work/test/test1-20141207/ work/test/test2.sh # ssh root@192.168.0.240 "ls -la /test1234" root@192.168.0.240's password: 合計 12 drwxr-xr-x 3 root root 4096 2月 4 23:32 2015 . dr-xr-xr-x. 29 root root 4096 2月 4 23:31 2015 .. drwxr-xr-x 3 root root 4096 2月 4 23:32 2015 work

3.【リモート→ローカル】リモートでファイル・フォルダを圧縮し、ローカルでアーカイブファイルとして保持させる

ssh接続先のリモート側にあるファイルやフォルダを圧縮し、ローカルにアーカイブファイルとして転送する場合は、以下のようにコマンドを実行する。

bash
ssh ユーザ名@リモートホスト "tar -cf - /アーカイブ化したいディレクトリのパス" | gzip > /ローカル側で作成するアーカイブ・ファイルのパス

shell
# ssh root@192.168.0.240 "ls -la /work/test" root@192.168.0.240's password: 合計 36 drwxr-xr-x 5 root root 4096 12月 7 21:04 2014 . drwxr-xr-x 3 root root 4096 11月 16 11:40 2014 .. drwxr-xr-x 2 root root 4096 12月 7 21:08 2014 20141207 drwxr-xr-x 2 root root 4096 12月 7 21:03 2014 test-20141207 -rw-r--r-- 1 root root 75 11月 16 15:42 2014 test.sh -rw-r--r-- 1 root root 34 11月 16 11:43 2014 test.txt drwxr-xr-x 2 root root 4096 12月 7 21:04 2014 test1-20141207 -rw-r--r-- 1 root root 90 11月 16 18:41 2014 test2.sh -rw-r--r-- 1 root root 87 11月 16 18:41 2014 test2.txt # ssh root@192.168.0.240 "tar -cf - /work/test" | gzip > /root/test.tar.gz root@192.168.0.240's password: tar: メンバ名から先頭の `/' を取り除きます # tar tvfz /root/test.tar.gz drwxr-xr-x root/root 0 2014-12-07 21:04 work/test/ -rw-r--r-- root/root 34 2014-11-16 11:43 work/test/test.txt drwxr-xr-x root/root 0 2014-12-07 21:08 work/test/20141207/ -rw------- root/root 150 2014-12-07 21:08 work/test/20141207/messages drwxr-xr-x root/root 0 2014-12-07 21:03 work/test/test-20141207/ -rw-r--r-- root/root 75 2014-11-16 15:42 work/test/test.sh -rw-r--r-- root/root 87 2014-11-16 18:41 work/test/test2.txt drwxr-xr-x root/root 0 2014-12-07 21:04 work/test/test1-20141207/ -rw-r--r-- root/root 90 2014-11-16 18:41 work/test/test2.sh

なお、以下のコマンドでも同様の効果が得られる。

bash
ssh ユーザ名@リモートホスト "tar -zcf - /アーカイブ化したいディレクトリのパス" > /ローカル側で作成するアーカイブ・ファイルのパス

4.【リモート→ローカル】リモートのアーカイブファイルをローカルで解凍する

ssh接続先のリモート側にあるアーカイブファイルを、ローカル側で解凍する場合、以下のようにコマンドを実行する。

bash
ssh ユーザ名@リモートホスト "cat /ローカル側で解凍させたいアーカイブファイル" | tar zxvf - -C ローカル側で展開させたいフォルダ

shell
# mkdir /test # ssh root@192.168.0.240 "cat /work.tar.gz" | tar zxvf - -C /test/ root@192.168.0.240's password: work/ work/test/ work/test/test.txt work/test/20141207/ work/test/20141207/messages work/test/test-20141207/ work/test/test.sh work/test/test2.txt work/test/test1-20141207/ work/test/test2.sh # find /test/ -ls 783363 4 drwxr-xr-x 3 root root 4096 2月 4 23:05 /test/ 783364 4 drwxr-xr-x 3 root root 4096 11月 16 11:40 /test/work 783365 4 drwxr-xr-x 5 root root 4096 12月 7 21:04 /test/work/test 783369 4 drwxr-xr-x 2 root root 4096 12月 7 21:03 /test/work/test/test-20141207 783366 4 -rw-r--r-- 1 root root 34 11月 16 11:43 /test/work/test/test.txt 783370 4 -rw-r--r-- 1 root root 75 11月 16 15:42 /test/work/test/test.sh 783373 4 -rw-r--r-- 1 root root 90 11月 16 18:41 /test/work/test/test2.sh 783367 4 drwxr-xr-x 2 root root 4096 12月 7 21:08 /test/work/test/20141207 783368 4 -rw------- 1 root root 150 12月 7 21:08 /test/work/test/20141207/messages 783371 4 -rw-r--r-- 1 root root 87 11月 16 18:41 /test/work/test/test2.txt 783372 4 drwxr-xr-x 2 root root 4096 12月 7 21:04 /test/work/test/test1-20141207