Docker Backup and restore postgres

I am using postgres docker: image 9.4. I need to know how to back up and restore only a container with volumes.

Created volume container:

docker run -v /var/lib/postgresql/data --name dbdata postgres:9.4 /bin/true 

Using the volume

 docker run --name=postgres --volumes-from=dbdata -d -p 6432:5432 postgres:9.4 

Reserve container

 docker run --volumes-from dbdata -v $(pwd):/backup postgres:9.4 tar cvf /backup/backup.tar /var/lib/postgresql/data 

Recover volume in a new container

 docker run --name=dbdata-new --volumes-from dbdata -v $(pwd):/backup ubuntu:14.04 /bin/sh -c 'cd /var/lib/postgresql/data && tar xvf /backup/backup.tar' 

Use in a new volume when creating a new postgres container:

 docker run --name=postgres-new --volumes-from=dbdata-new -d -p 7532:5432 postgres:9.4 

Problem: I get the following error in the logs when starting a new container.

initdb: the directory "/ var / lib / postgresql / data" exists, but is not empty If you want to create a new database system, delete or delete the directory "/ var / lib / postgresql / data" or run initdb with an argument other than " / var / lib / postgresql / data "

Not sure what I'm doing wrong. Can someone please indicate where I am making a mistake.

+7
docker postgresql
source share
2 answers

data-new --volumes-from dbdata -v $ (pwd): / backup ubuntu: 14.04 / bin / sh -c ' cd / var / lib / postgresql / data && & && & & tar xvf / backup / backup.tar '

Bold text was a problem. By expanding the backup in the / var / lib / postgresql / data folder, the tar command created / var / lib / postgresql / data inside the specified folder.

Thank you for helping MyKola.

+2
source share

It was not possible to easily reproduce the problem by following the steps with very rudiment data (one record one table one new db):

 psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 6432 -c "CREATE DATABASE ttt;" psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 6432 -d ttt -c "CREATE table a(b int); insert into a(b) values(1);" psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 6432 -d ttt -c "select * from a;" 

when I run postgres-new , I get no exceptions in the logs, and the data seems to be there:

 $ psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 7532 -d ttt -c "select * from a;" b --- 1 (1 row) 
+2
source share

All Articles