views:

273

answers:

3

Hi there!

I'm trying to figure out a way to use tar+pipes on a Ubuntu Server LTS.

I've got a postgresql command (pg_dump) that outputs lots of sql on the standard output:

pg_dump -U myUser myDB

I know how to redirect that to a file:

pg_dump -U myUser myDB > myDB.sql

In order to save some disk space, I would rather have it compressed: I can do a tar.gz file from that myDB.sql, and then delete myDB.sql.

But I was wondering - is there a way of doing this without creating the intermediate .sql file? I believe this could be accomplished with pipes... however I'm no shell guru, and know very little about them (I'm able to do ls | more, that's all). I've tried several variations of pg_dump .. | tar ... but with no success.

How can I use a pipe to use the output of pg_dump as an input for tar? Or did I just get something wrong?

+4  A: 

I don't see how "tar" figures into this at all; why not just compress the dump file itself?

pg_dump -U myUser myDB | gzip > myDB.sql.gz

Then, to restore:

gzip -cd myDB.sql.gz | pg_restore ...

The "tar" utility is for bundling up a bunch of files and directories into a single file (the name is a contraction of "tape archive"). In that respect, a "tar" file is kind-of like a "zip" file, except that "zip" always implies compression while "tar" does not.

Note finally that "gzip" is not "zip." The "gzip" utility just compresses; it doesn't make archives.

Pointy
or `zcat myDB.sql.gz |pg_restore ...`
vezult
thanks! very informative answer!
egarcia
+1  A: 

tar does not compress, what you want is gzip or a similat compression tool

mathroc
+1  A: 

Tar takes filenames as input. You probably just want to gzip the pg_dump output like so:

pg_dump -U myUser myDB |gzip > myDB.sql.gz
vezult