Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 535
unnik Avatar asked Jul 30 '15 10:07

unnik


1 Answers

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)
like image 96
Mykola Gurov Avatar answered Oct 12 '22 01:10

Mykola Gurov