#!/bin/sh # script for updating OTRS/Znuny on FreeBSD within the same major release, # e.g. 6.0.30 -> 6.0.38, and for updating Znuny across major releases, # e.g. 6.0.38 -> 6.1.2 DEST_VERSION="$1" OTRS_USER="${2:-otrs}" OTRS_DIR="$3" CURR_VERSION="$4" is_version_znuny () { local version local major local minor local build version="$1" major="${version%%.*}" if [ "$major" -gt 6 ]; then return 0 fi if [ "$major" -lt 6 ]; then return 1 fi minor="${version#*.}" build="${minor#*.}" minor="${minor%%.*}" if [ "$minor" -gt 0 ]; then return 0 fi build="${build%%-*}" if [ "$build" -gt 30 ]; then return 0 fi return 1 } run_upgrade_script () { local dest_version local dest_major local dest_minor local curr_version local curr_major local curr_minor dest_version="$1" dest_major="${dest_version%%.*}" dest_minor="${dest_version#*.}" dest_minor="${dest_minor%%.*}" curr_version="$2" curr_major="${curr_version%%.*}" curr_minor="${curr_version#*.}" curr_minor="${curr_minor%%.*}" if [ "$dest_major" -eq "$curr_major" -a "$dest_minor" -eq "$curr_minor" ]; then # no need to run upgrade script return fi if [ "$dest_major" -eq "$curr_major" -a "$dest_minor" -lt "$curr_minor" ]; then echo "*** Warning: Downgrading $curr_version -> $dest_version is not supported officially." >&2 return fi if [ "$dest_major" -lt "$curr_major" ]; then echo "*** Warning: Downgrading $curr_version -> $dest_version is not supported officially." >&2 return fi # here: dest_major > curr_major or # (dest_major == curr_major && dest_minor > curr_minor) su -m "$OTRS_USER" -c "'$OTRS_DIR'/scripts/MigrateToZnuny${dest_major}_${dest_minor}.pl" || exit 1 } die () { echo "$1" >&2 exit 1 } if [ -z "$DEST_VERSION" -o "$DEST_VERSION" = "-h" -o "$DEST_VERSION" = "--help" ]; then die "Usage: upgrade_otrs.sh [ [ []]]" fi if [ -z "$OTRS_DIR" ]; then OTRS_DIR=`su -m "$OTRS_USER" -c "echo ~$OTRS_USER"` || exit 1 fi # try to get current version from RELEASE file inside the installation directory if [ -z "$CURR_VERSION" ]; then while read L; do VERSION="${L#VERSION = }" if [ "$VERSION" != "$L" ]; then CURR_VERSION="$VERSION" fi done < "$OTRS_DIR"/RELEASE || exit 1 fi if [ -z "$CURR_VERSION" ]; then die "Unable to determine the currently installed OTRS version in $OTRS_DIR" fi if [ "$CURR_VERSION" = "$DEST_VERSION" ]; then die "OTRS version $DEST_VERSION is already installed" fi is_version_znuny "$CURR_VERSION" && CURR_PACKAGE="znuny-$CURR_VERSION" || \ CURR_PACKAGE="otrs-$CURR_VERSION" is_version_znuny "$DEST_VERSION" && DEST_PACKAGE="znuny-$DEST_VERSION" || \ DEST_PACKAGE="otrs-$DEST_VERSION" CURR_TARBALL="${CURR_PACKAGE}.tar.bz2" DEST_TARBALL="${DEST_PACKAGE}.tar.bz2" # fetch tarballs of current and new version if [ ! -f "$CURR_TARBALL" ]; then # fetch "http://ftp.otrs.org/pub/otrs/$CURR_TARBALL" || exit 1 fetch "https://download.znuny.org/releases/$CURR_TARBALL" || exit 1 fi if [ ! -f "$DEST_TARBALL" ]; then # fetch "http://ftp.otrs.org/pub/otrs/$DEST_TARBALL" || exit 1 fetch "https://download.znuny.org/releases/$DEST_TARBALL" || exit 1 fi CURR_TMP=`mktemp -d` || exit 1 DEST_TMP=`mktemp -d` || exit 1 echo "Extracting $CURR_PACKAGE to $CURR_TMP" tar -C "$CURR_TMP" -xjSf "$CURR_TARBALL" || exit 1 cd "$CURR_TMP/"* || exit 1 CURR_DIR=`pwd` || exit 1 cd - || exit 1 rm "$CURR_TARBALL" || exit 1 echo "Extracting $DEST_PACKAGE to $DEST_TMP" tar -C "$DEST_TMP" -xjSf "$DEST_TARBALL" || exit 1 cd "$DEST_TMP/"* || exit 1 DEST_DIR=`pwd` || exit 1 cd - || exit 1 # keep tarball of new version for next upgrade service apache24 onestatus && { service apache24 onestop || exit 1; } crontab -l -u "$OTRS_USER" && { crontab -r -f -u "$OTRS_USER" || exit 1; } su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Daemon.pl stop" || exit 1 su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Console.pl Maint::Cache::Delete" || exit 1 su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Console.pl Maint::Loader::CacheCleanup" || exit 1 su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Console.pl Maint::WebUploadCache::Cleanup" || exit 1 # once again to make sure daemon is not running anymore su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Daemon.pl stop" || exit 1 pkill -f "$OTRS_DIR" cd "$OTRS_DIR" || exit 1 echo "Removing files from $OTRS_DIR that are vanilla $CURR_VERSION ones" find . -type f | while read L; do [ -f "$CURR_DIR"/"$L" ] && cmp -s "$L" "$CURR_DIR"/"$L" && { rm "$L" || exit 1; } done echo "Installing new files from $DEST_VERSION" cp -ai "$DEST_DIR"/ "$OTRS_DIR"/ || exit 1 "$OTRS_DIR"/bin/otrs.SetPermissions.pl --admin-group=`id -gn 0` || exit 1 mkfifo "$DEST_TMP"/f || exit 1 grep required "$DEST_TMP"/f & su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.CheckModules.pl --all" >"$DEST_TMP"/f || exit 1 rm -r "$DEST_TMP" "$CURR_TMP" || exit 1 run_upgrade_script "$DEST_VERSION" "$CURR_VERSION" su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Console.pl Maint::Config::Rebuild --cleanup" || exit 1 su -m "$OTRS_USER" -c "'$OTRS_DIR'/bin/otrs.Console.pl Maint::Config::Sync" || exit 1 "$OTRS_DIR"/bin/Cron.sh start "$OTRS_USER" || exit 1 service apache24 onestart || exit 1 echo "Done."