tar and netcat

 

stdin and stdout with tar

While archiving, we can specify stdout as the output file so another command in a pipe can read it as stdin and process the archive.

This technique will transfer data through a Secure Shell (SSH) connection, for example:

    $ tar cvf - files/ | ssh user@example.com "tar xv -C Documents/"

In the preceding example, the files/directory is added to a tar archive which is output to stdout (denoted by -) and extracted to the Documents folder on the remote system.

Receiving server(open port 44023)

root@Receiver:/StoragePool/data/logs/Firewalls/2020/11# nc -l 44023 | pv | tar x

2.27GiB 0:01:16 [46.6MiB/s] [                                                                                                                                         <=>                   ]




Sending server (send dir named 12 to 22)

root@Sender:/opt/immune/storage/logs/Firewalls/2020/11# ls

01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22

root@Sender:/opt/immune/storage/logs/Firewalls/2020/11# tar cf - 13  14  15  16  17  18  19  20  21 | nc 172.16.104.39 44023 

The sender server should run the following command


$ tar cf - logs/ | netcat 192.168.0.1 9000

and the receiveing server should listen with with this command

$ netcat -l -p 9000 | tar x

zfs send receive using netcat

zfs sender part

zfs send tank/test@snap100 | nc -w 20 10.0.1.2 8023

This means that the dataset “test” from my ZFS pool “tank” and snapshot “manual-2020-08-27_19-18” are being sent via netcat to host 10.0.1.2 on port 8023.


zfs receive side

nc -l 8023 -w 120 | zfs receive tank/test

This means that netcat is listening on port 8023 for the dataset “test” and adding it to the ZFS pool “tank”.


to see progress use pv eg.

nc -l 8023 -w 120 | pv | zfs receive tank/test


Send Incremenatl Snapshots

root@141:# zfs list -t snapshot

NAME                  USED  AVAIL  REFER  MOUNTPOINT

LP_Pool/storage@100  2.16M      -  1.38G  -             ## this is already sent 

LP_Pool/storage@101   136K      -  1.89G  -             # new snap 1

LP_Pool/storage@102      0      -  2.91G  -             # new snap 2


root@141:# zfs send -I LP_Pool/storage@100 LP_Pool/storage@102 | nc -w 120 10.45.9.148 44022


It will send @100 @101 and @102.

the recv command will be same

Speed will depend on disk on sendeer side, Eg. below stats show that disk performance is saturated.

iostat -xz 1


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          49.70    0.00    8.48    0.00    0.00   41.82


Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

sda               0.00     0.00   13.59    0.00    73.79     0.00    10.86     2.71  196.29  196.29    0.00  73.71 100.19

sdb               0.00     0.00   14.56    0.00   166.99     0.00    22.93     2.58  177.33  177.33    0.00  68.80 100.19


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          48.51    0.00   10.45    2.99    0.00   38.06


Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

sda               0.00     0.00   15.00    0.00   124.00     0.00    16.53     2.52  181.33  181.33    0.00  65.33  98.00

sdb               0.00     0.00   15.00    0.00   120.00     0.00    16.00     2.66  189.87  189.87    0.00  65.33  98.00


avg-cpu:  %user   %nice %system %iowait  %steal   %idle

          38.69    0.00   13.69    0.00    0.00   47.62


Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util

sda               0.00     0.00   25.00    0.00   188.00     0.00    15.04     2.57  105.12  105.12    0.00  36.96  92.40

sdb               0.00     0.00   26.00    0.00   192.00     0.00    14.77     2.85  107.38  107.38    0.00  39.38 102.40