user3591466
user3591466

Reputation: 867

How do I back up docker volume for postgres?

Here is my docker compose file

version: '2'

services:
  postgres9:
    image: postgres:9.4
    expose:
      - 5432
    volumes:
      - data:/var/lib/postgresql/data

volumes:
  data: {}

I would like to back up data .

Is there a command for that in docker? I am using mac for my local and linux for my server

Upvotes: 1

Views: 2011

Answers (1)

vedarthk
vedarthk

Reputation: 1333

Volumes are not anything special in case of Docker. They are simple directories/files, when you are using volumes in compose file docker will create a directory and mount that inside of your container when you run the container. You can see that by:

docker inspect <container name/id>

in the output you will find information about volumes, you can see there the directory on the host OS

To backup your volume you can simply compress and store the directory using tar. To do that you need to know the path of the directory, you can either mount a directory from host OS like:

version: '2'

services:
  postgres9:
    image: postgres:9.4
    expose:
      - 5432
    volumes:
      - /var/lib/postgresql/data:/var/lib/postgresql/data

and you can backup /var/lib/postgresql/data from the host OS either by mounting it another container or from the host OS directly

OR there is another way, you can mount the same volume in another container in readonly mode and backup the directory:

version: '2'

services:
  postgres9:
    image: postgres:9.4
    expose:
      - 5432
    volumes:
      - data:/var/lib/postgresql/data
  backuptool:
    image: busybox:latest
    volumes:
      - data:/data:ro
volumes:
  data: {}

you can then tar and upload the backup of /data from the backuptool container

Upvotes: 2

Related Questions