Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.62 KB | None | 0 0
  1. #!/bin/bash
  2. # This script will delete snapshots that has the correct prefix and old enough date string in it.
  3.  
  4. # Used to get the age of snapshots to delete.
  5. # This has to be understood by (date --date STRING)
  6. age_to_delete="2 weeks ago"
  7.  
  8. # The prefix to add and delete
  9. prefix="pve-autosnapshot"
  10.  
  11. # Script
  12. ###################
  13.  
  14. # Creating the date to compare to.
  15. the_past=$(date +%F_%T -d "$age_to_delete" | grep -o "[0-9]" | tr -d "\n")
  16.  
  17. # Full Prefix
  18. prefix_search="$(echo -n "${prefix}" | tr "[:cntrl:][:punct:][:space:]" "_")"
  19.  
  20. # Create snapshots for containers
  21. # Containers dont have snapshot support yet.
  22.  
  23. # Create snapshots for virtual machines
  24. for vm_id in $(qm list | grep -oE "^[ ]*[0-9]{3}" | tr -d " "); do
  25.         err=false
  26.         for snapshot_id in $(qm listsnapshot "${vm_id}" | cut -d" " -f1 | grep -i "${prefix_search}"); do
  27.                 vm_id_time="$(echo "$snapshot_id" | grep -o "[0-9]" | tr -d "\n")"
  28.                 re='^-?[0-9]+$'
  29.                 if [[ ${vm_id_time} =~ $re ]] ; then
  30.                         if [ ${vm_id_time} -lt ${the_past} ] ; then
  31.                                 out_put="$(qm delsnapshot "${vm_id}" "${snapshot_id}" 2>&1)"
  32.                                 if [ $? -gt 0 ] && [[ "$out_put" != *"a template"* ]]; then
  33.                                         echo "Error for VM ID: $vm_id SNAP ID: $snapshot_id"
  34.                                         echo -e " Msg:\n  $out_put"
  35.                                         err=true
  36.                                 fi
  37.  
  38.                         fi
  39.                 fi
  40.         done
  41.         $err  && echo "-------------------"
  42. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement