trabajo-afectivo/contrib/backup/functions

147 lines
4.3 KiB
Text
Raw Normal View History

2016-12-10 13:48:33 +00:00
#!/bin/bash
#
# zammad backup script functions
#
function get_backup_date () {
TIMESTAMP="$(date +'%Y%m%d%H%M%S')"
2016-12-10 13:48:33 +00:00
if [ "${DEBUG}" == "yes" ]; then
echo "timestamp is ${TIMESTAMP}"
fi
2016-12-10 13:48:33 +00:00
}
function delete_old_backups () {
test -d ${BACKUP_DIR} && find ${BACKUP_DIR}/*_zammad_*.gz -type f -mtime +${HOLD_DAYS} -exec rm -r \;
2016-12-10 13:48:33 +00:00
}
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')"
2016-12-10 13:48:33 +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 () {
test -d ${BACKUP_DIR} || mkdir -p ${BACKUP_DIR}
2016-12-10 13:48:33 +00:00
if [ "${DEBUG}" == "yes" ]; then
echo "backup dir is ${BACKUP_DIR}"
fi
2016-12-10 13:48:33 +00:00
}
function backup_files () {
tar -czf ${BACKUP_DIR}/${TIMESTAMP}_zammad_files.tar.gz ${ZAMMAD_DIR}
2016-12-10 13:48:33 +00:00
}
function backup_db () {
if [ "${DB_ADAPTER}" == "mysql2" ]; then
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
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
2016-12-10 13:48:33 +00:00
}
function check_database_config_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
}
function restore_warning () {
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 -s CHOOSE_RESTORE
if [ "${CHOOSE_RESTORE}" != "yes" ]; then
echo "Restore aborted!"
exit 1
fi
}
function get_restore_dates () {
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")"
if [ "${ADAPTER}" == "postgresql" ]; then
DB_FILE_EXT="psql"
elif [ "${ADAPTER}" == "mysql2" ]; then
DB_FILE_EXT="mysql"
fi
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")"
}
function choose_restore_date () {
echo "Enter file date to restore: ${RESTORE_FILE_DATES}"
read -s RESTORE_FILE_DATE
if [ ! -f "${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz" ];then
echo "File ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz does not exist!\nRestore aborted!"
exit 1
fi
echo "Enter db date to restore: ${RESTORE_DB_DATES}"
read -s RESTORE_DB_DATE
if [ ! -f "${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz" ];then
echo "File ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_files.tar.gz does not exist!\nRestore aborted!"
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 () {
echo "# Starting Zammad"
${INIT_CMD} start zammad
}
function stop_zammad () {
echo "# Stopping Zammad"
${INIT_CMD} stop zammad
}
function delete_current_files () {
test -d ${ZAMMAD_DIR} && rm -rf ${ZAMMAD_DIR}
}
function restore_zammad () {
tar -C / -xzf ${BACKUP_DIR}/${RESTORE_FILE_DATE}_zammad_files.tar.gz
if [ "${ADAPTER}" == "postgresql" ]; then
gunzip ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | pg_restore -c ${DB_NAME}
elif [ "${ADAPTER}" == "mysql2" ]; then
gunzip ${BACKUP_DIR}/${RESTORE_DB_DATE}_zammad_db.${DB_FILE_EXT}.gz | mysql -u${DB_USER} -p${DB_PASS} ${DB_NAME}
fi
}
function restore_message () {
echo "Zammad restored!"
}