2

I'm following the offical docker guide from here to backup a docker volume. I'm also aware of this SO question however I'm still running into errors. Running the following command:

docker run --rm --volumes-from dbstore -v $(pwd):/backup ny_db_1 tar cvf /backup/backup.tar /dbdata

No matter what image name or container name or container id I put, I get the following error:

Unable to find image 'ny_db_1:latest' locally

The volume I want to backup:

$ docker volume ls
DRIVER              VOLUME NAME
local               ny_postgres_data

My containers:

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
39e71e660eda        postgres:10.1-alpine   "docker-entrypoint.s…"   4 days ago          Up 23 minutes       0.0.0.0:5434->5433/tcp   ny_db_1

How do I backup my volume?

Update:

I tried the following but ran into a new error:

$ docker run --rm --volumes-from 39e71e660eda -v $(pwd):/backup postgres:10.1-alpine tar:local cvf /backup/backup.tar /dbdata


/usr/local/bin/docker-entrypoint.sh: line 145: exec: tar:local: not found
pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • `ny_db_1` is a container name, the `docker run` syntax is `docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]` - replace that container name with an `IMAGE[:TAG]` that has `tar` installed (i.e. `ubuntu:18.04`) and you should be good. The idea is to run a new container from an image mounting the volumes from an existing container then to execute a command inside the new container to backup the data. – masseyb Sep 17 '19 at 06:51

2 Answers2

3

The docker run syntax is docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] - ny_db_1 is the name of your container, docker will attempt to use the IMAGE "ny_db_1" which does not exist hence the error: "Unable to find image 'ny_db_1:latest' locally" (latest is the default [:TAG] if none is specified).

--volumes-from will mount volumes from the specified container(s) into a new container spawned from IMAGE[:TAG] for example: docker run --rm --volumes-from db -v $(pwd):/backup ubuntu:18.04 tar czvf /backup/backup.tar /dbdata

Note: if you're backing up a PostgreSQL database then imho you'd be better off using the appropriate tools to backup and restore the database for example:

Backup using pg_dumpall:

docker run --rm \
    --name db-backup \
    --entrypoint pg_dumpall \
    --volume ${PWD}/backup:/backup \
    --volumes-from db \
    postgres:9 --host /var/run/postgresql --username postgres --clean --oids --file /backup/db.dump

Restore using psql:

docker run --rm -it \
    -v ${PWD}/backup:/restore \
    --name restore \
    postgres:10.1-alpine

docker exec restore psql \
    --host /var/run/postgresql \
    --username postgres \
    --file /restore/db.dump postgres

docker rename restore NEW_NAME
masseyb
  • 3,745
  • 1
  • 17
  • 29
2

try this command here:

docker run -it --rm -v ny_postgres_data:/volume -v /tmp:/backup ny_db_1 \
    tar -cjf /backup/ny_postgres_data -C /volume ./
LinPy
  • 16,987
  • 4
  • 43
  • 57