Skip to content

Instantly share code, notes, and snippets.

@lanrat
Created January 7, 2016 21:17
Show Gist options
  • Select an option

  • Save lanrat/b6ee25568febfca3d3d1 to your computer and use it in GitHub Desktop.

Select an option

Save lanrat/b6ee25568febfca3d3d1 to your computer and use it in GitHub Desktop.
Backup Docker container configurations and volumes
#! /usr/bin/env bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DATE=$(date +%Y-%m-%d)
BACKUP_DIR=${DIR}/${DATE}
echo "Backing up containers"
mkdir -p $BACKUP_DIR/containers/
for container in $(docker ps -qa)
do
name=$(docker inspect --format '{{.Name}}' $container | sed -e 's/\///')
echo "$name"
docker inspect $name > $BACKUP_DIR/containers/${name}.json
done
echo "Backing up volumes"
mkdir -p $BACKUP_DIR/volumes/
for volume in $(docker volume ls -q)
do
echo "$volume"
docker run --rm -v ${volume}:/data:ro -v $BACKUP_DIR/volumes:/backup debian tar -czf /backup/${volume}.tgz -C /data .
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment