Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker-Compose volume backup
#!/bin/bash
compose_file_path=$1
project_name=$2
backup_path=$3
function backup_volume {
volume_name=$1
backup_destination=$2
docker run --rm -v $volume_name:/data -v $backup_destination:/backup ubuntu tar -zcvf /backup/$volume_name.tar /data
}
echo "Stopping running containers"
docker-compose -f $compose_file_path stop
echo "Mounting volumes and performing backup..."
volumes=($(docker volume ls -f name=$project_name | awk '{if (NR > 1) print $2}'))
for v in "${volumes[@]}"
do
backup_volume $v $backup_path
done
echo "Restarting containers"
docker-compose -f $compose_file_path start
@hamzaabamboo
Copy link

hamzaabamboo commented Jul 18, 2019

Very nice and easy to use 👍

@tsotnesharvadze
Copy link

tsotnesharvadze commented Jun 19, 2020

It's necessary to stop containers?

@caneta
Copy link

caneta commented Sep 1, 2021

Nice...and how to restore?

@kiview
Copy link
Author

kiview commented Oct 11, 2021

@caneta Check out https://github.com/kiview/docker-volume-backup (not actively maintained, more like a PoC).

@WisZhou
Copy link

WisZhou commented Dec 16, 2021

Thank you for saving my life.

@Sieboldianus
Copy link

Sieboldianus commented Jan 4, 2022

Backup (i.e. core of the script above):

export VOLUME_NAME=flux_miniflux-db
export BACKUP_PATH=/srv/miniflux/backup
docker run --rm --volume $VOLUME_NAME:/data \
    -v $BACKUP_PATH:/backup ubuntu \
    tar -zcvf /backup/$VOLUME_NAME.tar /data

Restore (tested):

# cd into directory with volume as *tar
export VOLUME_NAME=flux_miniflux-db
docker run --rm  \
    --volume $VOLUME_NAME:/dbdata \
    --volume $(pwd):/backup ubuntu  \
    tar xvf /backup/$VOLUME_NAME.tar -C /dbdata --strip 1

Afterwards, check with

docker volume ls
>DRIVER    VOLUME NAME
>local     flux_miniflux-db

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment