command_backup.sh 3.3 KB

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