From acdbd4b83a1511f733e87e94251553d8c4ef49d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bauer?= Date: Sun, 7 May 2017 12:20:59 +0200 Subject: [PATCH] some enhancements to backup & restore --- contrib/backup/config | 10 --------- contrib/backup/config.dist | 8 +++++++ contrib/backup/functions | 37 ++++++++++++++++++++++++-------- contrib/backup/zammad_backup.sh | 19 +++++++++++++--- contrib/backup/zammad_restore.sh | 19 ++++++++++++---- 5 files changed, 67 insertions(+), 26 deletions(-) delete mode 100644 contrib/backup/config create mode 100644 contrib/backup/config.dist diff --git a/contrib/backup/config b/contrib/backup/config deleted file mode 100644 index 634ddfa56..000000000 --- a/contrib/backup/config +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -# -# zammad backup script config -# - -ZAMMAD_DIR="/opt/zammad" -DATABASE_CONFIG="config/database.yml" -BACKUP_DIR="/var/tmp/zammad_backup" -HOLD_DAYS="10" -DEBUG="no" diff --git a/contrib/backup/config.dist b/contrib/backup/config.dist new file mode 100644 index 000000000..2047a4475 --- /dev/null +++ b/contrib/backup/config.dist @@ -0,0 +1,8 @@ +#!/bin/bash +# +# zammad backup script config +# + +BACKUP_DIR='/var/tmp/zammad_backup' +HOLD_DAYS='10' +DEBUG='no' diff --git a/contrib/backup/functions b/contrib/backup/functions index 61a6f6c52..28a6eec16 100644 --- a/contrib/backup/functions +++ b/contrib/backup/functions @@ -3,6 +3,10 @@ # zammad backup script functions # +function get_zammad_dir () { + ZAMMAD_DIR="$(echo ${BACKUP_SCRIPT_PATH} | sed -e 's#/contrib/backup.*##g')" +} + function get_backup_date () { TIMESTAMP="$(date +'%Y%m%d%H%M%S')" @@ -16,10 +20,10 @@ function delete_old_backups () { } function get_db_credentials () { - DB_ADAPTER="$(grep 'adapter:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*adapter:\(\| \)//g')" - DB_NAME="$(grep 'database:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*database:\(\| \) //g')" - DB_USER="$(grep 'username:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*username:\(\| \)//g')" - DB_PASS="$(grep 'password:' < ${ZAMMAD_DIR}/${DATABASE_CONFIG} | sed -e 's/.*password:\(\| \)//g')" + DB_ADAPTER="$(grep 'adapter:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*adapter:\(\| \)//g')" + DB_NAME="$(grep 'database:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*database:\(\| \) //g')" + DB_USER="$(grep 'username:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*username:\(\| \)//g')" + DB_PASS="$(grep 'password:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*password:\(\| \)//g')" if [ "${DEBUG}" == "yes" ]; then echo "adapter=${DB_ADAPTER} dbname=${DB_NAME} dbuser=${DB_USER} dbpass=${DB_PASS}" @@ -35,25 +39,28 @@ function backup_dir_create () { } function backup_files () { + echo "creating file backup..." tar -czf ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz ${ZAMMAD_DIR} } function backup_db () { if [ "${DB_ADAPTER}" == "mysql2" ]; then + echo "creating mysql backup..." mysqldump --opt --single-transaction -u${DB_USER} -p${DB_PASS} ${DB_NAME} | gzip > ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz elif [ "${DB_ADAPTER}" == "postgresql" ]; then + echo "creating postgresql backup..." su -c "pg_dump -c ${DB_NAME} | gzip > /tmp/${TIMESTAMP}_zammad_db.psql.gz" postgres mv /tmp/${TIMESTAMP}_zammad_db.psql.gz ${BACKUP_DIR} else - echo "DB ADAPTER not found. if its sqlite backup is already saved in filebackup" + echo "DB ADAPTER not found. if its sqlite backup is already saved in the filebackup" fi } function check_database_config_exists () { - if [ -f ${ZAMMAD_DIR}/${DATABASE_CONFIG} ]; then + if [ -f ${ZAMMAD_DIR}/config/database.yml ]; then get_db_credentials else - echo -e "${ZAMMAD_DIR}/${DATABASE_CONFIG} is missing. is zammad configured yet? \nAborting restore..." + echo -e "${ZAMMAD_DIR}/config/database.yml is missing. is zammad configured yet? \nAborting..." exit 1 fi } @@ -155,6 +162,18 @@ function restore_zammad () { fi } -function restore_message () { - echo "# Zammad restored!" +function start_backup_message () { + echo -e "\n# Zammad backup started - $(date)!\n" +} + +function start_restore_message () { + echo -e "\n# Zammad restored started - $(date)!\n" +} + +function finished_backup_message () { + echo -e "\n# Zammad backuped successfully - $(date)!\n" +} + +function finished_restore_message () { + echo -e "\n# Zammad restored successfully - $(date)!\n" } diff --git a/contrib/backup/zammad_backup.sh b/contrib/backup/zammad_backup.sh index ef706d0f5..37a2481a9 100755 --- a/contrib/backup/zammad_backup.sh +++ b/contrib/backup/zammad_backup.sh @@ -4,14 +4,25 @@ # PATH=/sbin:/bin:/usr/sbin:/usr/bin: +BACKUP_SCRIPT_PATH="$(dirname $(realpath $0))" -# import config -. /opt/zammad/contrib/backup/config +if [ -f "${BACKUP_SCRIPT_PATH}/config" ]; then + # import config + . ${BACKUP_SCRIPT_PATH}/config +else + echo -e "\n The 'config' file is missing!" + echo -e " Please copy ${BACKUP_SCRIPT_PATH}/config.dist to ${BACKUP_SCRIPT_PATH}/config before running $0!\n" + exit 1 +fi # import functions -. /opt/zammad/contrib/backup/functions +. ${BACKUP_SCRIPT_PATH}/functions # exec backup +start_backup_message + +get_zammad_dir + check_database_config_exists delete_old_backups @@ -23,3 +34,5 @@ backup_dir_create backup_files backup_db + +finished_backup_message diff --git a/contrib/backup/zammad_restore.sh b/contrib/backup/zammad_restore.sh index ac04ed7fb..25c8c754a 100755 --- a/contrib/backup/zammad_restore.sh +++ b/contrib/backup/zammad_restore.sh @@ -4,14 +4,25 @@ # PATH=/sbin:/bin:/usr/sbin:/usr/bin: +BACKUP_SCRIPT_PATH="$(dirname $(realpath $0))" -# import config -. /opt/zammad/contrib/backup/config +if [ -f "${BACKUP_SCRIPT_PATH}/config" ]; then + # import config + . ${BACKUP_SCRIPT_PATH}/config +else + echo -e "\n The 'config' file is missing!" + echo -e " Please copy ${BACKUP_SCRIPT_PATH}/config.dist to ${BACKUP_SCRIPT_PATH}/config before running $0!\n" + exit 1 +fi # import functions -. /opt/zammad/contrib/backup/functions +. ${BACKUP_SCRIPT_PATH}/functions # exec restore +start_restore_message + +get_zammad_dir + restore_warning "${1}" check_database_config_exists @@ -28,4 +39,4 @@ restore_zammad start_zammad -restore_message +finished_restore_message