Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 312
Robert Maguda Avatar asked Dec 04 '18 21:12

Robert Maguda


People also ask

How do I backup my docker volume?

To back up a data volume you can run a new container using the volume you want to back up and executing the tar command to produce an archive of the volume content as described in the docker user guide. In your particular case, the data volume is used to store the data for a MySQL server.

Can you export docker volume?

If a volume is mounted on top of an existing directory in the container, docker export will export the contents of the underlying directory, not the contents of the volume. Refer to Backup, restore, or migrate data volumes in the user guide for examples on exporting data in a volume.

How do I copy a docker volume from one computer to another?

run two docker containers - one on the source the other on destination, each with one volume mounted - the source volume and destination volume. then run on the other of volumes rsync client that would connect to localhost:rsync , effective connecting via " socat pipe" to the rsync --daemon .

Where are docker named volumes stored?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).


1 Answers

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
like image 84
BMitch Avatar answered Sep 19 '22 13:09

BMitch