Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This script will delete snapshots that has the correct prefix and old enough date string in it.
- # Used to get the age of snapshots to delete.
- # This has to be understood by (date --date STRING)
- age_to_delete="2 weeks ago"
- # The prefix to add and delete
- prefix="pve-autosnapshot"
- # Script
- ###################
- # Creating the date to compare to.
- the_past=$(date +%F_%T -d "$age_to_delete" | grep -o "[0-9]" | tr -d "\n")
- # Full Prefix
- prefix_search="$(echo -n "${prefix}" | tr "[:cntrl:][:punct:][:space:]" "_")"
- # Create snapshots for containers
- # Containers dont have snapshot support yet.
- # Create snapshots for virtual machines
- for vm_id in $(qm list | grep -oE "^[ ]*[0-9]{3}" | tr -d " "); do
- err=false
- for snapshot_id in $(qm listsnapshot "${vm_id}" | cut -d" " -f1 | grep -i "${prefix_search}"); do
- vm_id_time="$(echo "$snapshot_id" | grep -o "[0-9]" | tr -d "\n")"
- re='^-?[0-9]+$'
- if [[ ${vm_id_time} =~ $re ]] ; then
- if [ ${vm_id_time} -lt ${the_past} ] ; then
- out_put="$(qm delsnapshot "${vm_id}" "${snapshot_id}" 2>&1)"
- if [ $? -gt 0 ] && [[ "$out_put" != *"a template"* ]]; then
- echo "Error for VM ID: $vm_id SNAP ID: $snapshot_id"
- echo -e " Msg:\n $out_put"
- err=true
- fi
- fi
- fi
- done
- $err && echo "-------------------"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement