command_backup.sh 6.5 KB

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