简体   繁体   中英

Backup and restore docker named volume

I have question regarding volumes and ownership.

As an example I'm using this image: privatebin , but this is the same for any case.

First I'm creating volume:

$ docker volume create privatebin-data

From docker inspect, I can see where data is located:

$ docker inspect privatebin-data 
[
    {
        "CreatedAt": "2018-12-04T21:42:46+01:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/privatebin-data/_data",
        "Name": "privatebin-data",
        "Options": {},
        "Scope": "local"
    }
]

Following instructions from docker hub, I'm starting image:

$ docker run -d --restart="always" --read-only -p 8080:80 -v privatebin-data:/srv/data privatebin/nginx-fpm-alpine:1.1.1

Then I visit http://localhost:8080 and everything is working as expected.

Content of volume now:

$ ls -l /var/lib/docker/volumes/privatebin-data/_data
total 16
drwx------ 3 82 82 4096 Dec  4 21:49 73
-rw-r----- 1 82 82   46 Dec  4 21:49 purge_limiter.php
-rw-r----- 1 82 82  529 Dec  4 21:49 salt.php
-rw-r----- 1 82 82  131 Dec  4 21:49 traffic_limiter.php

I want to backup directory by archiving it:

tar -C /var/lib/docker/volumes/privatebin-data -czf privatebin-data-backup.tar.gz _data

My question is: Can I safely assume that if I restart image, for example on other server, user and group owner will still be 82? Is this proper way of backuping and restoring docker volumes?

The UID/GID come from inside your image, privatebin/nginx-fpm-alpine. So as long as you create users in the same way/order in there, and nothing changes in your base image, then those ID's will be the same regardless of where you run the image.

My preferred way to backup and restore volumes is to use a utility container, just in case the backend of docker changes, or you decide to move your named volume to another location or external data store. The commands to do that look like:

docker run --rm \
  -v privatebin-data:/source:ro \
  busybox tar -czC /source . >privatebin-data-backup.tar.gz

and

docker run --rm -i \
  -v privatebin-data:/target \
  busybox tar -xzC /target <privatebin-data-backup.tar.gz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM