command_backup.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/bash
  2. # LGSM command_backup.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: Creates a .tar.gz file in the backup directory.
  7. local commandname="BACKUP"
  8. local commandaction="Backup"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. check.sh
  11. fn_script_log "Entering backup"
  12. # Check if a backup is pending or has been aborted using .backup.lock
  13. fn_check_pending_backup(){
  14. if [ -f "${tmpdir}/.backup.lock" ]; then
  15. fn_print_error "A backup is currently running or has been aborted."
  16. fn_print_info_nl "If you keep seing this message, remove the following file:"
  17. echo "${tmpdir}/.backup.lock"
  18. fn_script_log_fatal "A backup is currently running or has been aborted."
  19. fin_script_log_info "If you keep seing this message, remove the following file: ${tmpdir}/.backup.lock"
  20. core_exit.sh
  21. fi
  22. }
  23. # Initialization
  24. fn_backup_init(){
  25. fn_print_dots ""
  26. sleep 0.5
  27. # Prepare backup file name with servicename current date
  28. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  29. # Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue
  30. info_distro.sh
  31. fn_print_info_nl "A total of ${rootdirduexbackup} will be compressed into the following backup:"
  32. fn_script_log "A total of ${rootdirduexbackup} will be compressed into the following backup: ${backupdir}/${backupname}.tar.gz"
  33. echo "${backupdir}/${backupname}.tar.gz"
  34. }
  35. # Check if server is started and wether to stop it
  36. fn_backup_stop_server(){
  37. check_status.sh
  38. # Server is stopped
  39. if [ "${status}" == "0" ]; then
  40. serverstopped="no"
  41. # Server is up and stoponbackup is off
  42. elif [ "${stoponbackup}" == "off" ]; then
  43. serverstopped="no"
  44. fn_print_info_nl "${servicename} is started and will not be stopped."
  45. fn_print_information_nl "It is advised to stop the server to prevent a file changes and tar errors."
  46. fn_script_log_info "${servicename} is started during the backup"
  47. fn_script_log_info "It is advised to stop the server to prevent a file changes and tar errors."
  48. # Server is up and will be stopped if stoponbackup has no value or anything else than "off"
  49. else
  50. fn_print_warning_nl "${servicename} will be stopped during the backup."
  51. fn_script_log_warn "${servicename} will be stopped during the backup"
  52. sleep 4
  53. serverstopped="yes"
  54. exitbypass=1
  55. command_stop.sh
  56. fi
  57. }
  58. # Create required folders
  59. fn_backup_directories(){
  60. fn_print_dots "Backup in progress, please wait..."
  61. fn_script_log_info "Initiating backup"
  62. sleep 0.5
  63. # Directories creation
  64. # Create backupdir if it doesn't exist
  65. if [ ! -d "${backupdir}" ]; then
  66. fn_print_info_nl "Creating ${backupdir}"
  67. fn_script_log_info "Creating ${backupdir}"
  68. mkdir "${backupdir}"
  69. fi
  70. # Create tmpdir if it doesn't exist
  71. if [ -n "${tmpdir}" ]&&[ ! -d "${tmpdir}" ]; then
  72. fn_print_info_nl "Creating ${tmpdir}"
  73. fn_script_log "Creating ${tmpdir}"
  74. mkdir -p "${tmpdir}"
  75. fi
  76. }
  77. # Create lockfile
  78. fn_backup_create_lockfile(){
  79. if [ -d "${tmpdir}" ]; then
  80. touch "${tmpdir}/.backup.lock"
  81. fn_script_log "Lockfile created"
  82. fi
  83. }
  84. # Compressing files
  85. fn_backup_compression(){
  86. fn_script_log "Compressing ${rootdirduexbackup}"
  87. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "backups" ./*
  88. fn_script_log "Compression over"
  89. }
  90. # Check tar exit code and set the result
  91. fn_check_tar_exit(){
  92. if [ $? == 0 ]; then
  93. backupresult="PASS"
  94. else
  95. backupresult="FAIL"
  96. fi
  97. }
  98. # Remove lockfile
  99. fn_backup_remove_lockfile(){
  100. if [ -d "${tmpdir}" ]&&[ -f "${tmpdir}/.backup.lock" ]; then
  101. rm "${tmpdir}/.backup.lock"
  102. fn_script_log "Lockfile removed"
  103. fi
  104. }
  105. fn_backup_summary(){
  106. # when backupresult="PASS"
  107. if [ "${backupresult}" == "PASS" ]; then
  108. fn_print_ok_nl "Backup created: ${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  109. fn_script_log_pass "Backup created: ${backupdir}/${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  110. # When backupresult="FAIL"
  111. elif [ "${backupresult}" == "FAIL" ]; then
  112. fn_print_error_nl "Backup failed: ${backupname}.tar.gz"
  113. fn_script_log_error "Backup failed: ${backupname}.tar.gz"
  114. core_exit.sh
  115. else
  116. fn_print_error_nl "Could not determine compression result."
  117. fn_script_log_error "Could not determine compression result."
  118. core_exit.sh
  119. fi
  120. }
  121. # Clear old backups according to maxbackups and maxbackupdays variables
  122. fn_backup_clearing(){
  123. # How many backups there are
  124. backupscount=$(find "${backupdir}/" -type f -name "*.tar.gz"|wc -l)
  125. # How many backups exceed maxbackups
  126. backupquotadiff=$((backupscount-maxbackups))
  127. backupsoudatedcount=$(find "${backupdir}"/ -type f -name "*.tar.gz" -mtime +"${maxbackupdays}"|wc -l)
  128. # If backup variables are set
  129. if [ -n "${maxbackupdays}" ]&&[ -n "${maxbackups}" ]; then
  130. # If anything can be cleared
  131. if [ "${backupquotadiff}" -gt "0" ]||[ "${backupsoudatedcount}" -gt "0" ]; then
  132. # If maxbackups greater or equal than backupsoutdatedcount, then it is used over maxbackupdays
  133. if [ "${backupquotadiff}" -gt "${backupsoudatedcount}" ]||[ "${backupquotadiff}" -eq "${backupsoudatedcount}" ]; then
  134. # Display how many backups will be cleared
  135. echo "${backupquotadiff} backups will be clearned"
  136. fn_print_info_nl "${backupquotadiff} backups exceed max ${maxbackups} and will be cleared."
  137. fn_script_log "${backupquotadiff} backups exceed max ${maxbackups} and will be cleared"
  138. sleep 2
  139. # Clear over quota backups
  140. find "${backupdir}"/ -type f -name "*.tar.gz" -printf '%T@ %p\n' | sort -rn | tail -${backupquotadiff} | cut -f2- -d" " | xargs rm
  141. fn_print_ok_nl "Cleared ${backupquotadiff} backups."
  142. fn_script_log "Cleared ${backupquotadiff} backups"
  143. # If maxbackupdays is used over maxbackups
  144. elif [ "${backupquotadiff}" -lt "${backupsoudatedcount}" ]; then
  145. # Display how many backups will be cleared
  146. fn_print_info_nl "${backupsoudatedcount} backups older than ${maxbackupdays} days will be cleared."
  147. fn_script_log "${backupsoudatedcount} backups older than ${maxbackupdays} days will be cleared"
  148. find "${backupdir}"/ -mtime +"${maxbackupdays}" -type f -exec rm -f {} \;
  149. fn_print_ok_nl "Cleared ${backupsoudatedcount} backups."
  150. fn_script_log_pass "Cleared ${backupsoudatedcount} backups"
  151. else
  152. fn_script_log "No backups older than ${maxbackupdays} days were found"
  153. fi
  154. fi
  155. else
  156. fn_script_log "No backups to clear since maxbackupdays variable is empty"
  157. fi
  158. }
  159. # Restart the server if it was stopped for the backup
  160. fn_backup_start_back(){
  161. if [ "${serverstopped}" == "yes" ]; then
  162. exitbypass=1
  163. command_start.sh
  164. fi
  165. }
  166. # Run functions
  167. fn_check_pending_backup
  168. fn_backup_init
  169. fn_backup_stop_server
  170. fn_backup_directories
  171. fn_backup_create_lockfile
  172. fn_backup_compression
  173. fn_check_tar_exit
  174. fn_backup_remove_lockfile
  175. fn_backup_summary
  176. fn_backup_clearing
  177. fn_backup_start_back
  178. sleep 0.5
  179. core_exit.sh