command_backup.sh 3.3 KB

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