From 115ea3ad6f700a619387b4e3b9107b8a0fda896b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bauer?= Date: Sat, 10 Dec 2016 14:48:33 +0100 Subject: [PATCH] added backup script --- contrib/backup/config | 10 +++++++ contrib/backup/functions | 50 +++++++++++++++++++++++++++++++++ contrib/backup/zammad_backup.sh | 30 ++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 contrib/backup/config create mode 100644 contrib/backup/functions create mode 100755 contrib/backup/zammad_backup.sh diff --git a/contrib/backup/config b/contrib/backup/config new file mode 100644 index 000000000..11e5f5c62 --- /dev/null +++ b/contrib/backup/config @@ -0,0 +1,10 @@ +#!/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="yes" diff --git a/contrib/backup/functions b/contrib/backup/functions new file mode 100644 index 000000000..74330a544 --- /dev/null +++ b/contrib/backup/functions @@ -0,0 +1,50 @@ +#!/bin/bash +# +# zammad backup script functions +# + +function get_backup_date () { + TIMESTAMP="$(date +'%Y%m%d%H%M%S')" + + if [ "${DEBUG}" == "yes" ]; then + echo "timestamp is ${TIMESTAMP}" + fi +} + +function delete_old_backups () { + find ${BACKUP_DIR}/*_zammad_*.gz -maxdepth 2 -mindepth 2 -type f -mtime +${HOLD_DAYS} -exec rm -r \; +} + +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')" + + if [ "${DEBUG}" == "yes" ]; then + echo "adapter=${DB_ADAPTER} dbname=${DB_NAME} dbuser=${DB_USER} dbpass=${DB_PASS}" + fi +} + +function backup_dir_create () { + test -d ${BACKUP_DIR} || mkdir -p ${BACKUP_DIR} + + if [ "${DEBUG}" == "yes" ]; then + echo "backup dir is ${BACKUP_DIR}" + fi +} + +function backup_files () { + tar -czf ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz ${ZAMMAD_DIR} +} + +function backup_db () { + if [ "${DB_ADAPTER}" == "mysql2" ]; then + mysqldump --opt --single-transaction ${DB_NAME} | gzip > ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz + elif [ "${DB_ADAPTER}" == "postgresql" ]; then + su -c "pg_dump ${DB_NAME} | gzip > ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.psql.gz" postgres + else + echo "ADAPTER not found. if its sqlite backup is already saved in filebackup" + fi +} + diff --git a/contrib/backup/zammad_backup.sh b/contrib/backup/zammad_backup.sh new file mode 100755 index 000000000..bebc21b3d --- /dev/null +++ b/contrib/backup/zammad_backup.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# zammad backup script +# + +PATH=/sbin:/bin:/usr/sbin:/usr/bin: + +# import config +. config + +# import functions +. functions + +delete_old_backups + +# check if database.yml exists +if [ -f ${ZAMMAD_DIR}/${DATABASE_CONFIG} ]; then + get_db_credentials +else + echo "${ZAMMAD_DIR}//${DATABASE_CONFIG} is missing. is zammad configured yet?" + exit 1 +fi + +get_backup_date + +backup_dir_create + +backup_files + +backup_db