Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Configurables
  4. # exclude list rules:
  5. # * () runs task for all containers
  6. # * (2050) excludes container 2050
  7. # * (2050 2051) excludes container 2050 and 2051
  8. # * (all) excludes all containers
  9. timezone_exclude_list=()
  10. lxc_cleanup_exclude_list=(2050)
  11. backup_filename_tweak_exclude=(all)
  12.  
  13. # Job variables
  14. job_vmtype=`echo ${VMTYPE}` # will be one of: (qemu lxc)
  15. job_phase=`echo "$1"` # will be one of: (job-start job-end job-abort backup-start backup-end backup-abort log-end pre-stop pre-restart)
  16. job_type=`echo "$2"` # will be one of: (stop suspend snapshot)
  17. job_id=`echo "$3"` # container/VM ID
  18.  
  19. # Host variables
  20. hosttz=`cat /etc/timezone`
  21.  
  22. # Guest variables
  23. if [ "$job_vmtype" == "lxc" ]
  24. then
  25. pct_config_ostype=`pct config "$job_id" | grep ostype | cut -d ' ' -f2`
  26. pct_status=`pct status "$job_id" | cut -d ' ' -f2`
  27. fi
  28.  
  29. # additional variables
  30. debian_list=(debian ubuntu)
  31.  
  32. # LXC timezone setting
  33. if [ "$job_phase" == "backup-start" ]
  34. then
  35. # LXC
  36. if [ "$job_vmtype" == "lxc" ]
  37. then
  38. # excluded ID
  39. if (printf '%s\n' "${timezone_exclude_list[@]}" | grep -xq $job_id) || (printf '%s\n' "${timezone_exclude_list[@]}" | grep -xq all)
  40. then
  41. echo "Not updating Container time (excluded)"
  42. else
  43. # LXC state must be running
  44. if [[ "$pct_status" == "running" ]]
  45. then
  46. # debian/ubuntu Only
  47. if (printf '%s\n' "${debian_list[@]}" | grep -xq $pct_config_ostype)
  48. then
  49. lxctz=`pct exec "$job_id" -- bash -c "cat /etc/timezone"`
  50. if [ $hosttz != $lxctz ]
  51. then
  52. echo "Proxmox host timezone is $hosttz and the containers is $lxctz . fixing now"
  53. pct exec "$job_id" -- bash -c "echo $hosttz > /etc/timezone"
  54. else
  55. echo "Not updating Container time (already matching host)"
  56. fi
  57. else
  58. echo "Not updating Container time (unsupported OS)"
  59. # End OS type
  60. fi
  61. else
  62. echo "Not updating Container time (container not running)"
  63. # End state check
  64. fi
  65. # end of excluded check
  66. fi
  67. # End of LXC
  68. fi
  69. # End of backup-start
  70. fi
  71.  
  72.  
  73. # LXC apt clear
  74. if [ "$job_phase" == "backup-start" ]
  75. then
  76. # LXC
  77. if [ "$job_vmtype" == "lxc" ]
  78. then
  79. # excluded ID
  80. if (printf '%s\n' "${lxc_cleanup_exclude_list[@]}" | grep -xq $job_id) || (printf '%s\n' "${lxc_cleanup_exclude_list[@]}" | grep -xq all)
  81. then
  82. echo "Skipping pre backup guest cleanup (excluded)"
  83. else
  84. # LXC state must be running
  85. if [[ "$pct_status" == "running" ]]
  86. then
  87. # debian/ubuntu Only
  88. if (printf '%s\n' "${debian_list[@]}" | grep -xq $pct_config_ostype)
  89. then
  90. echo "Running pre backup apt clear"
  91. prespace=`pct exec "$job_id" -- bash -c "du -sh --block-size=1 /var/cache/apt/ | cut -f1"`
  92. pct exec "$job_id" -- bash -c "apt-get clean"
  93. postspace=`pct exec "$job_id" -- bash -c "du -sh --block-size=1 /var/cache/apt/ | cut -f1"`
  94. spacesavedbytes=`expr $postspace - $prespace`
  95. spacesavedbytes=${spacesavedbytes#-}
  96. spacesavedkb=`expr $spacesavedbytes / 1024`
  97. spacesavedmb=`expr $spacesavedbytes / 1024 / 1024`
  98. spacesavedgb=`expr $spacesavedbytes / 1024 / 1024 / 1024`
  99. if [[ "$spacesavedgb" -gt 0 ]]
  100. then
  101. spacesavedwords="$spacesavedgb GB"
  102. elif [[ "$spacesavedmb" -gt 0 ]]
  103. then
  104. spacesavedwords="$spacesavedmb MB"
  105. elif [[ "$spacesavedkb" -gt 0 ]]
  106. then
  107. spacesavedwords="$spacesavedkb KB"
  108. elif [[ "$spacesavedbytes" -gt 0 ]]
  109. then
  110. spacesavedwords="$spacesavedbytes Bytes"
  111. else
  112. spacesavedwords="zero space"
  113. fi
  114. echo "Pre backup guest cleanup saved $spacesavedwords"
  115. else
  116. echo "Skipping pre backup guest cleanup (unsupported OS)"
  117. # End OS type
  118. fi
  119. else
  120. echo "Skipping pre backup guest cleanup (container not running)"
  121. # End state check
  122. fi
  123. # end of excluded check
  124. fi
  125. # End of LXC
  126. fi
  127. # End of backup-start
  128. fi
  129.  
  130.  
  131. # Change backup filename
  132. if [ "$job_phase" == "backup-end" ]
  133. then
  134. # excluded ID
  135. if (printf '%s\n' "${backup_filename_tweak_exclude[@]}" | grep -xq $job_id) || (printf '%s\n' "${backup_filename_tweak_exclude[@]}" | grep -xq all)
  136. then
  137. echo "Skipping backup renaming (excluded)"
  138. else
  139. tarfile=${TARFILE}
  140. exten=${tarfile#*.}
  141. filename=${tarfile%.*.*}
  142. hostname=${HOSTNAME}
  143. hostname=`echo $hostname | cut -d "." -f1`
  144. newfilename="$filename-$hostname.$exten"
  145. echo "Renaming $tarfile to $newfilename"
  146. mv $tarfile $newfilename
  147. echo "Backup renaming successful"
  148. # end of excluded check
  149. fi
  150. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement