PDA

View Full Version : [all variants] tar from stdin: piping to tar.



JohnGalt131
December 11th, 2008, 01:15 PM
I want to do something like this


# dd if=/dev/sdX | tar -cvz /location/of/harddiskimage.tar.gz


I am currently using the following


# dd if=/dev/sdX | gzip > /location/of/harddiskimage.img.gz

However I would like to be able to add other things into the archive, not just compress the image.
Any help is appreciated

cmnorton
December 11th, 2008, 04:06 PM
What about:

dd if=/dev/sdX | tar -cvzf /location/of/harddiskimage.tar.gz

z is supposed to compress on create and decompress on extract.

JohnGalt131
December 11th, 2008, 04:08 PM
What about:

dd if=/dev/sdX | tar -cvzf /location/of/harddiskimage.tar.gz

z is supposed to compress on create and decompress on extract.


# dd if=/dev/zero | tar -cvzf /home/user/Desktop/imag.tgz
tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.

glennric
December 11th, 2008, 04:48 PM
Why not first do

dd if=/dev/sdX > filename
Then

tar czvf harddiskimage.tar.gz filename

??

Duck2006
December 11th, 2008, 04:55 PM
Some of this may help

http://ubuntuforums.org/showthread.php?t=633232
https://help.ubuntu.com/community/BackupYourSystem/TAR

JohnGalt131
December 11th, 2008, 05:01 PM
Why not first do

dd if=/dev/sdX > filename
Then

tar czvf harddiskimage.tar.gz filename

??

Isn't that two operations? dd is very slow as is and if you add to that time the time needed to tar it, that makes for two long operations when I'd rather it just be 1 long operation.

glennric
December 11th, 2008, 05:08 PM
I don't think that tar accepts data from standard input the way you are trying to do. It needs a list of files to work on, not data like that output by dd. So you may be stuck with two commands.

glennric
December 11th, 2008, 05:11 PM
Why are you trying to do this?

cmnorton
December 11th, 2008, 05:40 PM
# dd if=/dev/zero | tar -cvzf /home/user/Desktop/imag.tgz
tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.


Mea culpa. Sorry about the mis-information.

You can certainly dd from the device and pipe it into gzip. I just tried it.

sudo dd if=/dev/sdb1 | gzip -9 > temp.gz

This works, where /dev/sdb1 is my unmounted external USB drive.

Else, I believe you would have to dd out to a file, and then run that through tar.

Edit:
-----------

Are you trying to image a disk?

JohnGalt131
December 11th, 2008, 06:28 PM
Mea culpa. Sorry about the mis-information.

You can certainly dd from the device and pipe it into gzip. I just tried it.

sudo dd if=/dev/sdb1 | gzip -9 > temp.gz

This works, where /dev/sdb1 is my unmounted external USB drive.

Else, I believe you would have to dd out to a file, and then run that through tar.

Edit:
-----------

Are you trying to image a disk?
Yes I periodically create compressed images. But I would like to be able to append files to the archive (usually text files) such as a list of instructions for a restore, the contents of the image, etc. gzip only compresses, it does not (I don't think) archive.

caljohnsmith
December 11th, 2008, 07:22 PM
I want to do something like this


# dd if=/dev/sdX | tar -cvz /location/of/harddiskimage.tar.gz


I am currently using the following


# dd if=/dev/sdX | gzip > /location/of/harddiskimage.img.gz

However I would like to be able to add other things into the archive, not just compress the image.
Any help is appreciated
I believe Glennric is right, if you send standard output to tar, it needs to be a list of files to tar together; when you use "dd" to create an image, you are creating one, single huge "file" of your entire drive, so tar "cowardly" refuses to tar a single file. If you want to append files to your cloned HDD image that you create with dd, the only way I know of to do it is if the HDD image is decompressed, and then you can mount a partition (sda6 in the example below) in the HDD image by doing:

sudo losetup -o $((512*$(sudo sfdisk -luS harddiskimage.img 2>/dev/null | grep sda6 | awk '{print $2}'))) /dev/loop0 harddiskimage.img
sudo mount /dev/loop0 /mnt
And then your sda6 partition is mounted on /mnt, and you can add files to it. Essentially the code above is the same as manually doing:

sudo sfdisk -luS harddiskimage.img
And then using that output to know the starting sector of your sda6 partition as "start_sector" (sda6 is just an example), then you can do:


sudo losetup -o $((512*start_sector)) /dev/loop0 harddiskimage.img
sudo mount /dev/loop0 /mnt
And that's the same thing.