Skip to content

Instantly share code, notes, and snippets.

@swamibluedata
Last active July 25, 2016 16:17
Show Gist options
  • Save swamibluedata/b543a081f8cff36493069ea7769e770c to your computer and use it in GitHub Desktop.
Save swamibluedata/b543a081f8cff36493069ea7769e770c to your computer and use it in GitHub Desktop.
# Set the container name here
INSTANCE_NAME="bluedata-400"
# Extract the device num from the instance name
INSTANCE_DEV_ID=$(echo $INSTANCE_NAME | cut -d"-" -f2)
# device name to use for the snapshot
SNAP_NAME=$INSTANCE_NAME-snap
# To create a snapshot device, we need to specify a unique device id. This has to be 24-bit number
# Going to start from 16484 as the base
UNIQUE_DEVICE_BASE_ID="16384"
# Setup unique device id
UNIQUE_DEVICE_ID="$(( $UNIQUE_DEVICE_BASE_ID+$INSTANCE_DEV_ID ))"
# Get the docker device mapper pool information.
POOL=$(docker info | grep pool | awk '{print $3}')
DEVMAPPER_POOL="/dev/mapper/$POOL"
# Extract the container id.
CONT_ID=$(docker inspect -f {{.Id}} $INSTANCE_NAME)
# Get the size and the device id for this container
METADATA_FILE="/var/lib/docker/devicemapper/metadata/$CONT_ID"
SIZE=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj['size']")
DEVICE_ID=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj['device_id']")
# Temporarily suspend the pool to take snapshot and resume it
dmsetup suspend $DEVMAPPER_POOL
dmsetup message $DEVMAPPER_POOL 0 "create_snap $UNIQUE_DEVICE_ID $DEVICE_ID"
dmsetup resume $DEVMAPPER_POOL
dmsetup create $SNAP_NAME --table "0 $(($SIZE / 512)) thin $DEVMAPPER_POOL $UNIQUE_DEVICE_ID"
# We can do dd now, using 1M block size
time dd if=/dev/mapper/$SNAP_NAME conv=sync,noerror bs=128M | gzip -c > ~/$INSTANCE_NAME.tgz
# We can remove the snapshot now
dmsetup remove $INSTANCE_NAME-snap
dmsetup message $DEVMAPPER_POOL 0 "delete $UNIQUE_DEVICE_ID"
# For debug
ls -l ~/$INSTANCE_NANE.tgz
# For RESTORE
# Set the container name here
INSTANCE_NAME="bluedata-400"
# Extract the container id.
CONT_ID=$(docker inspect -f {{.Id}} $INSTANCE_NAME)
SNAP_NAME=$INSTANCE_NAME-snap
# Get the docker device mapper pool information.
POOL=$(docker info | grep pool | awk '{print $3}')
DEVMAPPER_POOL="/dev/mapper/$POOL"
# Get the size and the device id for this container
METADATA_FILE="/var/lib/docker/devicemapper/metadata/$CONT_ID"
SIZE=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj['size']")
DEVICE_ID=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj['device_id']")
# Stop the container
docker stop $INSTANCE_NAME
# Create a device using the same device_id
dmsetup create "$SNAP_NAME" --table "0 $(($SIZE / 512)) thin $DEVMAPPER_POOL $DEVICE_ID"
gunzip -c ~/$INSTANCE_NAME.tgz | dd of=/dev/mapper/$SNAP_NAME
dmsetup remove $SNAP_NAME
# Use instance_reboot function to restart the container
docker start $INSTANCE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment