command_backup.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. # Check if a backup is pending or has been aborted using .backup.lock
  12. if [ -f "${tmpdir}/.backup.lock" ]; then
  13. fn_print_warning_nl "A backup is currently running or has been aborted."
  14. fn_script_log_warn "A backup is currently running or has been aborted."
  15. while true; do
  16. read -e -i "y" -p "Continue anyway? [Y/N]" yn
  17. case $yn in
  18. [Yy]* ) fn_script_log "User continues anyway"; break;;
  19. [Nn]* ) echo Exiting; fn_script_log "User aborted"; return;;
  20. * ) echo "Please answer yes or no.";;
  21. esac
  22. done
  23. fi
  24. fn_print_dots ""
  25. sleep 0.5
  26. # Prepare backup file name with servicename current date
  27. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  28. # Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue
  29. info_distro.sh
  30. fn_print_info_nl "A total of ${rootdirduexbackup} will be compressed into the following backup:"
  31. echo "${backupdir}/${backupname}.tar.gz"
  32. while true; do
  33. read -e -i "y" -p "Continue? [Y/N]" yn
  34. case $yn in
  35. [Yy]* ) break;;
  36. [Nn]* ) echo "Exiting"; fn_script_log "User aborted"; return;;
  37. * ) echo "Please answer yes or no.";;
  38. esac
  39. done
  40. # Check if server is started
  41. check_status.sh
  42. if [ "${status}" != "0" ]; then
  43. echo ""
  44. fn_print_warning_nl "${servicename} is currently running."
  45. sleep 1
  46. while true; do
  47. read -p "Stop ${servicename} while running the backup? [Y/N]" yn
  48. case $yn in
  49. [Yy]* ) command_stop.sh; serverstopped="yes"; break;;
  50. [Nn]* ) serverstopped="no"; break;;
  51. * ) echo "Please answer yes or no.";;
  52. esac
  53. done
  54. fi
  55. fn_print_dots "Backup in progress, please wait..."
  56. fn_script_log_info "Started backup"
  57. sleep 1
  58. if [ ! -d "${backupdir}" ]; then
  59. mkdir "${backupdir}"
  60. fi
  61. # Create lockfile
  62. touch "${tmpdir}/.backup.lock"
  63. fn_script_log "Lockfile created"
  64. # Compressing files
  65. fn_script_log "Compressing ${rootdirduexbackup}".
  66. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "backups" ./*
  67. # Remove lockfile
  68. rm "${tmpdir}/.backup.lock"
  69. fn_script_log "Lockfile removed"
  70. # Check tar exit code and act accordingly
  71. if [ $? == 0 ]; then
  72. # Exit code doesn't report any error
  73. fn_print_ok_nl "Backup created: ${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  74. fn_script_log_pass "Backup created: ${backupdir}/${backupname}.tar.gz is $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}') size"
  75. # Clear old backups if backupdays variable exists
  76. if [ -n "${backupdays}" ]; then
  77. # Count how many backups can be cleared
  78. backupclearcount=$(find "${backupdir}"/ -type f -mtime +"${backupdays}"|wc -l)
  79. # Check if there is any backup to clear
  80. if [ "${backupclearcount}" -ne "0" ]; then
  81. fn_print_info_nl "${backupclearcount} backups older than ${backupdays} days can be cleared."
  82. fn_script_log "${backupclearcount} backups older than ${backupdays} days can be cleared."
  83. while true; do
  84. read -p "Clear older backups? [Y/N]" yn
  85. case $yn in
  86. [Yy]* ) clearoldbackups="yes"; break;;
  87. [Nn]* ) clearoldbackups="no"; fn_script_log "User didn't clear backups"; break;;
  88. * ) echo "Please answer yes or no.";;
  89. esac
  90. done
  91. # If user wants to clear backups
  92. if [ "${clearoldbackups}" == "yes" ]; then
  93. find "${backupdir}"/ -mtime +"${backupdays}" -type f -exec rm -f {} \;
  94. fn_print_ok_nl "Cleared ${backupclearcount} backups."
  95. fi
  96. fi
  97. fi
  98. # Restart the server if it was stopped for the backup
  99. if [ "${serverstopped}" == "yes" ]; then
  100. command_start.sh
  101. fi
  102. else
  103. # Exit code reports an error
  104. fn_print_error_nl "Backup failed: ${backupname}.tar.gz"
  105. fn_script_log_error "Backup failed: ${backupname}.tar.gz"
  106. fi
  107. sleep 0.5
  108. core_exit.sh