command_backup.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if [ -f "${tmpdir}/.backup.lock" ]; then
  14. fn_print_warning_nl "A backup is currently running or has been aborted."
  15. fn_script_log_warn "A backup is currently running or has been aborted"
  16. while true; do
  17. read -e -i "y" -p "Continue anyway? [Y/N]" yn
  18. case $yn in
  19. [Yy]* ) fn_script_log "User continues anyway"; break;;
  20. [Nn]* ) echo Exiting; fn_script_log "User aborted"; return;;
  21. * ) echo "Please answer yes or no.";;
  22. esac
  23. done
  24. echo ""
  25. fi
  26. fn_print_dots ""
  27. sleep 0.5
  28. # Prepare backup file name with servicename current date
  29. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  30. # Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue
  31. info_distro.sh
  32. fn_print_info_nl "A total of ${rootdirduexbackup} will be compressed into the following backup:"
  33. fn_script_log "A total of ${rootdirduexbackup} will be compressed into the following backup: ${backupdir}/${backupname}.tar.gz"
  34. echo "${backupdir}/${backupname}.tar.gz"
  35. echo ""
  36. while true; do
  37. read -e -i "y" -p "Continue? [Y/n]" yn
  38. case $yn in
  39. [Yy]* ) fn_script_log "User validates"; break;;
  40. [Nn]* ) echo "Exiting"; fn_script_log "User aborted"; return;;
  41. * ) echo "Please answer yes or no.";;
  42. esac
  43. done
  44. # Check if server is started
  45. check_status.sh
  46. if [ "${status}" != "0" ]; then
  47. echo ""
  48. fn_print_warning_nl "${servicename} is currently running."
  49. fn_script_log_warn "${servicename} is currently running"
  50. sleep 0.5
  51. while true; do
  52. read -e -i "n" -p "Stop ${servicename} while running the backup? [y/N]" yn
  53. case $yn in
  54. [Yy]* ) exitbypass=1; fn_script_log "User choose to stop the server"; command_stop.sh; serverstopped="yes"; break;;
  55. [Nn]* ) fn_script_log "User choose to not stop the server"; serverstopped="no"; break;;
  56. * ) echo "Please answer yes or no.";;
  57. esac
  58. done
  59. fi
  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. # Create lockfile
  77. if [ -d "${tmpdir}" ]; then
  78. touch "${tmpdir}/.backup.lock"
  79. fn_script_log "Lockfile created"
  80. fi
  81. # Compressing files
  82. fn_script_log "Compressing ${rootdirduexbackup}"
  83. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "backups" ./*
  84. fn_script_log "Compression over"
  85. # Remove lockfile
  86. if [ -d "${tmpdir}" ]; then
  87. rm "${tmpdir}/.backup.lock"
  88. fn_script_log "Lockfile removed"
  89. fi
  90. # Check tar exit code and act accordingly
  91. if [ $? == 0 ]; then
  92. # Exit code doesn't report any error
  93. fn_print_ok_nl "Backup created: ${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  94. fn_script_log_pass "Backup created: ${backupdir}/${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  95. # Clear old backups if backupdays variable exists
  96. if [ -n "${backupdays}" ]; then
  97. # Count how many backups can be cleared
  98. backupclearcount=$(find "${backupdir}"/ -type f -mtime +"${backupdays}"|wc -l)
  99. # Check if there is any backup to clear
  100. if [ "${backupclearcount}" -ne "0" ]; then
  101. fn_print_info_nl "${backupclearcount} backups older than ${backupdays} days can be cleared."
  102. fn_script_log "${backupclearcount} backups older than ${backupdays} days can be cleared"
  103. while true; do
  104. read -p -e"Clear older backups? [Y/N]" yn
  105. case $yn in
  106. [Yy]* ) clearoldbackups="yes"; break;;
  107. [Nn]* ) clearoldbackups="no"; fn_script_log "Not clearing backups"; break;;
  108. * ) echo "Please answer yes or no.";;
  109. esac
  110. done
  111. # If user wants to clear backups
  112. if [ "${clearoldbackups}" == "yes" ]; then
  113. find "${backupdir}"/ -mtime +"${backupdays}" -type f -exec rm -f {} \;
  114. fn_print_ok_nl "Cleared ${backupclearcount} backups."
  115. fn_script_log_pass "Cleared ${backupclearcount} backups"
  116. fi
  117. else
  118. fn_script_log "No backups older than ${backupdays} days were found"
  119. fi
  120. else
  121. fn_script_log "No backups to clear since backupdays variable is empty"
  122. fi
  123. # Restart the server if it was stopped for the backup
  124. if [ "${serverstopped}" == "yes" ]; then
  125. exitbypass=1
  126. command_start.sh
  127. fi
  128. else
  129. # Exit code reports an error
  130. fn_print_error_nl "Backup failed: ${backupname}.tar.gz"
  131. fn_script_log_error "Backup failed: ${backupname}.tar.gz"
  132. fi
  133. sleep 0.5
  134. core_exit.sh