Docker Community Forums

Share and learn in the Docker community.

[SOLVED] How to backup & restore volumes with app data like Postgres data?


(Dooku) #1

Hi all,

I have created a container with Postgres and a volume for pgdata. I want to be able to backup and recover the database data easily with btrfs snapshots. For this purpose I have mapped the pgdata folder to a subvolume like this (using run not create!):

-v /var/lib/docker/btrfs/subvolumes/pgdata:/var/lib/postgresql/data/pgdata

I can do a snapshot after I have stopped the container without problems but when I try to start the container again the container is exiting immediately. I suppose the reason is docker is trying to recreate the volume from scratch and fails. So how can I tell or Postgres to use the existing pgdata btrfs subvolume? How I can tell docker or an app in general to use an existing volume?

Any ideas?

(Todd W.) #2

what does docker start -a container_name show for logs as to what it is doing? Maybe that will help give you a hint as to where it is getting stuck.

(Dooku) #3

pennacook thanks for the response. After some googling and some experiments it seems to be working. The problem was apparently due to permissions and I have found a solution here:

First I have checked which uid and gid the directory in the container had, then created the directory in the host and changed the uid and gid from root to the same uid and gid of the directory in the container. I’ve tested this by making pre and post snapshots after deleting the directory and has worked.
Another thing that caused me problems was mounting pgdata directly on the btrfs subvolume. I had to create another directory within the subvolume like this:
-v /var/lib/docker/btrfs/subvolumes/subvolume/pgdata:/var/lib/postgresql/data/pgdata
So again uid and gid of pgdata must be the same in the host and in the container (maybe only uid not sure).