7

I want to backup a database using psql and COPY command. here is my script:

psql "user=${USERNAME} host=${HOSTNAME} dbname=${DBNAME} password=${PASSWORD}" -c \
"COPY (SELECT * FROM tbl) ORDER BY id ASC) TO STDOUT WITH CSV HEADER;" | \
bzip2 -z -f --best -c > /home/${DBNAME}-${FILENAME}.csv.bz2

Also I want to know how many lines are copied from database but psql does not have such feature. so I need external command. I need something like this:

psql ... | wc -l | bzip2

Is there any solution? The only solution I have found is to use fifo:

mkfifo /home/backup.fifo
psql ... | tee /home/backup.fifo | bzip2

Now from another terminal window I can use wc:

wc -l /home/backup.fifo

1 Answer 1

14

Both bash and zsh have a >(pipeline) feature:

psql ... | tee >(wc -l) | bzip2

Note that the > here is not a normal redirection, but a necessary part of the syntax. You would need a second > if you wanted to combine it with an actual redirection (with a space in between so it wouldn't be read as >> redirect-for-append).

4
  • 1
    what is >(wc -l) called so I can search it? :D Commented Apr 8, 2012 at 9:29
  • 2
    @MajidAzimi It's called Process Substitution. Commented Apr 8, 2012 at 9:30
  • 2
    You probably want >(wc -l >&2) unless you want the wc output to get bzipped as well. Commented Apr 8, 2012 at 9:34
  • 1
    That is an... interesting... bash behavior; I would expect it to be independent of the rest of the pipeline (and in zsh it is). Commented Apr 8, 2012 at 9:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.