4
0

command_backup.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/bin/bash
  2. # LinuxGSM command_backup.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Creates a .tar.gz file in the backup directory.
  7. commandname="BACKUP"
  8. commandaction="Backup"
  9. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_firstcommand_set
  11. # Trap to remove lockfile on quit.
  12. fn_backup_trap() {
  13. echo -e ""
  14. echo -en "backup ${backupname}.${compressext}..."
  15. fn_print_canceled_eol_nl
  16. fn_script_log_info "Backup ${backupname}.${compressext}: CANCELED"
  17. rm -f "${backupdir:?}/${backupname}.${compressext}" | tee -a "${lgsmlog}"
  18. echo -en "backup ${backupname}.${compressext}..."
  19. fn_print_removed_eol_nl
  20. fn_script_log_info "Backup ${backupname}.${compressext}: REMOVED"
  21. # Remove backup lockfile.
  22. rm -f "${lockdir:?}/backup.lock"
  23. fn_backup_start_server
  24. unset exitbypass
  25. core_exit.sh
  26. }
  27. # Check if a backup is pending or has been aborted using backup.lock.
  28. fn_backup_check_lockfile() {
  29. # Remove stale lockfile.
  30. if [ -f "${lockdir}/backup.lock" ]; then
  31. if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
  32. fn_print_dots "Lockfile found: "
  33. fn_print_checking_eol
  34. fn_print_warn "Lockfile found: Removing stale lockfile: "
  35. fn_print_warn_eol
  36. fn_script_log_warn "Lockfile found: Removing stale lockfile"
  37. rm -f "${lockdir:?}/backup.lock"
  38. fi
  39. fi
  40. if [ -f "${lockdir}/backup.lock" ]; then
  41. fn_print_info_nl "Lockfile found: Backup is currently running"
  42. fn_script_log_error "Lockfile found: Backup is currently running: ${lockdir}/backup.lock"
  43. core_exit.sh
  44. fi
  45. }
  46. # Initialisation.
  47. fn_backup_init() {
  48. # Backup file name with selfname and current date.
  49. backupname="${selfname}-$(date '+%Y-%m-%d-%H%M%S')"
  50. info_distro.sh
  51. fn_print_dots "Starting backup"
  52. fn_script_log_info "Starting backup"
  53. if [ ! -d "${backupdir}" ] || [ "${backupcount}" == "0" ]; then
  54. fn_print_info_nl "Starting backup: No previous backups found"
  55. fn_script_log_info "No previous backups found"
  56. else
  57. fn_print_info_nl "Starting backup: Previous backups found"
  58. fn_script_log_info "Previous backups found"
  59. if [ "${lastbackupdaysago}" == "0" ]; then
  60. daysago="less than 1 day ago"
  61. elif [ "${lastbackupdaysago}" == "1" ]; then
  62. daysago="1 day ago"
  63. else
  64. daysago="${lastbackupdaysago} days ago"
  65. fi
  66. echo -e "* Previous backup was created ${daysago}, total size ${lastbackupsize}"
  67. fi
  68. }
  69. # Check if server is started and whether to stop it.
  70. fn_backup_stop_server() {
  71. check_status.sh
  72. # Server is running but will not be stopped.
  73. if [ "${stoponbackup}" == "off" ]; then
  74. fn_print_warn_nl "${selfname} is currently running"
  75. echo -e "* Although unlikely; creating a backup while ${selfname} is running might corrupt the backup."
  76. fn_script_log_warn "${selfname} is currently running"
  77. fn_script_log_warn "Although unlikely; creating a backup while ${selfname} is running might corrupt the backup"
  78. # Server is running and will be stopped if stoponbackup=on or unset.
  79. # If server is started
  80. elif [ "${status}" != "0" ]; then
  81. fn_print_restart_warning
  82. startserver="1"
  83. exitbypass=1
  84. command_stop.sh
  85. fn_firstcommand_reset
  86. fi
  87. }
  88. # Create required folders.
  89. fn_backup_dir() {
  90. # Create backupdir if it doesn't exist.
  91. if [ ! -d "${backupdir}" ]; then
  92. mkdir -p "${backupdir}"
  93. fi
  94. }
  95. fn_backup_create_lockfile() {
  96. # Create lockfile.
  97. date '+%s' > "${lockdir:?}/backup.lock"
  98. fn_script_log_info "Backup lockfile generated"
  99. fn_script_log_info "${lockdir}/backup.lock"
  100. # trap to remove lockfile on quit.
  101. trap fn_backup_trap INT
  102. }
  103. fn_select_compression() {
  104. if command -v zstd > /dev/null 2>&1; then
  105. compressprog="zstd"
  106. compressext="tar.zst"
  107. compressflag="--zstd"
  108. elif command -v pigz > /dev/null 2>&1; then
  109. compressprog="pigz"
  110. compressext="tar.gz"
  111. compressflag="--use-compress-program=pigz"
  112. elif command -v gzip > /dev/null 2>&1; then
  113. compressprog="gzip"
  114. compressext="tar.gz"
  115. compressflag="--gzip"
  116. else
  117. compressprog=""
  118. compressext="tar"
  119. compressflag=""
  120. fi
  121. }
  122. # Compressing files.
  123. fn_backup_compression() {
  124. fn_print_info "A total of ${rootdirduexbackup} will be compressed."
  125. fn_script_log_info "A total of ${rootdirduexbackup} will be compressed: ${backupdir}/${backupname}.${compressext}"
  126. fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.${compressext}, in progress ..."
  127. fn_script_log_info "Backup ${rootdirduexbackup} ${backupname}.${compressext}, in progress"
  128. excludedir=$(fn_backup_relpath)
  129. if [ ! -d "${excludedir}" ]; then
  130. fn_print_fail_nl "Problem identifying the previous backup directory for exclusion."
  131. fn_script_log_fail "Problem identifying the previous backup directory for exclusion"
  132. core_exit.sh
  133. fi
  134. if [ -n "${compressflag}" ]; then
  135. tar ${compressflag} -hcf "${backupdir}/${backupname}.${compressext}" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" --exclude "${tmpdir}" ./.
  136. else
  137. tar -hcf "${backupdir}/${backupname}.${compressext}" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" --exclude "${tmpdir}" ./.
  138. fi
  139. exitcode=$?
  140. if [ "${exitcode}" -ne 0 ]; then
  141. fn_print_fail_eol
  142. fn_script_log_fail "Backup in progress: FAIL"
  143. echo -e "${extractcmd}" | tee -a "${lgsmlog}"
  144. fn_print_fail_nl "Starting backup"
  145. fn_script_log_fail "Starting backup"
  146. else
  147. fn_print_ok_eol
  148. fn_print_ok "Completed: ${italic}${backupname}.${compressext}${default}, total size $(du -sh "${backupdir}/${backupname}.${compressext}" | awk '{print $1}')"
  149. fn_script_log_pass "Backup created: ${backupname}.${compressext}, total size $(du -sh "${backupdir}/${backupname}.${compressext}" | awk '{print $1}')"
  150. alert="backup"
  151. alert.sh
  152. fi
  153. }
  154. # Clear old backups according to maxbackups and maxbackupdays variables.
  155. fn_backup_prune() {
  156. # Clear if backup variables are set.
  157. if [ "${maxbackups}" ] && [ -n "${maxbackupdays}" ]; then
  158. # How many backups there are.
  159. info_distro.sh
  160. # How many backups exceed maxbackups.
  161. backupquotadiff=$((backupcount - maxbackups))
  162. # How many backups exceed maxbackupdays.
  163. backupsoudatedcount=$(find "${backupdir}"/ -type f -name "*.tar.*" -mtime +"${maxbackupdays}" | wc -l)
  164. # If anything can be cleared.
  165. if [ "${backupquotadiff}" -gt "0" ] || [ "${backupsoudatedcount}" -gt "0" ]; then
  166. fn_print_dots "Pruning"
  167. fn_script_log_info "Backup pruning activated"
  168. fn_print_ok_nl "Pruning"
  169. # If maxbackups greater or equal to backupsoutdatedcount, then it is over maxbackupdays.
  170. if [ "${backupquotadiff}" -ge "${backupsoudatedcount}" ]; then
  171. # Display how many backups will be cleared.
  172. echo -e "* Pruning: ${backupquotadiff} backup(s) has exceeded the ${maxbackups} backups limit"
  173. fn_script_log_info "Pruning: ${backupquotadiff} backup(s) has exceeded the ${maxbackups} backups limit"
  174. fn_sleep_time_1
  175. fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)"
  176. fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
  177. # Clear backups over quota.
  178. find "${backupdir}"/ -type f -name "*.tar.*" -printf '%T@ %p\n' | sort -rn | tail -${backupquotadiff} | cut -f2- -d" " | xargs rm
  179. fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
  180. fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
  181. # If maxbackupdays is used over maxbackups.
  182. elif [ "${backupquotadiff}" -lt "${backupsoudatedcount}" ]; then
  183. # Display how many backups will be cleared.
  184. echo -e "* Pruning: ${backupsoudatedcount} backup(s) are older than ${maxbackupdays} days."
  185. fn_script_log_info "Pruning: ${backupsoudatedcount} backup(s) older than ${maxbackupdays} days."
  186. fn_sleep_time_1
  187. fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)."
  188. fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
  189. # Clear backups over quota
  190. find "${backupdir}"/ -type f -mtime +"${maxbackupdays}" -exec rm -f {} \;
  191. fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
  192. fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
  193. fi
  194. fi
  195. fi
  196. }
  197. fn_backup_relpath() {
  198. # Written by CedarLUG as a "realpath --relative-to" alternative in bash.
  199. # Populate an array of tokens initialized from the rootdir components.
  200. mapfile -t rdirtoks < <(readlink -f "${rootdir}" | sed "s/\//\n/g")
  201. if [ ${#rdirtoks[@]} -eq 0 ]; then
  202. fn_print_fail_nl "Problem assessing rootdir during relative path assessment"
  203. fn_script_log_fail "Problem assessing rootdir during relative path assessment: ${rootdir}"
  204. core_exit.sh
  205. fi
  206. # Populate an array of tokens initialized from the backupdir components.
  207. mapfile -t bdirtoks < <(readlink -f "${backupdir}" | sed "s/\//\n/g")
  208. if [ ${#bdirtoks[@]} -eq 0 ]; then
  209. fn_print_fail_nl "Problem assessing backupdir during relative path assessment"
  210. fn_script_log_fail "Problem assessing backupdir during relative path assessment: ${rootdir}"
  211. core_exit.sh
  212. fi
  213. # Compare the leading entries of each array. These common elements will be clipped off.
  214. # for the relative path output.
  215. for ((base = 0; base < ${#rdirtoks[@]}; base++)); do
  216. [[ "${rdirtoks[$base]}" != "${bdirtoks[$base]}" ]] && break
  217. done
  218. # Next, climb out of the remaining rootdir location with updir references.
  219. for ((x = base; x < ${#rdirtoks[@]}; x++)); do
  220. echo -n "../"
  221. done
  222. # Climb down the remaining components of the backupdir location.
  223. for ((x = base; x < $((${#bdirtoks[@]} - 1)); x++)); do
  224. echo -n "${bdirtoks[$x]}/"
  225. done
  226. # In the event there were no directories left in the backupdir above to
  227. # traverse down, just add a newline. Otherwise at this point, there is
  228. # one remaining directory component in the backupdir to navigate.
  229. if (("$base" < "${#bdirtoks[@]}")); then
  230. echo -e "${bdirtoks[$((${#bdirtoks[@]} - 1))]}"
  231. else
  232. echo
  233. fi
  234. }
  235. # Start the server if it was stopped for the backup.
  236. fn_backup_start_server() {
  237. if [ -n "${startserver}" ]; then
  238. exitbypass=1
  239. command_start.sh
  240. fn_firstcommand_reset
  241. fi
  242. }
  243. fn_print_dots ""
  244. check.sh
  245. core_logs.sh
  246. fn_select_compression
  247. fn_backup_check_lockfile
  248. fn_backup_create_lockfile
  249. fn_backup_init
  250. fn_backup_stop_server
  251. fn_backup_dir
  252. fn_backup_compression
  253. fn_backup_prune
  254. fn_backup_start_server
  255. # Remove backup lockfile.
  256. rm -f "${lockdir:?}/backup.lock"
  257. core_exit.sh