functions 5.2 KB

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