scpと圧縮は中間保存なしで同時に行われます。

scpと圧縮は中間保存なしで同時に行われます。

正式な方法は何ですか?

  • scpファイルをリモートの場所に
  • 転送中のファイルを圧縮します(tar個々のファイル、フォルダ全体、7zaまたは他のより効率的な圧縮)。
  • 中間ファイルを保存せずに上記の操作を実行します。

私は次のようなシェルパイプに精通しています。

tar cf - MyBackups | 7za a -si -mx=9 -ms=on MyBackups.tar.7z

基本的に:

  • フォルダ全体を1つにマージtar
  • データをコンプレッサーstdoutに渡すstdin
  • 適用する怒る圧縮

sshリンクを介してこれを行い、ファイルをリモートファイルシステムに保存する最善の方法は何ですか?


sshfsむしろインストールしない方が良いでしょう。


これはうまくいきません:

scp <(tar cvf - MyBackups | 7za a -si -mx=9 -so) localhost:/tmp/tmp.tar.7z

なぜなら:

/dev/fd/63: not a regular file

答え1

必要なことを行う方法はいくつかあります。最も簡単な方法はpipeを使用することです。

tar zcvf -  MyBackups | ssh user@server "cat > /path/to/backup/foo.tgz"

ここで圧縮はtar任意の呼び出しgzip(フラグ)によってz処理されます。compressZ)とbzip()を使用することもできますj。の場合は、7z次のようにします。

tar cf - MyBackups | 7za a -si -mx=9 -ms=on MyBackups.tar.7z | 
   ssh user@server "cat > /path/to/backup/foo.7z"

これ最高しかし、方法はrsync

   Rsync is a fast and extraordinarily versatile  file  copying  tool.   It  can  copy
   locally, to/from another host over any remote shell, or to/from a remote rsync dae‐
   mon.  It offers a large number of options that control every aspect of its behavior
   and  permit  very  flexible  specification of the set of files to be copied.  It is
   famous for its delta-transfer algorithm, which reduces the amount of data sent over
   the network by sending only the differences between the source files and the exist‐
   ing files in the destination.  Rsync is widely used for backups and  mirroring  and
   as an improved copy command for everyday use.

rsync持つ方法選択肢が多すぎます。確かに読んでみる価値がありますが、一目で怖いです。この場合、あなたの懸念は次のとおりです。

    -z, --compress              compress file data during the transfer
        --compress-level=NUM    explicitly set compression level

   -z, --compress
          With this option, rsync compresses the file data as it is sent to the desti‐
          nation machine, which reduces the amount of data being transmitted --  
          something that is useful over a slow connection.

          Note  that this option typically achieves better compression ratios than can
          be achieved by using a compressing remote shell or a  compressing  transport
          because  it takes advantage of the implicit information in the matching data
          blocks that are not explicitly sent over the connection.

したがって、あなたの場合は、次のようなことをしたいと思います。

rsync -z MyBackups user@server:/path/to/backup/

ファイルは転送中に圧縮され、宛先に到達すると解凍されます。


より多くの選択:

  • scpデータ自体を圧縮できます

     -C      Compression enable.  Passes the -C flag to ssh(1) to
             enable compression.
    
    $ scp -C source user@server:/path/to/backup
    
  • rsyncたぶんあなたが親切になる方法があるかもしれませんが7za、そうすることは役に立ちません。この方法の利点rsyncは、ローカルファイルとリモートファイルの間で変更されたビットのみをコピーすることです。ただし、ローカルの小さな変更によって圧縮ファイルが非常に異なる可能性があるため、使用する必要はありませんrsync。それは仕事を複雑にし、役に立たない。ssh上図のように直接使用してください。もしあなたなら本物これを行うには、サブシェルを引数として渡してみてくださいrsync。私のシステムでは、7za圧縮データを端末に書き込めないため使用できません。実装が異なる場合があります。次のことを試してください(これは私には合わない):

    rsync $(tar cf - MyBackups | 7za a -an -txz -si -so) \
      user@server:/path/to/backup
    
  • もう一つの点は7z Linuxのバックアップには使用しないでください。。マニュアルページには次のように表示されます7z

    次の理由から、Linux/Unix バックアップに 7-zip 形式を使用しないでください。
    - 7-zipはファイルの所有者/グループを保存しません。

答え2

私の考えでは、この命令はうまくいくと思います。

ssh user@host "cd /path/to/data/;tar zc directory_name" | tar zx 

まず、ターゲットホストでこのコマンドを実行する必要があります。そして説明が必要な詳細は次のとおりです。

  1. ssh user@host はホストへの接続を開き、ホストからデータを転送します。
  2. cd /path/to/data は、必要なデータが格納されているディレクトリに入ります。
  3. tar zc *は圧縮を開始し、それをSTDOUTに入れます。
  4. パイプ(|)は、ソースのSTDOUTを「tar zx」を実行しているターゲットのSTDINにパイプし、ソースのデータストリームを連続的に解凍します。

ご覧のとおり、このコマンドはすぐに帯域幅を圧縮して保存します。より良い結果を得るために別の圧縮を使用することもできますが、圧縮と解凍にはCPUサイクルが必要であることに注意してください。

引用する

答え3

小さな改善dkbhadeshiyaの答え:これを行う必要はなく、cd dir作業ディレクトリを指定するだけですtar

ssh user@host "tar -C /path/to/data/ -zc directory_name" | tar zx 

同じ方法でディレクトリをアップロードすることもできます。

tar zc directory_name/ | ssh user@host "tar zx -C /new/path/to/data/"

関連情報