command_backup.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. # Trap to remove lockfile on quit.
  12. fn_backup_trap(){
  13. echo ""
  14. echo -ne "backup ${backupname}.tar.gz..."
  15. fn_print_canceled_eol_nl
  16. fn_script_log_info "Backup ${backupname}.tar.gz: CANCELED"
  17. sleep 1
  18. rm -f "${backupdir}/${backupname}.tar.gz" | tee -a "${scriptlog}"
  19. echo -ne "backup ${backupname}.tar.gz..."
  20. fn_print_removed_eol_nl
  21. fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED"
  22. # Remove lock file
  23. rm -f "${tmpdir}/.backup.lock"
  24. core_exit.sh
  25. }
  26. # Check if a backup is pending or has been aborted using .backup.lock
  27. fn_backup_check_lockfile(){
  28. if [ -f "${tmpdir}/.backup.lock" ]; then
  29. fn_print_info_nl "Lock file found: Backup is currently running"
  30. fn_script_log_error "Lock file found: Backup is currently running: ${tmpdir}/.backup.lock"
  31. core_exit.sh
  32. fi
  33. }
  34. # Initialisation
  35. fn_backup_init(){
  36. # Backup file name with servicename and current date
  37. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  38. info_distro.sh
  39. fn_print_dots "Backup starting"
  40. fn_script_log_info "Backup starting"
  41. sleep 1
  42. fn_print_ok "Backup starting"
  43. sleep 1
  44. echo -ne "\n"
  45. if [ ! -d "${backupdir}" ]||[ "${backupcount}" == "0" ]; then
  46. fn_print_info_nl "There are no previous backups"
  47. else
  48. if [ "${lastbackupdaysago}" == "0" ]; then
  49. daysago="less than 1 day ago"
  50. elif [ "${lastbackupdaysago}" == "1" ]; then
  51. daysago="1 day ago"
  52. else
  53. daysago="${lastbackupdaysago} days ago"
  54. fi
  55. echo " * Previous backup was created ${daysago}, total size ${lastbackupsize}"
  56. sleep 1
  57. fi
  58. }
  59. # Check if server is started and wether to stop it
  60. fn_backup_stop_server(){
  61. check_status.sh
  62. # Server is stopped
  63. if [ "${status}" == "0" ]; then
  64. serverstopped="no"
  65. # Server is running and stoponbackup=off
  66. elif [ "${stoponbackup}" == "off" ]; then
  67. serverstopped="no"
  68. fn_print_warn_nl "${servicename} is currently running"
  69. echo " * Although unlikely; creating a backup while ${servicename} is running might corrupt the backup."
  70. fn_script_log_warn "${servicename} is currently running"
  71. fn_script_log_warn "Although unlikely; creating a backup while ${servicename} is running might corrupt the backup"
  72. # Server is running and will be stopped if stoponbackup=on or unset
  73. else
  74. fn_print_warn_nl "${servicename} will be stopped during the backup"
  75. fn_script_log_warn "${servicename} will be stopped during the backup"
  76. sleep 4
  77. serverstopped="yes"
  78. exitbypass=1
  79. command_stop.sh
  80. fi
  81. }
  82. # Create required folders
  83. fn_backup_dir(){
  84. # Create backupdir if it doesn't exist
  85. if [ ! -d "${backupdir}" ]; then
  86. mkdir -p "${backupdir}"
  87. fi
  88. }
  89. fn_backup_create_lockfile(){
  90. # Create lockfile
  91. date > "${tmpdir}/.backup.lock"
  92. fn_script_log_info "Lockfile generated"
  93. fn_script_log_info "${tmpdir}/.backup.lock"
  94. # trap to remove lockfile on quit.
  95. trap fn_backup_trap INT
  96. }
  97. # Compressing files
  98. fn_backup_compression(){
  99. # Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue
  100. fn_print_info "A total of ${rootdirduexbackup} will be compressed."
  101. fn_script_log_info "A total of ${rootdirduexbackup} will be compressed: ${backupdir}/${backupname}.tar.gz"
  102. sleep 2
  103. fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.tar.gz, in progress..."
  104. fn_script_log_info "backup ${rootdirduexbackup} ${backupname}.tar.gz, in progress"
  105. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "backups" ./*
  106. local exitcode=$?
  107. if [ ${exitcode} -ne 0 ]; then
  108. fn_print_fail_eol
  109. fn_script_log_fatal "Backup in progress: FAIL"
  110. echo "${tarcmd}" | tee -a "${scriptlog}"
  111. fn_print_fail_nl "Starting backup"
  112. fn_script_log_fatal "Starting backup"
  113. else
  114. fn_print_ok_eol
  115. sleep 1
  116. fn_print_ok_nl "Completed: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
  117. fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
  118. fi
  119. # Remove lock file
  120. rm -f "${tmpdir}/.backup.lock"
  121. }
  122. # Clear old backups according to maxbackups and maxbackupdays variables
  123. fn_backup_prune(){
  124. # Clear if backup variables are set
  125. if [ -n "${maxbackups}" ]&&[ -n "${maxbackupdays}" ]; then
  126. # How many backups there are
  127. info_distro.sh
  128. # How many backups exceed maxbackups
  129. backupquotadiff=$((backupcount-maxbackups))
  130. # How many backups exceed maxbackupdays
  131. backupsoudatedcount=$(find "${backupdir}"/ -type f -name "*.tar.gz" -mtime +"${maxbackupdays}"|wc -l)
  132. # If anything can be cleared
  133. if [ "${backupquotadiff}" -gt "0" ]||[ "${backupsoudatedcount}" -gt "0" ]; then
  134. fn_print_dots "Pruning"
  135. fn_script_log_info "Backup pruning activated"
  136. sleep 1
  137. fn_print_ok_nl "Pruning"
  138. sleep 1
  139. # If maxbackups greater or equal to backupsoutdatedcount, then it is over maxbackupdays
  140. if [ "${backupquotadiff}" -ge "${backupsoudatedcount}" ]; then
  141. # Display how many backups will be cleared
  142. echo " * Pruning: ${backupquotadiff} backup(s) has exceeded the ${maxbackups} backups limit"
  143. fn_script_log_info "Pruning: ${backupquotadiff} backup(s) has exceeded the ${maxbackups} backups limit"
  144. sleep 1
  145. fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)"
  146. fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
  147. sleep 1
  148. # Clear backups over quota
  149. find "${backupdir}"/ -type f -name "*.tar.gz" -printf '%T@ %p\n' | sort -rn | tail -${backupquotadiff} | cut -f2- -d" " | xargs rm
  150. fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
  151. fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
  152. # If maxbackupdays is used over maxbackups
  153. elif [ "${backupquotadiff}" -lt "${backupsoudatedcount}" ]; then
  154. # Display how many backups will be cleared
  155. echo " * Pruning: ${backupsoudatedcount} backup(s) are older than ${maxbackupdays} days."
  156. fn_script_log_info "Pruning: ${backupsoudatedcount} backup(s) older than ${maxbackupdays} days."
  157. sleep 1
  158. fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)."
  159. fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
  160. sleep 1
  161. # Clear backups over quota
  162. find "${backupdir}"/ -type f -mtime +"${maxbackupdays}" -exec rm -f {} \;
  163. fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
  164. fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
  165. fi
  166. sleep 1
  167. fi
  168. fi
  169. }
  170. # Restart the server if it was stopped for the backup
  171. fn_backup_start_server(){
  172. if [ "${serverstopped}" == "yes" ]; then
  173. exitbypass=1
  174. command_start.sh
  175. fi
  176. }
  177. # Run functions
  178. fn_backup_check_lockfile
  179. fn_backup_create_lockfile
  180. fn_backup_init
  181. fn_backup_stop_server
  182. fn_backup_dir
  183. fn_backup_compression
  184. fn_backup_prune
  185. fn_backup_start_server
  186. core_exit.sh