functions 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. #
  3. # zammad backup script functions
  4. #
  5. function get_backup_date () {
  6. TIMESTAMP="$(date +'%Y%m%d%H%M%S')"
  7. if [ "${DEBUG}" == "yes" ]; then
  8. echo "timestamp is ${TIMESTAMP}"
  9. fi
  10. }
  11. function delete_old_backups () {
  12. test -d ${BACKUP_DIR} && find ${BACKUP_DIR}/*_zammad_*.gz -type f -mtime +${HOLD_DAYS} -exec rm {} \;
  13. }
  14. function get_db_credentials () {
  15. DB_ADAPTER="$(grep 'adapter:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*adapter:\(\| \)//g')"
  16. DB_NAME="$(grep 'database:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*database:\(\| \) //g')"
  17. DB_USER="$(grep 'username:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*username:\(\| \)//g')"
  18. DB_PASS="$(grep 'password:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*password:\(\| \)//g')"
  19. if [ "${DEBUG}" == "yes" ]; then
  20. echo "adapter=${DB_ADAPTER} dbname=${DB_NAME} dbuser=${DB_USER} dbpass=${DB_PASS}"
  21. fi
  22. }
  23. function backup_dir_create () {
  24. test -d ${BACKUP_DIR} || mkdir -p ${BACKUP_DIR}
  25. if [ "${DEBUG}" == "yes" ]; then
  26. echo "backup dir is ${BACKUP_DIR}"
  27. fi
  28. }
  29. function backup_files () {
  30. tar -czf ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz ${ZAMMAD_DIR}
  31. }
  32. function backup_db () {
  33. if [ "${DB_ADAPTER}" == "mysql2" ]; then
  34. mysqldump --opt --single-transaction -u${DB_USER} -p${DB_PASS} ${DB_NAME} | gzip > ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz
  35. elif [ "${DB_ADAPTER}" == "postgresql" ]; then
  36. su -c "pg_dump -c ${DB_NAME} | gzip > /tmp/${TIMESTAMP}_zammad_db.psql.gz" postgres
  37. mv /tmp/${TIMESTAMP}_zammad_db.psql.gz ${BACKUP_DIR}
  38. else
  39. echo "DB ADAPTER not found. if its sqlite backup is already saved in filebackup"
  40. fi
  41. }
  42. function check_database_config_exists () {
  43. if [ -f ${ZAMMAD_DIR}/${DATABASE_CONFIG} ]; then
  44. get_db_credentials
  45. else
  46. echo -e "${ZAMMAD_DIR}/${DATABASE_CONFIG} is missing. is zammad configured yet? \nAborting restore..."
  47. exit 1
  48. fi
  49. }
  50. function restore_warning () {
  51. if [ -n "${1}" ]; then
  52. CHOOSE_RESTORE="yes"
  53. else
  54. echo -e "The restore will delete your current config and database! \nBe sure to have a backup available! \n"
  55. echo -e "Enter 'yes' if you want to proceed!"
  56. read -p 'Restore?: ' CHOOSE_RESTORE
  57. fi
  58. if [ "${CHOOSE_RESTORE}" != "yes" ]; then
  59. echo "Restore aborted!"
  60. exit 1
  61. fi
  62. }
  63. function get_restore_dates () {
  64. RESTORE_FILE_DATES="$(find ${BACKUP_DIR} -type f -iname '*_zammad_files.tar.gz' | sed -e "s#${BACKUP_DIR}/##g" -e "s#_zammad_files.tar.gz##g" | sort)"
  65. if [ "${DB_ADAPTER}" == "postgresql" ]; then
  66. DB_FILE_EXT="psql"
  67. elif [ "${DB_ADAPTER}" == "mysql2" ]; then
  68. DB_FILE_EXT="mysql"
  69. fi
  70. RESTORE_DB_DATES="$(find ${BACKUP_DIR} -type f -iname "*_zammad_db.${DB_FILE_EXT}.gz" | sed -e "s#${BACKUP_DIR}/##g" -e "s#_zammad_db.${DB_FILE_EXT}.gz##g" | sort)"
  71. }
  72. function choose_restore_date () {
  73. if [ -n "${1}" ]; then
  74. RESTORE_FILE_DATE="${1}"
  75. else
  76. echo -e "Enter file date to restore: \n${RESTORE_FILE_DATES}"
  77. read -p 'File date: ' RESTORE_FILE_DATE
  78. fi
  79. if [ ! -f "${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz" ];then
  80. echo -e "File ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz does not exist! \nRestore aborted!"
  81. exit 1
  82. fi
  83. if [ -n "${1}" ]; then
  84. RESTORE_DB_DATE="${1}"
  85. else
  86. echo -e "Enter db date to restore: \n${RESTORE_DB_DATES}"
  87. read -p 'DB date: ' RESTORE_DB_DATE
  88. fi
  89. if [ ! -f "${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz" ];then
  90. echo -e "File ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz does not exist! \nRestore aborted!"
  91. exit 1
  92. fi
  93. }
  94. function detect_initcmd () {
  95. if [ -n "$(which systemctl 2> /dev/null)" ]; then
  96. INIT_CMD="systemctl"
  97. elif [ -n "$(which initctl 2> /dev/null)" ]; then
  98. INIT_CMD="initctl"
  99. else
  100. function sysvinit () {
  101. service $2 $1
  102. }
  103. INIT_CMD="sysvinit"
  104. fi
  105. if [ "${DOCKER}" == "yes" ]; then
  106. INIT_CMD="initctl"
  107. fi
  108. if [ "${DEBUG}" == "yes" ]; then
  109. echo "INIT CMD = ${INIT_CMD}"
  110. fi
  111. }
  112. function start_zammad () {
  113. echo "# Starting Zammad"
  114. ${INIT_CMD} start zammad
  115. }
  116. function stop_zammad () {
  117. echo "# Stopping Zammad"
  118. ${INIT_CMD} stop zammad
  119. }
  120. function restore_zammad () {
  121. echo "# Restoring Files"
  122. tar -C / --overwrite -xzf ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz
  123. if [ "${DB_ADAPTER}" == "postgresql" ]; then
  124. echo "# Restoring PostgrSQL DB"
  125. gunzip -ck < ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | su -c "psql -d ${DB_NAME}" postgres
  126. elif [ "${DB_ADAPTER}" == "mysql2" ]; then
  127. echo "# Restoring MySQL DB"
  128. gunzip -ck < ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | mysql -u${DB_USER} -p${DB_PASS} ${DB_NAME}
  129. fi
  130. }
  131. function restore_message () {
  132. echo "# Zammad restored!"
  133. }