Nicole Staline
Nicole Staline

Reputation: 671

PG_Restore local backup to docker container using pipe viewer

I was using the following command to restore from a backup:

pv backup.sql.gz | gunzip | pg_restore -d $DBHOST

This was helpful because I can roughly see how far a long the backup has to finish.

I recently moved the DB into a docker container and wanted to be able to run the same restore, but have been struggling getting the command to work. I'm assuming I have to redirect the gunzip output somehow, but haven't been having any luck. Any help would be appreciated.

Upvotes: 0

Views: 498

Answers (1)

Gerard van Helden
Gerard van Helden

Reputation: 1602

You don't need to expose the server ports, you can use

pv backup.sql.gz | gunzip | docker exec -i [container_name] pg_restore -d $DBHOST

This will open the pg_restore command within the running container and pipe into that process. Note the '-i' flag, it's important. Assuming your server container also has clients installed, but if not, I'd recommend you to do so anyway.

Typically, this is a pattern I use very often in scripts as well, even using SSH in between; this will all work transparently:

pv backup.sql.gz | gunzip | ssh some_ssh_host docker exec -i [container_name] pg_restore -d $DBHOST

Note that if you do this over SSH as well, it will start to make sense to decide where to execute your unzip:

pv backup.sql.gz | ssh some_ssh_host gunzip \| docker exec -i [container_name] pg_restore -d $DBHOST

Note that the pipe in that case needs to be escaped to send it as a literal to the ssh command.

Upvotes: 0

Related Questions