Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Add getopt c68fc68 Sep 8, 2015
1 contributor

Users who have contributed to this file

executable file 137 lines (114 sloc) 4.22 KB
#!/bin/bash
usage ()
{
echo "Usage: pve-snap --mode [--storage STORAGENAME] [--leave NUMBER] [--sleep=NUMBER]"
echo
echo "Example: pve-snap --weekly --storage=ceph --leave=2 --sleep=10"
echo " or: pve-snap --yearly --storage=pve-data"
echo " or: pve-snap -d -s stor -l 4"
echo
echo "Arguments:"
echo " -d, --daily Run this script in mode for daily autosnapshots"
echo " -w, --weekly Run this script in mode for weekly autosnapshots"
echo " -m, --monthly Run this script in mode for monthly autosnapshots"
echo " -y, --yearly Run this script in mode for yearly autosnapshots"
echo " -s, --storage=STORAGENAME Specify the storage name for which will be used auto snapshots"
echo " (not specified will enable for all storages)"
echo " -l, --leave=NUMBER Specify the number of snapshots which should will leave, anything longer will be removed"
echo " -D, --sleep=NUMBER Specify the modifier for sleep that would create a delay after each snapshot operation"
echo " (calculate as: NUMBER gigabytes per minute, min 1 minute)"
echo
exit
}
loadkeys ()
{
OPTS=`getopt -o dwmysl: --long daily,weekly,monthly,yearly,storage,leave,sleep: -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
mode=
storage=
leave=
sleepmodofier=
while true; do
case "$1" in
-d | --daily ) mode=daily; shift ;;
-w | --weekly ) mode=weekly; shift ;;
-m | --monthly ) mode=monthly; shift ;;
-y | --yearly ) mode=yearly; shift ;;
-s | --storage ) storage="$2"; shift; shift ;;
-l | --leave ) leave="$2"; shift; shift ;;
-D | --sleep ) sleepmodifier="$2"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ "$mode" = "" ] ; then echo -e "\nMode is not set\n"; usage; break; fi
}
gensnapname ()
{
if [ $mode == "daily" ] ; then
snapprefix="autodaily"
snapname="$snapprefix$(date +%y%m%d)"
elif [ $mode == "weekly" ] ; then
snapprefix="autoweekly"
snapname="$snapprefix$(date +%y%m%d)"
elif [ $mode == "monthly" ] ; then
snapprefix="automonthly"
snapname="$snapprefix$(date +%y%m%d)"
elif [ $mode == "yearly" ] ; then
snapprefix="autoyearly"
snapname="$snapprefix$(date +%y%m%d)"
fi
}
getvms ()
{
vms=($(/usr/sbin/qm list | awk '{print$1}' | sed 1d))
}
cleansnapshots ()
{
if [[ "$leave" != "" ]] ; then
snapshots=($(grep -Po "(?<=\[).*(?=\])" "/etc/pve/nodes/$(hostname)/qemu-server/$vm.conf" | grep ^"$snapprefix" | tac))
i=0
for snap in "${snapshots[@]}"
do
if [ "$i" -ge "$leave" ] ; then
echo "vm$vm: Removing snapshot $snap"
/usr/sbin/qm delsnapshot $vm $snap
else
echo "vm$vm: Saving snapshot $snap"
fi
let i++
done
fi
}
sleepsnapshot ()
{
if [ "$sleepmodifier" != "" ] ; then
disksizes=($(sed '/^\[.*\]$/q' /etc/pve/nodes/$(hostname)/qemu-server/$vm.conf | grep "$storage:vm-$vm" | sed -r -e 's/.*size=[^0-9]*//g ; s/[^MG0-9]*//g'))
for disksize in "${disksizes[@]}"
do
if [[ $disksize == *"G" ]] ;
then disksize=($(echo $disksize | sed 's/[^0-9]//g'))
else disksize=$[($(echo $disksize | sed 's/[^0-9]//g'))/1024/1024/1024] ; fi
sleeptime=$[$disksize/$sleepmodifier]
if [ $sleeptime = 0 ] ; then sleeptime=1; fi
sleep ${sleeptime}m
done
fi
}
autosnapshot ()
{
for vm in "${vms[@]}"
do
storagecheck=($(sed '/^\[.*\]$/q' /etc/pve/nodes/$(hostname)/qemu-server/$vm.conf | grep -ci "$storage:[vm-]*$vm"))
if [[ "$storagecheck" > "0" ]] ; then
echo "vm$vm: Creating snapshot $snapname"
/usr/sbin/qm snapshot $vm $snapname
sleepsnapshot
cleansnapshots
fi
done
}
loadkeys $@
gensnapname
getvms
autosnapshot
You can’t perform that action at this time.