Skip to content

Instantly share code, notes, and snippets.

@dangmai
Last active October 11, 2018 16:40
Show Gist options
  • Save dangmai/f84278c39926239ab02fe89e25fffa69 to your computer and use it in GitHub Desktop.
Save dangmai/f84278c39926239ab02fe89e25fffa69 to your computer and use it in GitHub Desktop.
Send Docker Compose Volumes to a different host
#!/bin/bash
# It's best to do ssh-copy-id to the destination before running this script
# This script takes 3 params:
# The first is the prefix for the volume names, usually it's the docker compose dir name without special chars
# Second is the user@host ssh location
# Third optional: Replace the original prefix with a different prefix on the destrination
docker volume ls --format "{{.Name}}" | grep "^${1}" | while read vol
do
if [ -z "${3}" ]
then
dest_vol="$vol"
else
dest_vol="${vol/${1}/${3}}"
fi
docker run --rm -v ${vol}:/from alpine ash -c "cd /from ; tar -cf - . " | ssh "${2}" 'docker run --rm -i -v '"${dest_vol}"':/to alpine ash -c "cd /to ; tar -xpvf - " '
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment