Assuming we already have volumes, first let's show Docker volumes list
docker volume ls
Then we have two options, according to our methods of storing backups: we can pack volume with Docker tools or use operating system tools, as far as Docker volume represented by specific directory.
To make Docker volume backup with Docker tools:
docker run \
--rm --volumes-from CONTAINER_NAME \
-v $(pwd):/backup DESIRED_IMAGE \
tar cvfz /backup/ARCHIVE_NAME_`date +"%Y_%m_%d"`.tar \
MOUNT_POINT_IN_THE_CONTAINER
CONTAINER_NAME - our container
DESIRED_IMAGE - an image to be used as temporary environment, this may be ubuntu, busybox or whatever
ARCHIVE_NAME - our backup name, goes with `date +"%Y_%m_%d"` for created date mark
MOUNT_POINT_IN_THE_CONTAINER - mount point inside our container, where data stored
Official documentation on this topic. I think it needs some explanation at least for first use.
To make Docker volume backup by archiving a directory:
Let's find out where the volume directory is placed:
docker inspect VOLUME_NAME | grep Mountpoint
We should receive something like this. For example, if we are dealing with mysql container:
"Mountpoint": "/var/lib/docker/volumes/project_name/_data",
Now, when we have a path, let's archive it:
tar -cvzf ARCHIVE_NAME_`date +"%Y_%m_%d"`.tar PATH
ARCHIVE_NAME - our backup name, goes with `date +"%Y_%m_%d"` for created date mark
PAH - directory path from Mountpoint