#!/bin/bash # Please read the README.md distributed along this script #set -x ALL_VOLS=`docker volume ls --format '{{.Name}}'` TEMP_VOLS= SKIP_VOLS= VOLS= if [ "$#" -eq 0 ]; then echo "" echo "-----------------------------------------" echo "Usage: $0" echo "--single-vol [VOLUME] You can explicitly include a volume (can occur multiple times)" echo "--exclude-vol [VOLUME] You can explicitly exclude a volume (can occur multiple times)" echo "--all-vol This will backup all volumes (as reported by 'docker volume ls')" echo "-----------------------------------------" echo "" exit fi while [[ $# -gt 0 ]] do key="$1" case $key in --all-vol) TEMP_VOLS+=$ALL_VOLS shift ;; --single-vol) TEMP_VOLS+=" $2" shift shift ;; --exclude-vol) SKIP_VOLS+=" $2" shift shift ;; esac done #TODO: fix this inefficient shit! for v in $TEMP_VOLS; do SKIP="0" for t in $VOLS; do #echo "test $v agains $t" if [ "$t" == $v ]; then # echo "skip" SKIP=1 fi done for t in $SKIP_VOLS; do #echo "test $v agains $t" if [ "$t" == $v ]; then # echo "skip" SKIP=1 fi done if [ "$SKIP" == "0" ]; then # echo "add $v" VOLS+=" $v" fi done for v in $VOLS; do TIMESTAMP=`date +"%Y%m%d_%H%M%S"` FILENAME=${TIMESTAMP}_volume_$v.tar.gz echo " +--> backing up volume: $v to file: $FILENAME" docker run --rm -it -v $v:/IN:ro -v `pwd`:/OUT bash tar -C /IN -czf /OUT/$FILENAME . done