command_backup.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash
  2. # LinuxGSM 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 "${lgsmlog}"
  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. excludedir=$(realpath --relative-to="${rootdir}" "${backupdir}")
  106. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" ./*
  107. local exitcode=$?
  108. if [ ${exitcode} -ne 0 ]; then
  109. fn_print_fail_eol
  110. fn_script_log_fatal "Backup in progress: FAIL"
  111. echo "${tarcmd}" | tee -a "${lgsmlog}"
  112. fn_print_fail_nl "Starting backup"
  113. fn_script_log_fatal "Starting backup"
  114. else
  115. fn_print_ok_eol
  116. sleep 1
  117. fn_print_ok_nl "Completed: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
  118. fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
  119. fi
  120. # Remove lock file
  121. rm -f "${tmpdir}/.backup.lock"
  122. }
  123. # Clear old backups according to maxbackups and maxbackupdays variables
  124. fn_backup_prune(){
  125. # Clear if backup variables are set
  126. if [ -n "${maxbackups}" ]&&[ -n "${maxbackupdays}" ]; then
  127. # How many backups there are
  128. info_distro.sh
  129. # How many backups exceed maxbackups
  130. backupquotadiff=$((backupcount-maxbackups))
  131. # How many backups exceed maxbackupdays
  132. backupsoudatedcount=$(find "${backupdir}"/ -type f -name "*.tar.gz" -mtime +"${maxbackupdays}"|wc -l)
  133. # If anything can be cleared
  134. if [ "${backupquotadiff}" -gt "0" ]||[ "${backupsoudatedcount}" -gt "0" ]; then
  135. fn_print_dots "Pruning"
  136. fn_script_log_info "Backup pruning activated"
  137. sleep 1
  138. fn_print_ok_nl "Pruning"
  139. sleep 1
  140. # If maxbackups greater or equal to backupsoutdatedcount, then it is over maxbackupdays
  141. if [ "${backupquotadiff}" -ge "${backupsoudatedcount}" ]; then
  142. # Display how many backups will be cleared
  143. echo " * Pruning: ${backupquotadiff} backup(s) has exceeded the ${maxbackups} backups limit"
  144. fn_script_log_info "Pruning: ${backupquotadiff} backup(s) has exceeded the ${maxbackups} backups limit"
  145. sleep 1
  146. fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)"
  147. fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
  148. sleep 1
  149. # Clear backups over quota
  150. find "${backupdir}"/ -type f -name "*.tar.gz" -printf '%T@ %p\n' | sort -rn | tail -${backupquotadiff} | cut -f2- -d" " | xargs rm
  151. fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
  152. fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
  153. # If maxbackupdays is used over maxbackups
  154. elif [ "${backupquotadiff}" -lt "${backupsoudatedcount}" ]; then
  155. # Display how many backups will be cleared
  156. echo " * Pruning: ${backupsoudatedcount} backup(s) are older than ${maxbackupdays} days."
  157. fn_script_log_info "Pruning: ${backupsoudatedcount} backup(s) older than ${maxbackupdays} days."
  158. sleep 1
  159. fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)."
  160. fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
  161. sleep 1
  162. # Clear backups over quota
  163. find "${backupdir}"/ -type f -mtime +"${maxbackupdays}" -exec rm -f {} \;
  164. fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
  165. fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
  166. fi
  167. sleep 1
  168. fi
  169. fi
  170. }
  171. # Restart the server if it was stopped for the backup
  172. fn_backup_start_server(){
  173. if [ "${serverstopped}" == "yes" ]; then
  174. exitbypass=1
  175. command_start.sh
  176. fi
  177. }
  178. # Run functions
  179. fn_backup_check_lockfile
  180. fn_backup_create_lockfile
  181. fn_backup_init
  182. fn_backup_stop_server
  183. fn_backup_dir
  184. fn_backup_compression
  185. fn_backup_prune
  186. fn_backup_start_server
  187. core_exit.sh