2018-02-01 06:09:32 +00:00
|
|
|
#!/usr/bin/env bash
|
2016-12-10 13:48:33 +00:00
|
|
|
#
|
|
|
|
# zammad backup script functions
|
|
|
|
#
|
|
|
|
|
2017-05-07 10:20:59 +00:00
|
|
|
function get_zammad_dir () {
|
|
|
|
ZAMMAD_DIR="$(echo ${BACKUP_SCRIPT_PATH} | sed -e 's#/contrib/backup.*##g')"
|
|
|
|
}
|
|
|
|
|
2016-12-10 13:48:33 +00:00
|
|
|
function get_backup_date () {
|
2017-02-12 11:52:10 +00:00
|
|
|
TIMESTAMP="$(date +'%Y%m%d%H%M%S')"
|
2016-12-10 13:48:33 +00:00
|
|
|
|
2017-02-12 11:52:10 +00:00
|
|
|
if [ "${DEBUG}" == "yes" ]; then
|
|
|
|
echo "timestamp is ${TIMESTAMP}"
|
|
|
|
fi
|
2016-12-10 13:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function delete_old_backups () {
|
2017-03-13 07:49:57 +00:00
|
|
|
test -d ${BACKUP_DIR} && find ${BACKUP_DIR}/*_zammad_*.gz -type f -mtime +${HOLD_DAYS} -exec rm {} \;
|
2016-12-10 13:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_db_credentials () {
|
2017-05-07 10:20:59 +00:00
|
|
|
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')"
|
2016-12-10 13:48:33 +00:00
|
|
|
|
2017-02-12 11:52:10 +00:00
|
|
|
if [ "${DEBUG}" == "yes" ]; then
|
|
|
|
echo "adapter=${DB_ADAPTER} dbname=${DB_NAME} dbuser=${DB_USER} dbpass=${DB_PASS}"
|
|
|
|
fi
|
2016-12-10 13:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function backup_dir_create () {
|
2017-02-12 11:52:10 +00:00
|
|
|
test -d ${BACKUP_DIR} || mkdir -p ${BACKUP_DIR}
|
2016-12-10 13:48:33 +00:00
|
|
|
|
2017-02-12 11:52:10 +00:00
|
|
|
if [ "${DEBUG}" == "yes" ]; then
|
|
|
|
echo "backup dir is ${BACKUP_DIR}"
|
|
|
|
fi
|
2016-12-10 13:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function backup_files () {
|
2017-05-07 10:20:59 +00:00
|
|
|
echo "creating file backup..."
|
2019-07-01 15:13:19 +00:00
|
|
|
tar -C / -czf ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz --exclude='tmp' ${ZAMMAD_DIR#/}
|
2017-11-22 15:33:50 +00:00
|
|
|
ln -sfn ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz ${BACKUP_DIR}/latest_zammad_files.tar.gz
|
2016-12-10 13:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function backup_db () {
|
2017-02-12 11:52:10 +00:00
|
|
|
if [ "${DB_ADAPTER}" == "mysql2" ]; then
|
2017-05-07 10:20:59 +00:00
|
|
|
echo "creating mysql backup..."
|
2017-02-12 11:52:10 +00:00
|
|
|
mysqldump --opt --single-transaction -u${DB_USER} -p${DB_PASS} ${DB_NAME} | gzip > ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz
|
2017-11-22 15:33:50 +00:00
|
|
|
ln -sfn ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.mysql.gz ${BACKUP_DIR}/latest_zammad_db.mysql.gz
|
2017-02-12 11:52:10 +00:00
|
|
|
elif [ "${DB_ADAPTER}" == "postgresql" ]; then
|
2017-05-07 10:20:59 +00:00
|
|
|
echo "creating postgresql backup..."
|
2017-02-14 17:40:19 +00:00
|
|
|
su -c "pg_dump -c ${DB_NAME} | gzip > /tmp/${TIMESTAMP}_zammad_db.psql.gz" postgres
|
|
|
|
mv /tmp/${TIMESTAMP}_zammad_db.psql.gz ${BACKUP_DIR}
|
2017-11-22 15:33:50 +00:00
|
|
|
ln -sfn ${BACKUP_DIR}/${TIMESTAMP}_zammad_db.psql.gz ${BACKUP_DIR}/latest_zammad_db.psql.gz
|
2017-02-12 11:52:10 +00:00
|
|
|
else
|
2017-05-07 10:20:59 +00:00
|
|
|
echo "DB ADAPTER not found. if its sqlite backup is already saved in the filebackup"
|
2017-02-12 11:52:10 +00:00
|
|
|
fi
|
2016-12-10 13:48:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 09:52:35 +00:00
|
|
|
function check_database_config_exists () {
|
2017-05-07 10:20:59 +00:00
|
|
|
if [ -f ${ZAMMAD_DIR}/config/database.yml ]; then
|
2017-02-12 11:52:10 +00:00
|
|
|
get_db_credentials
|
|
|
|
else
|
2017-05-07 10:20:59 +00:00
|
|
|
echo -e "${ZAMMAD_DIR}/config/database.yml is missing. is zammad configured yet? \nAborting..."
|
2017-02-12 11:52:10 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore_warning () {
|
2017-02-12 13:56:39 +00:00
|
|
|
if [ -n "${1}" ]; then
|
|
|
|
CHOOSE_RESTORE="yes"
|
|
|
|
else
|
|
|
|
echo -e "The restore will delete your current config and database! \nBe sure to have a backup available! \n"
|
|
|
|
echo -e "Enter 'yes' if you want to proceed!"
|
|
|
|
read -p 'Restore?: ' CHOOSE_RESTORE
|
|
|
|
fi
|
2017-02-12 11:52:10 +00:00
|
|
|
|
|
|
|
if [ "${CHOOSE_RESTORE}" != "yes" ]; then
|
|
|
|
echo "Restore aborted!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_restore_dates () {
|
2017-02-12 13:56:39 +00:00
|
|
|
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)"
|
2017-02-12 11:52:10 +00:00
|
|
|
|
2017-02-12 13:56:39 +00:00
|
|
|
if [ "${DB_ADAPTER}" == "postgresql" ]; then
|
2017-02-12 11:52:10 +00:00
|
|
|
DB_FILE_EXT="psql"
|
2017-02-12 13:56:39 +00:00
|
|
|
elif [ "${DB_ADAPTER}" == "mysql2" ]; then
|
2017-02-12 11:52:10 +00:00
|
|
|
DB_FILE_EXT="mysql"
|
|
|
|
fi
|
|
|
|
|
2017-02-12 13:56:39 +00:00
|
|
|
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)"
|
2017-02-12 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function choose_restore_date () {
|
2017-02-12 13:56:39 +00:00
|
|
|
if [ -n "${1}" ]; then
|
|
|
|
RESTORE_FILE_DATE="${1}"
|
|
|
|
else
|
|
|
|
echo -e "Enter file date to restore: \n${RESTORE_FILE_DATES}"
|
|
|
|
read -p 'File date: ' RESTORE_FILE_DATE
|
|
|
|
fi
|
|
|
|
|
2017-02-12 11:52:10 +00:00
|
|
|
if [ ! -f "${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz" ];then
|
2017-02-12 13:56:39 +00:00
|
|
|
echo -e "File ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz does not exist! \nRestore aborted!"
|
2017-02-12 11:52:10 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2017-02-12 13:56:39 +00:00
|
|
|
if [ -n "${1}" ]; then
|
|
|
|
RESTORE_DB_DATE="${1}"
|
|
|
|
else
|
|
|
|
echo -e "Enter db date to restore: \n${RESTORE_DB_DATES}"
|
|
|
|
read -p 'DB date: ' RESTORE_DB_DATE
|
|
|
|
fi
|
|
|
|
|
2017-02-12 11:52:10 +00:00
|
|
|
if [ ! -f "${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz" ];then
|
2017-02-12 13:56:39 +00:00
|
|
|
echo -e "File ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz does not exist! \nRestore aborted!"
|
2017-02-12 11:52:10 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function detect_initcmd () {
|
|
|
|
if [ -n "$(which systemctl 2> /dev/null)" ]; then
|
|
|
|
INIT_CMD="systemctl"
|
|
|
|
elif [ -n "$(which initctl 2> /dev/null)" ]; then
|
|
|
|
INIT_CMD="initctl"
|
|
|
|
else
|
|
|
|
function sysvinit () {
|
|
|
|
service $2 $1
|
|
|
|
}
|
|
|
|
INIT_CMD="sysvinit"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${DOCKER}" == "yes" ]; then
|
|
|
|
INIT_CMD="initctl"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${DEBUG}" == "yes" ]; then
|
|
|
|
echo "INIT CMD = ${INIT_CMD}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_zammad () {
|
2017-02-12 14:56:05 +00:00
|
|
|
echo "# Starting Zammad"
|
2017-02-12 11:52:10 +00:00
|
|
|
${INIT_CMD} start zammad
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop_zammad () {
|
|
|
|
echo "# Stopping Zammad"
|
|
|
|
${INIT_CMD} stop zammad
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore_zammad () {
|
2020-08-20 13:33:20 +00:00
|
|
|
if ! command -v zammad > /dev/null; then
|
|
|
|
# See #3160 for the reasons of this :>
|
|
|
|
restore_files
|
|
|
|
fi
|
2017-02-12 11:52:10 +00:00
|
|
|
|
2017-02-12 13:56:39 +00:00
|
|
|
if [ "${DB_ADAPTER}" == "postgresql" ]; then
|
2020-08-20 13:47:31 +00:00
|
|
|
echo "# Checking requirements"
|
|
|
|
|
|
|
|
if ! id "zammad" &> /dev/null; then
|
|
|
|
echo "# ERROR: User 'zammad' is not present, but should be by default! #"
|
|
|
|
echo "# Restoration was NOT successfull, exitting ..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-04-24 07:47:25 +00:00
|
|
|
echo "# ... Dropping current database ${DB_NAME}"
|
|
|
|
|
|
|
|
# This step is only needed for some pgsql dumps, as they don't provide a drop table statement which will cause
|
|
|
|
# relation errors during restoration (as on package installation there's always an initialised database)
|
|
|
|
if command -v zammad > /dev/null; then
|
|
|
|
zammad config:set DISABLE_DATABASE_ENVIRONMENT_CHECK=1
|
|
|
|
zammad run rake db:drop
|
|
|
|
zammad config:set DISABLE_DATABASE_ENVIRONMENT_CHECK=0
|
|
|
|
else
|
|
|
|
${ZAMMAD_DIR}/bin/rake db:drop
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "# ... Creating database ${DB_NAME} for owner ${DB_USER}"
|
|
|
|
if command -v zammad > /dev/null; then
|
|
|
|
# We'll skip this part for docker installations
|
|
|
|
su -c "psql -c \"CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};\"" postgres
|
|
|
|
else
|
|
|
|
${ZAMMAD_DIR}/bin/rake db:create
|
|
|
|
fi
|
|
|
|
|
2017-02-12 13:56:39 +00:00
|
|
|
echo "# Restoring PostgrSQL DB"
|
2020-08-20 13:54:49 +00:00
|
|
|
zcat ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | su -c "psql -d ${DB_NAME}" zammad
|
2017-02-12 13:56:39 +00:00
|
|
|
elif [ "${DB_ADAPTER}" == "mysql2" ]; then
|
|
|
|
echo "# Restoring MySQL DB"
|
2017-09-17 11:34:23 +00:00
|
|
|
zcat ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | mysql -u${DB_USER} -p${DB_PASS} ${DB_NAME}
|
2017-02-12 11:52:10 +00:00
|
|
|
fi
|
2020-08-20 13:33:20 +00:00
|
|
|
|
|
|
|
if command -v zammad > /dev/null; then
|
|
|
|
# See #3160 for the reasons of this :>
|
|
|
|
restore_files
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore_files () {
|
|
|
|
echo "# Restoring Files"
|
|
|
|
tar -C / --overwrite -xzf ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz
|
|
|
|
echo "# Ensuring correct file rights ..."
|
|
|
|
chown -R zammad:zammad ${ZAMMAD_DIR}
|
2017-02-12 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 10:20:59 +00:00
|
|
|
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"
|
2017-02-12 09:52:35 +00:00
|
|
|
}
|