Proxmox scheduled snapshot creation

This post is not finished, more text will be provided soon.

The following script creates a snapshot of all running VMs on a Proxmox node, it might be suitable for scheduling by cron

#!/usr/bin/env bash
#
# Date: 2017-05-31
# Version: 1.0
# Author: Stellan Nordenbro <stellan.nordenbro@gmail.com>
#
# The MIT License (MIT)
#
# Copyright (c) 2017 Stellan Nordenbro
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

# Quick-check before we allow bad things to happen
if [ -z "${BASH_VERSINFO}" ]; then
  echo "ERROR: You must execute this script with BASH"
  exit 255
fi

# Go to the correct folder
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR

# The usage text
usage() { 
  echo "Usage: `basename $0` [-hdqS]" 1>&2
  echo
  echo "Optional parameters:" 1>&2
  echo " -h             Displays this message" 1>&2
  echo " -d             Dry-run: no commands will be executed" 1>&2
  echo " -q             Quiet: output to stdout will be hidden" 1>&2
  echo " -S             Silent: output to both stdout and stderr will be hidden" 1>&2
  echo
  exit 1; 
}

# Parse commandline
ALLARGS="$@"
set -- $(getopt hdqS -- "$@")
while true;
do
    case "$1" in
                (-h) usage;;
		(-d) DRY_RUN=yes;;
                (-q) QUIET=yes;;
                (-S) SILENT=yes;;
                (--) ;;
                (-*) echo "Error: unrecognized option $1" 1>&2; exit 1;;
                (*)  break;;
    esac
    shift
done
 
# send all stdout to /dev/null
if [ "${QUIET}" = "yes" ] || [ "${SILENT}" = "yes" ]; then
        exec 1> /dev/null
fi
 
# send all stdout and stderr to /dev/null
if [ "${SILENT}" = "yes" ]; then
        exec 2> /dev/null
fi

################################################################################
# FUNCTIONS
################################################################################
function GetVMName() {
	if [ $# -ne 1 ]; then
		echo "ERROR: ${FUNCNAME} expected 1 parameters: Usage ${FUNCNAME} VM_ID"
		exit 1
	fi
	
	value=$(qm config $1 | grep "name" | awk '{ print $2}')
	echo "$value"
}

function CreateVMSnapshot() {
	if [ $# -ne 1 ]; then
		echo "ERROR: ${FUNCNAME} expected 1 parameters: Usage ${FUNCNAME} VM_ID"
		exit 1
	fi
	
	VM_ID="$1"
	VM_NAME=$(GetVMName ${VM_ID})
	SNAPSHOT_NAME="AS_$(date +"%Y%m%d_%H%M")"
	SNAPSHOT_DESCRIPTION="Automated snapshot by script"
	
	echo "* Creating snapshot ${SNAPSHOT_NAME} on virtual machine ${VM_NAME} with id ${VM_ID}..."
	if [ "${DRY_RUN}" = "yes" ]; then
        	echo "   DRY-RUN:   qm snapshot ${VM_ID} \"${SNAPSHOT_NAME}\" -description \"${SNAPSHOT_DESCRIPTION}\" -vmstate 0"
	else
        	qm snapshot ${VM_ID} "${SNAPSHOT_NAME}" -description "${SNAPSHOT_DESCRIPTION}" -vmstate 0
        	snapshot_count=$(qm listsnapshot ${VM_ID} | awk '{print $1}' | grep "${SNAPSHOT_NAME}" | wc -l)
        	if [ $snapshot_count != 1 ]; then
        	        echo "ERROR: Snaphot creation failed on virtual machine ${VM_NAME} with id ${VM_ID}"
        	        exit 1
        	fi
	fi
	echo -e "* Done creating snapshot ${SNAPSHOT_NAME} on virtual machine ${VM_NAME} with id ${VM_ID}.\n\n"
}

################################################################################
# CONFIGURATIOM
################################################################################
HOSTNAME=$(hostname)

echo "Creating scheduled snapshots of virtual machines on $HOSTNAME"
echo -e "-------------------------------------------------------\n\n"

################################################################################
# SNAPSHOT
################################################################################

echo "* Retrieving running VMs"
RUNNING_VMS=($(qm list | grep -v VMID | grep "running" | awk '{print $1}'))
echo "* Done."

echo "* Creating snapshot for running VMs"
for VM_ID in "${RUNNING_VMS[@]}"; do
	CreateVMSnapshot "$VM_ID"
done
echo "* Done creating VM snapshots"

echo "Script execution done."

If anyone else have use for this script then go ahead, no guarantees of course, use it on your own risk…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s