Created
May 6, 2020 23:50
-
-
Save flavio-fernandes/f2c18fed400b8a65e1918b1bc438ce35 to your computer and use it in GitHub Desktop.
Bash to create external snapshot. Inspired by https://blog.programster.org/kvm-external-snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o xtrace | |
set -o errexit | |
DOMAIN=${1:-$VM} | |
if [ -z "${DOMAIN}" ]; then | |
DOMAIN="$(basename $PWD)_default" | |
fi | |
# Check that we got a valid DOMAIN | |
virsh dominfo $DOMAIN >/dev/null || exit 1 | |
TIMESTAMP=$(date +%s) | |
SNAPSHOT_NAME=$TIMESTAMP | |
VM_FOLDER="${PWD}/.vagrant" | |
[ -d "${VM_FOLDER}/machines/default" ] || { echo 'Dont see expected VM_folder. Bailing...' >&2; exit 1; } | |
SNAPSHOT_FOLDER="${VM_FOLDER}/snapshots/${DOMAIN}/${TIMESTAMP}" | |
mkdir -pv ${SNAPSHOT_FOLDER} | tail -1 | |
MEM_FILE="${SNAPSHOT_FOLDER}/mem.qcow2" | |
DISK_FILE="${SNAPSHOT_FOLDER}/disk.qcow2" | |
# Find out if running or not | |
STATE=$(virsh domstate ${DOMAIN} 2>/dev/null | grep -q running && echo running ||:) | |
if [ "$STATE" = "running" ]; then | |
virsh snapshot-create-as \ | |
--domain $DOMAIN $SNAPSHOT_NAME \ | |
--diskspec vda,file=$DISK_FILE,snapshot=external \ | |
--memspec file=$MEM_FILE,snapshot=external \ | |
--atomic | |
else | |
virsh snapshot-create-as \ | |
--domain $DOMAIN $SNAPSHOT_NAME \ | |
--diskspec vda,file=$DISK_FILE,snapshot=external \ | |
--disk-only \ | |
--atomic | |
fi | |
echo ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment