Docker Backup and restore postgres

I using docker postgres:9.4 image. I need to know how to backup and restore volume only container.

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

Backup Volume container

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

Restore volume in 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 the new volume in creating new postgres container:

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

Issue: I get the below error in the logs when I run the new container.

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

Not sure what I am doing wrong. Can someone please point out where I am making mistake.


Could not easily reproduce the issue 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 start the postgres-new I get no exceptions in 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)

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

The bold text was the problem. By untaring the backup in the folder /var/lib/postgresql/data ,the tar command was creating /var/lib/postgresql/data inside the above folder.

Thank you for all the help MyKola.

链接地址: http://www.djcxy.com/p/87098.html

上一篇: 如何显示在特定目录中使用的Vagrant Box版本

下一篇: Docker备份和恢复postgres