Using Netcat for Backup: Difference between revisions
No edit summary |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== | ==Getting started== | ||
Providing you have netcat installed, on the destination machine execute the following command: | Providing you have netcat installed, on the destination machine execute the following command: | ||
<syntaxhighlight lang="bash"> | |||
nc -l -p 6666 > filename.tar.bz2 | |||
</syntaxhighlight> | |||
This creates a listening socket on port 6666 with the filename specified above. | This creates a listening socket on port 6666 with the filename specified above. | ||
Line 11: | Line 12: | ||
So on the source you can issue the following command: | So on the source you can issue the following command: | ||
<syntaxhighlight lang="bash"> | |||
tar jlcvPpf - / > /dev/tcp/192.168.0.2/6666 | |||
</syntaxhighlight> | |||
In the example above 192.168.0.2 is the address of the destination address and the forward slash indicates that it is the entire filesystem to be backed up. You can easily change this for example to ''tar jlcvPpf - /var/www > /dev/tcp/192.168.0.2/6666'' to backup the /var/www directory. | In the example above 192.168.0.2 is the address of the destination address and the forward slash indicates that it is the entire filesystem to be backed up. You can easily change this for example to ''tar jlcvPpf - /var/www > /dev/tcp/192.168.0.2/6666'' to backup the /var/www directory. | ||
Line 21: | Line 24: | ||
*j - do bzip2 compression on the archive | *j - do bzip2 compression on the archive | ||
*l - one file system | *l - one file system | ||
*c - create archive | *c - create archive | ||
*v - be verbose about it | *v - be verbose about it | ||
Line 34: | Line 37: | ||
* On host machine | * On host machine | ||
nc -l -p 6666 < foo.tar.bz2 | nc -l -p 6666 < foo.tar.bz2 | ||
* On client machine | * On client machine | ||
nc 192.168.0.2 6666 | tar -xj | nc 192.168.0.2 6666 | tar -xj | ||
If the client is a very slow machine it might be faster to decompress on the host | If the client is a very slow machine it might be faster to decompress on the host | ||
(Host) | (Host) | ||
bzip2 -d --stdout foo.tar.bz2 | nc -l -p 6666 | bzip2 -d --stdout foo.tar.bz2 | nc -l -p 6666 | ||
(Client) | (Client) | ||
nc 192.168.0.2 6666 | tar -x | nc 192.168.0.2 6666 | tar -x | ||
===Some Updates=== | |||
Below is some information added by Colm Buckley on the [http://www.linux.ie Irish Linux Users Group] mailing list. | |||
If you have more than one directory to be transferred, you can list several | |||
on the "tar" command line above, or even use a wildcard - although if there | |||
are very many, the wildcard might expand to more than the capacity of the | |||
command line. Experimentation will reveal the best thing to do. | |||
On the server, do: | |||
nc -l -p 6666 | tar xvf - | |||
That tells it to listen on the socket and pipe the results directly into the | |||
tar expand command. | |||
[[Category:Linux| ]] |
Latest revision as of 11:59, 27 October 2018
Getting started
Providing you have netcat installed, on the destination machine execute the following command:
nc -l -p 6666 > filename.tar.bz2
This creates a listening socket on port 6666 with the filename specified above.
We now need to setup the source machine to dump whatever directories or filesystems you want to send to the destination machine.
So on the source you can issue the following command:
tar jlcvPpf - / > /dev/tcp/192.168.0.2/6666
In the example above 192.168.0.2 is the address of the destination address and the forward slash indicates that it is the entire filesystem to be backed up. You can easily change this for example to tar jlcvPpf - /var/www > /dev/tcp/192.168.0.2/6666 to backup the /var/www directory.
If there is no /dev/tcp it is possible to use netcat on both ends
tar jlcPpf - / | nc 192.168.0.2 6666
The options for the tar command are as follows:
- j - do bzip2 compression on the archive
- l - one file system
- c - create archive
- v - be verbose about it
- P - don't strip leading / from pathnames
- p - preserve permissions
- f - the file name to write, which in this case is - which means standard output.
Sending back the compressed archive
There is no need to send the whole archive back to the client, and then decompress it. This can all be done at once
- On host machine
nc -l -p 6666 < foo.tar.bz2
- On client machine
nc 192.168.0.2 6666 | tar -xj
If the client is a very slow machine it might be faster to decompress on the host
(Host)
bzip2 -d --stdout foo.tar.bz2 | nc -l -p 6666
(Client)
nc 192.168.0.2 6666 | tar -x
Some Updates
Below is some information added by Colm Buckley on the Irish Linux Users Group mailing list.
If you have more than one directory to be transferred, you can list several on the "tar" command line above, or even use a wildcard - although if there are very many, the wildcard might expand to more than the capacity of the command line. Experimentation will reveal the best thing to do.
On the server, do:
nc -l -p 6666 | tar xvf -
That tells it to listen on the socket and pipe the results directly into the tar expand command.