command_backup.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. # LGSM command_backup.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. # Description: Creates a .tar.gz file in the backup directory.
  6. local commandname="BACKUP"
  7. local commandaction="Backup"
  8. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  9. check.sh
  10. fn_print_header
  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_warning_nl "A backup is currently running or has been aborted."
  16. fn_script_log_warn "A backup is currently running or has been aborted"
  17. if [ "${backupnoprompt}" == "1" ]; then
  18. # Exit if is in noprompt mode
  19. fn_print_error "Backup already in progress"
  20. fn_script_log_fatal "Backup already in progress"
  21. core_exit.sh
  22. else
  23. # Prompts user if in regular mode
  24. while true; do
  25. read -e -i "y" -p "Continue anyway? [Y/N]" yn
  26. case $yn in
  27. [Yy]* ) fn_script_log "User continues anyway"; break;;
  28. [Nn]* ) echo Exiting; fn_script_log "User aborted"; return;;
  29. * ) echo "Please answer yes or no.";;
  30. esac
  31. done
  32. echo ""
  33. fi
  34. fi
  35. }
  36. # Initialization
  37. fn_backup_init(){
  38. fn_print_dots ""
  39. sleep 0.5
  40. # Prepare backup file name with servicename current date
  41. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  42. # Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue
  43. info_distro.sh
  44. fn_print_info_nl "A total of ${rootdirduexbackup} will be compressed into the following backup:"
  45. fn_script_log "A total of ${rootdirduexbackup} will be compressed into the following backup: ${backupdir}/${backupname}.tar.gz"
  46. echo "${backupdir}/${backupname}.tar.gz"
  47. echo ""
  48. # Prompt to start the backup if not in noprompt mode
  49. if [ "${backupnoprompt}" != "1" ]; then
  50. while true; do
  51. read -e -i "y" -p "Continue? [Y/n]" yn
  52. case $yn in
  53. [Yy]* ) fn_script_log "User validates"; break;;
  54. [Nn]* ) echo "Exiting"; fn_script_log "User aborted"; return;;
  55. * ) echo "Please answer yes or no.";;
  56. esac
  57. done
  58. echo ""
  59. fi
  60. }
  61. # Check if server is started
  62. fn_backup_stop_server(){
  63. check_status.sh
  64. if [ "${status}" != "0" ]; then
  65. echo ""
  66. fn_print_warning_nl "${servicename} is currently running."
  67. fn_script_log_warn "${servicename} is currently running"
  68. sleep 0.5
  69. if [ "${backupnoprompt}" == "1" ]; then
  70. # Don't stop the server in noprompt mode
  71. serverstopped="no"
  72. else
  73. # Otherwise ask the user if it should be stopped or not
  74. while true; do
  75. read -e -i "n" -p "Stop ${servicename} while running the backup? [y/N]" yn
  76. case $yn in
  77. [Yy]* ) exitbypass=1; fn_script_log "User choose to stop the server"; command_stop.sh; serverstopped="yes"; break;;
  78. [Nn]* ) fn_script_log "User choose to not stop the server"; serverstopped="no"; break;;
  79. * ) echo "Please answer yes or no.";;
  80. esac
  81. done
  82. fi
  83. fi
  84. }
  85. # Create required folders
  86. fn_backup_directories(){
  87. fn_print_dots "Backup in progress, please wait..."
  88. fn_script_log_info "Initiating backup"
  89. sleep 0.5
  90. # Directories creation
  91. # Create backupdir if it doesn't exist
  92. if [ ! -d "${backupdir}" ]; then
  93. fn_print_info_nl "Creating ${backupdir}"
  94. fn_script_log_info "Creating ${backupdir}"
  95. mkdir "${backupdir}"
  96. fi
  97. # Create tmpdir if it doesn't exist
  98. if [ -n "${tmpdir}" ]&&[ ! -d "${tmpdir}" ]; then
  99. fn_print_info_nl "Creating ${tmpdir}"
  100. fn_script_log "Creating ${tmpdir}"
  101. mkdir -p "${tmpdir}"
  102. fi
  103. }
  104. # Create lockfile
  105. fn_backup_create_lockfile(){
  106. if [ -d "${tmpdir}" ]; then
  107. touch "${tmpdir}/.backup.lock"
  108. fn_script_log "Lockfile created"
  109. fi
  110. }
  111. # Compressing files
  112. fn_backup_compression(){
  113. fn_script_log "Compressing ${rootdirduexbackup}"
  114. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "backups" ./*
  115. fn_script_log "Compression over"
  116. }
  117. # Check tar exit code and set the result
  118. fn_check_tar_exit(){
  119. if [ $? == 0 ]; then
  120. backupresult="FAIL"
  121. else
  122. backupresult="PASS"
  123. fi
  124. }
  125. # Remove lockfile
  126. fn_backup_remove_lockfile(){
  127. if [ -d "${tmpdir}" ]&&[ -f "${tmpdir}/.backup.lock" ]; then
  128. rm "${tmpdir}/.backup.lock"
  129. fn_script_log "Lockfile removed"
  130. fi
  131. }
  132. fn_backup_summary(){
  133. # when backupresult="PASS"
  134. if [ "${backupresult}" == "PASS" ]; then
  135. fn_print_ok_nl "Backup created: ${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  136. fn_script_log_pass "Backup created: ${backupdir}/${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  137. # When backupresult="FAIL"
  138. elif [ "${backupresult}" == "PASS" ]; then
  139. fn_print_error_nl "Backup failed: ${backupname}.tar.gz"
  140. fn_script_log_error "Backup failed: ${backupname}.tar.gz"
  141. core_exit.sh
  142. else
  143. fn_print_error_nl "Could not determine compression result."
  144. fn_script_log_error "Could not determine compression result."
  145. core_exit.sh
  146. fi
  147. }
  148. # Clear old backups according to maxbackups and maxbackupdays variables
  149. fn_backup_clearing(){
  150. if [ -n "${backupdays}" ]; then
  151. # Count how many backups can be cleared
  152. backupclearcount=$(find "${backupdir}"/ -type f -mtime +"${backupdays}"|wc -l)
  153. # Check if there is any backup to clear
  154. if [ "${backupclearcount}" -ne "0" ]; then
  155. fn_print_info_nl "${backupclearcount} backups older than ${backupdays} days can be cleared."
  156. fn_script_log "${backupclearcount} backups older than ${backupdays} days can be cleared"
  157. if [ "${backupnoprompt}" != "1" ]; then
  158. while true; do
  159. read -p -e"Clear older backups? [Y/N]" yn
  160. case $yn in
  161. [Yy]* ) clearoldbackups="yes"; break;;
  162. [Nn]* ) clearoldbackups="no"; fn_script_log "Not clearing backups"; break;;
  163. * ) echo "Please answer yes or no.";;
  164. esac
  165. done
  166. else
  167. clearoldbackups="yes"
  168. fi
  169. # If user wants to clear backups or if noprompt
  170. if [ "${clearoldbackups}" == "yes" ]; then
  171. find "${backupdir}"/ -mtime +"${backupdays}" -type f -exec rm -f {} \;
  172. fn_print_ok_nl "Cleared ${backupclearcount} backups."
  173. fn_script_log_pass "Cleared ${backupclearcount} backups"
  174. fi
  175. else
  176. fn_script_log "No backups older than ${backupdays} days were found"
  177. fi
  178. else
  179. fn_script_log "No backups to clear since backupdays variable is empty"
  180. fi
  181. }
  182. # Restart the server if it was stopped for the backup
  183. fn_backup_start_back(){
  184. if [ "${serverstopped}" == "yes" ]; then
  185. exitbypass=1
  186. command_start.sh
  187. fi
  188. }
  189. # Run functions
  190. fn_check_pending_backup
  191. fn_backup_init
  192. fn_backup_stop_server
  193. fn_backup_directories
  194. fn_backup_create_lockfile
  195. fn_backup_compression
  196. fn_check_tar_exit
  197. fn_backup_remove_lockfile
  198. fn_backup_summary
  199. fn_backup_clearing
  200. fn_backup_start_back
  201. sleep 0.5
  202. core_exit.sh