functions 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env 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. ln -sfn ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz ${BACKUP_DIR}/latest_zammad_files.tar.gz
  36. }
  37. function backup_db () {
  38. if [ "${DB_ADAPTER}" == "mysql2" ]; then
  39. echo "creating mysql backup..."
  40. mysqldump --opt --single-transaction -u${DB_USER} -p${DB_PASS} ${DB_NAME} | gzip > ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz
  41. ln -sfn ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz ${BACKUP_DIR}/latest_zammad_db.mysql.gz
  42. elif [ "${DB_ADAPTER}" == "postgresql" ]; then
  43. echo "creating postgresql backup..."
  44. su -c "pg_dump -c ${DB_NAME} | gzip > /tmp/${TIMESTAMP}_zammad_db.psql.gz" postgres
  45. mv /tmp/${TIMESTAMP}_zammad_db.psql.gz ${BACKUP_DIR}
  46. ln -sfn ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.psql.gz ${BACKUP_DIR}/latest_zammad_db.psql.gz
  47. else
  48. echo "DB ADAPTER not found. if its sqlite backup is already saved in the filebackup"
  49. fi
  50. }
  51. function check_database_config_exists () {
  52. if [ -f ${ZAMMAD_DIR}/config/database.yml ]; then
  53. get_db_credentials
  54. else
  55. echo -e "${ZAMMAD_DIR}/config/database.yml is missing. is zammad configured yet? \nAborting..."
  56. exit 1
  57. fi
  58. }
  59. function restore_warning () {
  60. if [ -n "${1}" ]; then
  61. CHOOSE_RESTORE="yes"
  62. else
  63. echo -e "The restore will delete your current config and database! \nBe sure to have a backup available! \n"
  64. echo -e "Enter 'yes' if you want to proceed!"
  65. read -p 'Restore?: ' CHOOSE_RESTORE
  66. fi
  67. if [ "${CHOOSE_RESTORE}" != "yes" ]; then
  68. echo "Restore aborted!"
  69. exit 1
  70. fi
  71. }
  72. function get_restore_dates () {
  73. 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)"
  74. if [ "${DB_ADAPTER}" == "postgresql" ]; then
  75. DB_FILE_EXT="psql"
  76. elif [ "${DB_ADAPTER}" == "mysql2" ]; then
  77. DB_FILE_EXT="mysql"
  78. fi
  79. 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)"
  80. }
  81. function choose_restore_date () {
  82. if [ -n "${1}" ]; then
  83. RESTORE_FILE_DATE="${1}"
  84. else
  85. echo -e "Enter file date to restore: \n${RESTORE_FILE_DATES}"
  86. read -p 'File date: ' RESTORE_FILE_DATE
  87. fi
  88. if [ ! -f "${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz" ];then
  89. echo -e "File ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz does not exist! \nRestore aborted!"
  90. exit 1
  91. fi
  92. if [ -n "${1}" ]; then
  93. RESTORE_DB_DATE="${1}"
  94. else
  95. echo -e "Enter db date to restore: \n${RESTORE_DB_DATES}"
  96. read -p 'DB date: ' RESTORE_DB_DATE
  97. fi
  98. if [ ! -f "${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz" ];then
  99. echo -e "File ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz does not exist! \nRestore aborted!"
  100. exit 1
  101. fi
  102. }
  103. function detect_initcmd () {
  104. if [ -n "$(which systemctl 2> /dev/null)" ]; then
  105. INIT_CMD="systemctl"
  106. elif [ -n "$(which initctl 2> /dev/null)" ]; then
  107. INIT_CMD="initctl"
  108. else
  109. function sysvinit () {
  110. service $2 $1
  111. }
  112. INIT_CMD="sysvinit"
  113. fi
  114. if [ "${DOCKER}" == "yes" ]; then
  115. INIT_CMD="initctl"
  116. fi
  117. if [ "${DEBUG}" == "yes" ]; then
  118. echo "INIT CMD = ${INIT_CMD}"
  119. fi
  120. }
  121. function start_zammad () {
  122. echo "# Starting Zammad"
  123. ${INIT_CMD} start zammad
  124. }
  125. function stop_zammad () {
  126. echo "# Stopping Zammad"
  127. ${INIT_CMD} stop zammad
  128. }
  129. function restore_zammad () {
  130. echo "# Restoring Files"
  131. tar -C / --overwrite -xzf ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz
  132. if [ "${DB_ADAPTER}" == "postgresql" ]; then
  133. echo "# Restoring PostgrSQL DB"
  134. zcat ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | su -c "psql -d ${DB_NAME}" postgres
  135. elif [ "${DB_ADAPTER}" == "mysql2" ]; then
  136. echo "# Restoring MySQL DB"
  137. zcat ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | mysql -u${DB_USER} -p${DB_PASS} ${DB_NAME}
  138. fi
  139. }
  140. function start_backup_message () {
  141. echo -e "\n# Zammad backup started - $(date)!\n"
  142. }
  143. function start_restore_message () {
  144. echo -e "\n# Zammad restored started - $(date)!\n"
  145. }
  146. function finished_backup_message () {
  147. echo -e "\n# Zammad backuped successfully - $(date)!\n"
  148. }
  149. function finished_restore_message () {
  150. echo -e "\n# Zammad restored successfully - $(date)!\n"
  151. }