Skip to content

Instantly share code, notes, and snippets.

@AndresPineros
Last active January 2, 2018 21:02
Show Gist options
  • Save AndresPineros/dffd858404976d010db8bcffc3cec010 to your computer and use it in GitHub Desktop.
Save AndresPineros/dffd858404976d010db8bcffc3cec010 to your computer and use it in GitHub Desktop.
Docker Named Volume Backup tool
#!/bin/bash
action=$1 #Backup or restore
volume_name=$2 #The name of the docker-compose volume to restore or backup.
output_dir=$3 #Where to read from (if restore) or to write to (if backup) the backup.
file_name=$4 #How the backup is (if restore) or will (if backup) be named.
function usage() {
echo "Usage: db (backup|restore) volume_name output_input_dir file_name"
exit
}
function backup() {
docker run --rm -v $volume_name:/tmpvolume -v $output_dir:/tmpbackup alpine tar cf /tmpbackup/$file_name.tar /tmpvolume
}
function restore() {
docker run --rm -v $volume_name:/tmpbackup -v $output_dir:/tmpvolume alpine tar xf /tmpvolume/$file_name.tar -C /tmpbackup --strip 1
}
if [ $# -ne 4 ]; then
usage
fi
case "$action" in
"backup" )
backup
;;
"restore" )
restore
;;
* )
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment