A bash shell script that takes and maintains a set number of external virtual machine snapshots using virsh snapshot and blockcommit.
The script was written to require the 4 following input parameters so the same script can be used with multiple virtual machines:
- virtual machine name / domain
- root directory containing subdirectories for each virtual machine
- the frequency of snapshots
- the number of snapshots to retain
There are 6 options for the frequency of snapshots: secondly, minutely, hourly, daily, weekly, and monthly. These options do not schedule the script, but only define a timestamp variable that is used for naming the new snapshot files so that conflicts are avoided.
The following example command assumes these input parameter values:
- the virtual machine name / domain is "debiantest"
- the base image file is located at "/mnt/Home/VirtualMachines/debiantest"
- the script will be triggered by a systemd service / timer to run "weekly"
- the desire is to have 3 snapshots, plus the base image file, at all times, so 2 snapshots will be retained
./autosnapcommit.sh "debiantest" "/mnt/Home/VirtualMachines" "weekly" 2
- Validate the virtual machine name / domain and file location
- Recognize errors and provide an exit path
- Create a log file and log messages throughout the process
- Validate and, if necessary, create the logs and snapshots directories
- Determine the virtual machine state
- Start the virtual machine, if it was not running
- Disable the virtual machine's AppArmor profile so that a blockcommit can be performed
- Determine whether a blockcommit to shorten the backing chain needs to be performed, and if necessary, perform it
- Create a new snapshot
- Shut down the virtual machine, if the initial state was shut off