How to redirect program error to a file while using pipes

I’m running a command on a Linux machine to backup my DB:

(echo "`date`: START DUMPING"; db_dump.sh; echo "`date`: DONE DUMPING") >>db_dump.log

And in the db_dump.sh:

pg_dump -v --dbname=mydb | pigz | openssl enc -aes128 -k ssecret -out db_dump.gz.aes

How would I catch the informative output from pg_dump, pigz and openssl to db_dump.log?

Currently I’m thinking doing it:

pg_dump -v --dbname=mydb 2>>db_dump.log| pigz 2>>db_dump.log| openssl enc -aes128 -k ssecret -out db_dump.gz.aes 2>>db_dump.log

but that seems quite cumbersome…

Answer

Try grouping with brackets:

( pg_dump -v --dbname=mydb | pigz | openssl enc -aes128 -k ssecret -out db_dump.gz.aes ) 2>>db_dump.log

Individual stderr output from all parts of the pipe will go into the same destination.

Leave a Reply

Your email address will not be published. Required fields are marked *