Friday, September 9, 2011

duplicating devices via dd or via nc

using dd and gzip
Backup the drive
# dd if=/dev/hda conv=sync,noerror bs=64K | gzip -c  > /mnt/sda1/hda.img.gz
To restore your system
# gunzip -c /mnt/sda1/hda.img.gz | dd of=/dev/hda conv=sync,noerror bs=64K
view partitions in the image (once uncompressed)
# fdisk -l hda.img > /mnt/sda1/hda_fdisk.info 
 
netcat chatserver
create the chat server on this machine and set it to listen to 3333 TCP port:
$ nc -l 3333
On the other end, connect to the server with the following:
$ nc 192.168.0.1 3333

creating a partition image and sending it to a remote machine on-the-fly:
$ dd if=/dev/hdb5 | gzip -9 | nc -l 3333
On the remote machine, connect to the server and receive the partition image with the following command:
$ nc 192.168.0.1 3333 | pv -b > myhdb5partition.img.gz
This might not be as classy as the partition backups using partimage, but it is efficient.

http://www.g-loaded.eu/2006/11/06/netcat-a-couple-of-useful-examples/ 

audio examples, and named fifo examples here:
http://www.debian-administration.org/articles/145

file transfer:
tar cz /etc | ssh user@host -c ‘cd /bak ; cat > etc.tgz’

On the localhost, this will create an archive of /etc, which is piped into an ssh connection. On the remote host, the snippet enclosed in ” is executed: Change to the backup directory, and pipe the content of the connection into a file. If I wanted to extract the files again on the receiving side, I could do:

tar cz /etc | ssh user@host -c ‘cd /bak ; cat tar xz’

Note that the ‘-f -’ given in the article is redundant. If tar is not given a file, it uses STDIN/OUT. The advantage over recursive scp is that this method preserves device nodes and file permissions. The advantage over rsync is that it is using familiar tar semantics on both sides (of course, if you want diffing, you need rsync, which works just fine over SSH).

Of course, the other direction (pulling files from a host) is equally possible:

ssh user@host -c ‘tar cz /etc’ > /bak/etc.tgz

No comments:

Post a Comment