mailsystem/upgrade_roundcube.sh

627 lines
16 KiB
Bash
Executable File

#!/usr/bin/env bash
clear
echo -e "\n \033[32mStart script for upgrading Roundcube Webmailer..\033[m"
## -----------------------------------------------------------------
## ----------------------------------------------------------------
## ---
## --- For configurations see file conf/install_upgrade_roundcube.conf
## ---
## --- Dont make changes here!
## ---
## -----------------------------------------------------------------
## -----------------------------------------------------------------
# -------------
# - Settings
# -------------
_src_base_dir="$(realpath $(dirname $0))"
conf_file="${_src_base_dir}/conf/install_upgrade_roundcube.conf"
curdir=`pwd`
log_file="$(mktemp)"
tmp_dir="$(mktemp -d)"
backup_date="$(date +%Y-%m-%d-%H%M)"
crontab_backup_file="/root/crontab-root.${backup_date}"
# -------------
# - Functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f "$log_file"
rm -rf "$tmp_dir"
exit $1
}
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "\t[ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation wird abgebrochen\033[m"
echo ""
clean_up 1
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_ok() {
echo -e "\033[85G[ \033[32mok\033[m ]"
}
echo_failed(){
echo -e "\033[85G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[85G[ \033[30m\033[1mskipped\033[m ]"
}
echo_not_yet_implemented(){
echo -e "\033[85G[ \033[30m\033[1mnot yet implemented\033[m ]"
}
trap clean_up SIGHUP SIGINT SIGTERM
# - Support systemd ?
# -
if [[ "X$(which systemd)" = "X" ]]; then
SYSTEMD_EXISTS=false
else
SYSTEMD_EXISTS=true
fi
DEFAULT_DB_HOST="localhost"
DEFAULT_DB_NAME="roundcubemail"
DEFAULT_DB_USER="roundcube"
echo
echononl " Include Configuration file.."
if [[ ! -f $conf_file ]]; then
echo_failed
fatal "Missing configuration file '$conf_file'"
else
source $conf_file
echo_ok
fi
[[ -n "$WEBSITE_NAME" ]] || fatal "Website's name (WEBSITE_NAME) not present!"
DEFAULT_WEBSITE_BASEDIR="/var/www/${WEBSITE_NAME}"
[[ -n "$WEBSITE_BASEDIR" ]] || WEBSITE_BASEDIR=$DEFAULT_WEBSITE_BASEDIR
CUR_INSTALL_DIR="$(realpath "${WEBSITE_BASEDIR}/htdocs")"
if [[ ! -d "$CUR_INSTALL_DIR" ]] ; then
fatal "No current installation of roundcube found!"
fi
[[ -n "$PHP_DEBIAN_INSTALLATION" ]] || PHP_DEBIAN_INSTALLATION=false
[[ -n "$DB_TYPE" ]] || fatal "Database Type of Roundcube Database (DB_TYPE) not present!"
[[ -n "$DB_HOST" ]] || DB_HOST="$DEFAULT_DB_HOST"
[[ -n "$DB_NAME" ]] || DB_NAME="$DEFAULT_DB_NAME"
[[ -n "$DB_USER" ]] || DB_USER="$DEFAULT_DB_USER"
[[ -n "$MYSQL_DEBIAN_INSTALLATION" ]] || MYSQL_DEBIAN_INSTALLATION=false
if [ "$DB_TYPE" = "postgres" -o "$DB_TYPE" = "postgresql" -o "$DB_TYPE" = "pgsql" -o "$DB_TYPE" = "psql" ];then
DB_TYPE="pgsql"
fi
if [[ "$DB_TYPE" = "mysql" ]]; then
if $MYSQL_DEBIAN_INSTALLATION ; then
[[ -n "$MYSQL_CREDENTIALS" ]] || MYSQL_CREDENTIALS="$DEFAULT_DEBIAN_MYSQL_CREDENTIALS"
else
[[ -n "$MYSQL_CREDENTIALS" ]] || MYSQL_CREDENTIALS="$DEFAULT_MYSQL_CREDENTIALS"
fi
else
[[ "$DB_TYPE" = "pgsql" ]] || fatal "Unknown Database Type '$DB_TYPE' (DB_TYPE)"
fi
echo -e "\033[32m--\033[m"
echo ""
echo "Version of the Roundcube Webmailer to install"
echo ""
echo ""
ROUNDCUBE_VERSION=
while [ "X$ROUNDCUBE_VERSION" = "X" ]
do
echononl "Roundcube Version: "
read ROUNDCUBE_VERSION
if [ "X$ROUNDCUBE_VERSION" = "X" ]; then
echo -e "\n\t\033[33m\033[1mA version number is required!\033[m\n"
fi
done
echo ""
echo -e "\033[32m--\033[m"
echo ""
NEW_INSTALL_DIR="${WEBSITE_BASEDIR}/roundcubemail-${ROUNDCUBE_VERSION}"
if [[ "$NEW_INSTALL_DIR" = "$CUR_INSTALL_DIR" ]] ; then
warn "Version '${ROUNDCUBE_VERSION}' is already installed"
echononl "Type uppercase \"yes\" to proceed with upgrading: "
read _proceed
[[ "$_proceed" == "YES" ]] || fatal "Exiting by user request.."
fi
echo ""
echo ""
echo -e "\033[1;32mSettings for installation of \033[1;37mRoundcube Webmail\033[m"
echo ""
echo -e "\tRoundcube Version....................: $ROUNDCUBE_VERSION"
echo ""
echo -e "\tName of the Website..................: $WEBSITE_NAME"
echo ""
if [[ "$DB_TYPE" = "mysql" ]]; then
echo -e "\tDatabase type of Roundcube Database..: MySQL"
echo -e "\tMySQL from Debian Package System.....: $MYSQL_DEBIAN_INSTALLATION"
else
echo -e "\tDatabase type of Roundcube Database..: PostgreSQL"
fi
echo -e "\tHost of Roundcube Database...........: $DB_HOST"
echo -e "\tName of Roundcube Database...........: $DB_NAME"
echo -e "\tUser of Roundcube Database...........: $DB_USER"
echo -e "\tPassword of Roundcube Database.......: $DB_PASS"
if [[ "$DB_TYPE" = "mysql" ]]; then
echo -e "\tMySQL Credentials (root access)......: $MYSQL_CREDENTIALS"
fi
echo ""
echo -e "\tCrontab backup file..................: $crontab_backup_file"
echo ""
echo -n "Type upper case 'YES' to continue executing with this parameters: "
read OK
if [[ "$OK" = "YES" ]] ; then
echo ""
echo ""
echo -e " \033[1;32mGoing to upgrade Roundcube Webmailer \033[1;37m$network \033[m"
echo ""
else
fatal "Abort by user request - Answer as not 'YES'"
fi
if [[ "$DB_TYPE" = "mysql" ]]; then
if ! mysql $MYSQL_CREDENTIALS -N -s -e \
"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$DB_NAME'" 2>> $log_file \
| grep $DB_NAME >> $log_file 2>&1 ; then
fatal "MySQL Database '$DB_NAME' does not exit. (See Parameter 'DB_NAME')"
fi
elif [[ "$DB_TYPE" = "pgsql" ]]; then
count=$(su - postgres -c "psql -q -A -t -l" | grep -c -e "^$DB_NAME")
if [[ $count -eq 0 ]];then
fatal "PostgreSQL Database '$DB_NAME' does not exit. (See Parameter 'DB_NAME')"
fi
else
fatal "Cannot detect database type (value of DB_TYPE is neither 'mysql' nor 'pgsql')"
fi
echo -e "\n\n \033[37m\033[1mCreate some Backups..\033[m\n"
echononl " Backup existing Database '$DB_NAME'"
if [[ "$DB_TYPE" = "mysql" ]]; then
echo -n " (MySQL).."
mysqldump $MYSQL_CREDENTIALS --opt $DB_NAME > ${WEBSITE_BASEDIR}/${DB_NAME}.$backup_date.sql 2> $log_file
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
elif [[ "$DB_TYPE" = "pgsql" ]]; then
echo -n " (PostgreSQL).."
su - postgres -c "pg_dump -c $DB_NAME" > ${WEBSITE_BASEDIR}/${DB_NAME}.$backup_date.sql 2> $log_file
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
fi
echononl " Backup existing web-directory .."
if [[ -d "$CUR_INSTALL_DIR" ]]; then
mv "$CUR_INSTALL_DIR" "${CUR_INSTALL_DIR}.$backup_date" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
else
fatal "No current installation of roundcube found!"
fi
echo -e "\n\n \033[37m\033[1mDownloud/Unpack source archive..\033[m\n"
echononl " Download 'roundcubemail-${ROUNDCUBE_VERSION}'.."
if [[ ! -f "$_src_base_dir/roundcubemail-${ROUNDCUBE_VERSION}.tar.gz" ]]; then
wget -O ${_src_base_dir}/roundcubemail-${ROUNDCUBE_VERSION}.tar.gz https://github.com/roundcube/roundcubemail/releases/download/${ROUNDCUBE_VERSION}/roundcubemail-${ROUNDCUBE_VERSION}.tar.gz > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
else
echo_skipped
fi
echononl " Remove existing source directory"
if [[ -d "${_src_base_dir}/roundcubemail-${ROUNDCUBE_VERSION}" ]]; then
rm -rf "${_src_base_dir}/roundcubemail-${ROUNDCUBE_VERSION}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
else
echo_skipped
fi
echononl " Unpack roundcube source archive.."
gunzip < ${_src_base_dir}/roundcubemail-${ROUNDCUBE_VERSION}.tar.gz | tar -C ${_src_base_dir} -xf - > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
echo -e "\n\n \033[37m\033[1mSome pre-installation tasks..\033[m\n"
## - Disable crontab for user root
## -
echononl " Backup crontab"
echo "" >> ${logdir}/main.log
echo "crontab -u root -l > $crontab_backup_file" >> ${logdir}/main.log
crontab -u root -l >> $crontab_backup_file 2>> ${logdir}/main.log
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Disable crontab for user root"
echo "" >> ${logdir}/main.log
echo "crontab -r -u root" >> ${logdir}/main.log
crontab -r -u root >> ${logdir}/main.log 2>&1
if [[ "$?" = "0" ]]; then
echo_ok
else
echo_failed
fi
echononl " Stop Apache Webserver.."
if $SYSTEMD_EXISTS ; then
systemctl stop apache2 > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
else
/etc/init.d/apache2 stop > ${logdir}/main.log 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
# - Determin PHP of all installed versions
# -
if $PHP_DEBIAN_INSTALLATION ; then
echononl " Get get major version installed PHP"
php_major_version="$(php --version | head -1 | cut -d' ' -f2 | cut -d '-' -f1 | cut -d'.' -f1,2)"
if [[ -z "$php_major_version" ]]; then
echo_failed
error "Getting version numbers of installed PHP versions failed! No installed PHP versiond found!"
else
echo_ok
fi
else
echononl " Get major version of all installed PHP versions"
php_major_versions="$(find /usr/local/ -maxdepth 1 -mindepth 1 -type l -name "php-*" -print | cut -d "-" -f2 | sort)"
if [[ -z "$php_major_versions" ]]; then
echo_failed
error "Getting version numbers of installed PHP versions failed! No installed PHP versiond found!"
else
echo_ok
fi
fi
# - Stop all PHP FPM engines
# -
if [[ -n "$php_major_versions" ]]; then
for _ver in $php_major_versions ; do
echononl " Stop PHP FPM engine v${_ver}.."
if [[ -f "/etc/init.d/php-${_ver}-fpm" ]]; then
/etc/init.d/php-${_ver}-fpm stop > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
elif [[ -f "/etc/systemd/system/php-${_ver}-fpm.service" ]] ; then
systemctl stop php-${_ver}-fpm > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
else
echo_skipped
fi
done
else
if $PHP_DEBIAN_INSTALLATION ; then
echononl " Stop PHP FPM engine v${php_major_version}.."
if [[ -f "/etc/init.d/php$(echo $php_major_version | cut -d'.' -f1)-fpm" ]] ; then
/etc/init.d/php$(echo $php_major_version | cut -d'.' -f1)-fpm stop > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
else
echo_skipped
fi
else
echo_skipped
fi
fi
echo -e "\n\n \033[37m\033[1mUgrade Roundcube Webmail..\033[m\n"
echononl " Copy current web-directory into a the new one.."
cp -a "${CUR_INSTALL_DIR}.$backup_date" "${WEBSITE_BASEDIR}/roundcubemail-${ROUNDCUBE_VERSION}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
echononl " Set actual timestamp to the new web-directory"
touch -t "$(date +%Y%m%d%H%M.%S)" "${WEBSITE_BASEDIR}/roundcubemail-${ROUNDCUBE_VERSION}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
echononl " Change into new roundcube source directory"
cd "${_src_base_dir}/roundcubemail-${ROUNDCUBE_VERSION}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
echononl " Create log-directory for update log file"
if [[ ! -d "${_src_base_dir}/log" ]]; then
mkdir "${_src_base_dir}/log" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
else
echo_skipped
fi
echo ""
echo " Update the the roundcube web-directory to version '${ROUNDCUBE_VERSION}'"
echononl " See: ${_src_base_dir}/log/update_roundcube-${ROUNDCUBE_VERSION}.${backup_date}.log"
echo "y" | ${_src_base_dir}/roundcubemail-${ROUNDCUBE_VERSION}/bin/installto.sh "${WEBSITE_BASEDIR}/roundcubemail-${ROUNDCUBE_VERSION}" > ${_src_base_dir}/log/update_roundcube-${ROUNDCUBE_VERSION}.${backup_date}.log 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fi
echo ""
echononl " Change into new roundcube web directory"
cd "${WEBSITE_BASEDIR}/roundcubemail-${ROUNDCUBE_VERSION}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
echononl " Update dependencies by running 'php composer.phar update --no-dev'"
php composer.phar update --no-dev > ${_src_base_dir}/log/update_roundcube-${ROUNDCUBE_VERSION}-dependencies.${backup_date}.log 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat ${_src_base_dir}/log/update_roundcube-${ROUNDCUBE_VERSION}-dependencies.${backup_date}.log)"
fi
echononl " Index build-in addressbook"
${WEBSITE_BASEDIR}/roundcubemail-${ROUNDCUBE_VERSION}/bin/indexcontacts.sh > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
_failed=false
echononl " Symlink DocumentRoot to the new installation directory.."
if [[ -h "${WEBSITE_BASEDIR}/htdocs" ]]; then
rm "${WEBSITE_BASEDIR}/htdocs" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
_failed=true
fi
elif [[ -f "${WEBSITE_BASEDIR}/htdocs" ]]; then
mv "${WEBSITE_BASEDIR}/htdocs" "${WEBSITE_BASEDIR}/htdocs/.$backup_date" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
_failed=true
fi
fi
ln -s "roundcubemail-${ROUNDCUBE_VERSION}" "${WEBSITE_BASEDIR}/htdocs" >> $log_file 2>&1
if [[ $? -ne 0 ]]; then
_failed=true
fi
if $_failed ; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
echo -e "\n\n \033[37m\033[1mSome post-installation tasks..\033[m\n"
# - Start all PHP FPM engines
# -
if [[ -n "$php_major_versions" ]]; then
for _ver in $php_major_versions ; do
echononl " Start PHP FPM engine v${_ver}.."
if [[ -f "/etc/init.d/php-${_ver}-fpm" ]]; then
/etc/init.d/php-${_ver}-fpm start > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
elif [[ -f "/etc/systemd/system/php-${_ver}-fpm.service" ]] ; then
systemctl start php-${_ver}-fpm > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
else
echo_skipped
fi
done
else
if $PHP_DEBIAN_INSTALLATION ; then
echononl " Start PHP FPM engine v${php_major_version}.."
if [[ -f "/etc/init.d/php$(echo $php_major_version | cut -d'.' -f1)-fpm" ]] ; then
/etc/init.d/php$(echo $php_major_version | cut -d'.' -f1)-fpm start > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
else
echo_skipped
fi
else
echo_skipped
fi
fi
# - Start Apache Webserver
# -
echononl " Start Apache Webserver.."
if $SYSTEMD_EXISTS ; then
systemctl start apache2 > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
else
/etc/init.d/apache2 start> $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
echononl " Renstall previously saved crontab from '$crontab_backup_file'.."
crontab $crontab_backup_file > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
echononl " Delete previously saved crontab file '$crontab_backup_file'.."
rm "$crontab_backup_file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
echo ""
clean_up 0