4
0

command_backup.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/bin/bash
  2. # LinuxGSM command_backup.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Creates a .tar.gz file in the backup directory.
  7. commandname="BACKUP"
  8. commandaction="Backing up"
  9. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_firstcommand_set
  11. check.sh
  12. # Trap to remove lockfile on quit.
  13. fn_backup_trap() {
  14. echo -e ""
  15. echo -en "backup ${backupname}.tar.gz..."
  16. fn_print_canceled_eol_nl
  17. fn_script_log_info "Backup ${backupname}.tar.gz: CANCELED"
  18. rm -f "${backupdir:?}/${backupname}.tar.gz" | tee -a "${lgsmlog}"
  19. echo -en "backup ${backupname}.tar.gz..."
  20. fn_print_removed_eol_nl
  21. fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED"
  22. # Remove backup lockfile.
  23. rm -f "${lockdir:?}/backup.lock"
  24. fn_backup_start_server
  25. unset exitbypass
  26. core_exit.sh
  27. }
  28. # Check if a backup is pending or has been aborted using backup.lock.
  29. fn_backup_check_lockfile() {
  30. # Remove stale lockfile.
  31. if [ -f "${lockdir}/backup.lock" ]; then
  32. if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
  33. fn_print_dots "Lockfile found: "
  34. fn_print_checking_eol
  35. fn_print_warn "Lockfile found: Removing stale lockfile: "
  36. fn_print_warn_eol
  37. fn_script_log_warn "Lockfile found: Removing stale lockfile"
  38. rm -f "${lockdir:?}/backup.lock"
  39. fi
  40. fi
  41. if [ -f "${lockdir}/backup.lock" ]; then
  42. fn_print_info_nl "Lockfile found: Backup is currently running"
  43. fn_script_log_error "Lockfile found: Backup is currently running: ${lockdir}/backup.lock"
  44. core_exit.sh
  45. fi
  46. }
  47. # Initialisation.
  48. fn_backup_init() {
  49. # Backup file name with selfname and current date.
  50. backupname="${selfname}-$(date '+%Y-%m-%d-%H%M%S')"
  51. info_distro.sh
  52. fn_print_dots "Backup starting"
  53. fn_script_log_info "Backup starting"
  54. fn_print_ok_nl "Backup starting"
  55. if [ ! -d "${backupdir}" ] || [ "${backupcount}" == "0" ]; then
  56. fn_print_info_nl "There are no previous backups"
  57. else
  58. if [ "${lastbackupdaysago}" == "0" ]; then
  59. daysago="less than 1 day ago"
  60. elif [ "${lastbackupdaysago}" == "1" ]; then
  61. daysago="1 day ago"
  62. else
  63. daysago="${lastbackupdaysago} days ago"
  64. fi
  65. echo -e "* Previous backup was created ${daysago}, total size ${lastbackupsize}"
  66. fi
  67. }
  68. # Check if server is started and whether to stop it.
  69. fn_backup_stop_server() {
  70. check_status.sh
  71. # Server is running but will not be stopped.
  72. if [ "${stoponbackup}" == "off" ]; then
  73. fn_print_warn_nl "${selfname} is currently running"
  74. echo -e "* Although unlikely; creating a backup while ${selfname} is running might corrupt the backup."
  75. fn_script_log_warn "${selfname} is currently running"
  76. fn_script_log_warn "Although unlikely; creating a backup while ${selfname} is running might corrupt the backup"
  77. # Server is running and will be stopped if stoponbackup=on or unset.
  78. # If server is started
  79. elif [ "${status}" != "0" ]; then
  80. fn_print_restart_warning
  81. startserver="1"
  82. exitbypass=1
  83. command_stop.sh
  84. fn_firstcommand_reset
  85. fi
  86. }
  87. # Create required folders.
  88. fn_backup_dir() {
  89. # Create backupdir if it doesn't exist.
  90. if [ ! -d "${backupdir}" ]; then
  91. mkdir -p "${backupdir}"
  92. fi
  93. }
  94. # Migrate Backups from old dir before refactor
  95. fn_backup_migrate_olddir() {
  96. # Check if old backup dir is there before the refactor and move the backups
  97. if [ -d "${rootdir}/backups" ]; then
  98. if [ "${rootdir}/backups" != "${backupdir}" ]; then
  99. fn_print_dots "Backup directory is being migrated"
  100. fn_script_log_info "Backup directory is being migrated"
  101. fn_script_log_info "${rootdir}/backups > ${backupdir}"
  102. mv "${rootdir}/backups/"* "${backupdir}" 2> /dev/null
  103. exitcode=$?
  104. if [ "${exitcode}" == 0 ]; then
  105. rmdir "${rootdir}/backups" 2> /dev/null
  106. exitcode=$?
  107. fi
  108. if [ "${exitcode}" != 0 ]; then
  109. fn_print_error_nl "Backup directory is being migrated"
  110. fn_script_log_error "Backup directory is being migrated"
  111. else
  112. fn_print_ok_nl "Backup directory is being migrated"
  113. fn_script_log_pass "Backup directory is being migrated"
  114. fi
  115. fi
  116. fi
  117. }
  118. fn_backup_create_lockfile() {
  119. # Create lockfile.
  120. date '+%s' > "${lockdir:?}/backup.lock"
  121. fn_script_log_info "Backup lockfile generated"
  122. fn_script_log_info "${lockdir}/backup.lock"
  123. # trap to remove lockfile on quit.
  124. trap fn_backup_trap INT
  125. }
  126. # Compressing files.
  127. fn_backup_compression() {
  128. # Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue.
  129. fn_print_info "A total of ${rootdirduexbackup} will be compressed."
  130. fn_script_log_info "A total of ${rootdirduexbackup} will be compressed: ${backupdir}/${backupname}.tar.gz"
  131. fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.tar.gz, in progress..."
  132. fn_script_log_info "backup ${rootdirduexbackup} ${backupname}.tar.gz, in progress"
  133. excludedir=$(fn_backup_relpath)
  134. # Check that excludedir is a valid path.
  135. if [ ! -d "${excludedir}" ]; then
  136. fn_print_fail_nl "Problem identifying the previous backup directory for exclusion."
  137. fn_script_log_fatal "Problem identifying the previous backup directory for exclusion"
  138. core_exit.sh
  139. fi
  140. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" ./.
  141. local exitcode=$?
  142. if [ "${exitcode}" != 0 ]; then
  143. fn_print_fail_eol
  144. fn_script_log_fatal "Backup in progress: FAIL"
  145. echo -e "${extractcmd}" | tee -a "${lgsmlog}"
  146. fn_print_fail_nl "Starting backup"
  147. fn_script_log_fatal "Starting backup"
  148. else
  149. fn_print_ok_eol
  150. fn_print_ok_nl "Completed: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
  151. fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
  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.gz" -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
  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.gz" -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
  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. declare -a rdirtoks=($(readlink -f "${rootdir}" | sed "s/\// /g"))
  201. if [ ${#rdirtoks[@]} -eq 0 ]; then
  202. fn_print_fail_nl "Problem assessing rootdir during relative path assessment"
  203. fn_script_log_fatal "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. declare -a bdirtoks=($(readlink -f "${backupdir}" | sed "s/\// /g"))
  208. if [ ${#bdirtoks[@]} -eq 0 ]; then
  209. fn_print_fail_nl "Problem assessing backupdir during relative path assessment"
  210. fn_script_log_fatal "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. # Run functions.
  244. fn_backup_check_lockfile
  245. fn_backup_init
  246. fn_backup_stop_server
  247. fn_backup_dir
  248. fn_backup_migrate_olddir
  249. fn_backup_create_lockfile
  250. fn_backup_compression
  251. fn_backup_prune
  252. fn_backup_start_server
  253. # Remove backup lockfile.
  254. rm -f "${lockdir:?}/backup.lock"
  255. core_exit.sh