#!/bin/bash # There are three ways to install: # - tarball # - git # - packages # So we need to figure out what type, where it was installed, and what to uninstall. # Don't run this as a koha user! if [[ "$USER" =~ "koha" ]]; then echo "Do not run this script as a koha user." exit 1 fi # MUST HAVE $KOHA_CONF AND $PERL5LIB DEFINED! if [ "X$KOHA_CONF" == "X" ]; then echo "\$KOHA_CONF is not defined. Unable to uninstall." exit 1 fi # MUST HAVE $PERL5LIB DEFINED! if [ "X$PERL5LIB" == "X" ]; then echo "\$PERL5LIB is not defined. Unable to uninstall." exit 1 fi # So they shouldn't have to keep typing the sudo password. echo "Please be prepared to type in your password." CHKSUDO=`sudo dpkg -l | grep perl | wc -l` RESULT=$? if [[ "$RESULT" -ne "0" || "$CHKSUDO" -eq "0" ]]; then echo "There is an unexplainable lack of sudo capability or perl. Aborting." exit 1 fi # The general installation is based on MySQL, if they don't have MySQL, we'll abort. MYSQL=`which mysql 2> /dev/null` MYSQLDUMP=`which mysqldump 2> /dev/null` if [[ "${#MYSQL}" -eq "0" || ! -e $MYSQL ]; then echo "MySQL is not in an expected location. Aborting!" exit 1 fi if [ ! "${MYSQLDUMP}" -eq "0" || -e $MYSQLDUMP ]; then echo "MySQL dump is not in an expected location. Aborting!" exit 1 fi # Where is the install log? INSTALL_LOG=`sudo perl -e "use XML::LibXML; my \$parse = XML::LibXML->new();my \$xmldoc = \$parse->parse_file(\"$KOHA_CONF\");for my \$sample (\$xmldoc->findnodes('yazgfs/config/install_log')) { print \$sample->textContent(); }"` # Grab the database user name DB_USER=`sudo perl -e "use XML::LibXML; my \$parse = XML::LibXML->new();my \$xmldoc = \$parse->parse_file(\"$KOHA_CONF\");for my \$sample (\$xmldoc->findnodes('yazgfs/config/user')) { print \$sample->textContent(); }"` # Grab the database password DB_PASS=`sudo perl -e "use XML::LibXML; my \$parse = XML::LibXML->new();my \$xmldoc = \$parse->parse_file(\"$KOHA_CONF\");for my \$sample (\$xmldoc->findnodes('yazgfs/config/pass')) { print \$sample->textContent(); }"` # Are we running packages? PKG=`sudo dpkg -l | grep koha-common | wc -l` # Are we running git/dev? the INTRANETDIR will have a .git subdirectory. # Determine the INTRANETDIR. INTRANETDIR=`sudo perl -e "use XML::LibXML; my \$parse = XML::LibXML->new();my \$xmldoc = \$parse->parse_file(\"$KOHA_CONF\");for my \$sample (\$xmldoc->findnodes('yazgfs/config/intranetdir')) { print $sample->textContent(); }"` # Is there a .git subdirectory? sudo ls $INTRANETDIR/.git &> /dev/null RESULT=$? if [ "$RESULT" -eq "0" ]; then GIT=1 else GIT=0 fi # If it isn't packages or git, it is likely a tarball. if [[ "$PKG" -eq "0" && "$GIT" -eq "0" ]]; then TARBALL=1 else TARBALL=0 fi # We should only try uninstalling if we figure there is one kind of installation. if [ "$($PKG+$GIT+$TARBALL)" -ne "1" ]; then echo "Unable to determine what kind of installation you have. Aborting." exit 1 fi # Tell the user what kind of installation they have. if [ "$PKG" -eq "1" ]; then echo "You likely have a packages installation." elif [ "$GIT" -eq "1" ]; then echo "You likely have a git installation." else echo "You likely have a tarball installation." fi echo # Check if there are koha databases. LOGIN="--user=$DB_USER --password=$DB_PASS" CHKDBS=`$MYSQL -B -N -e "show databases;" $LOGIN | grep koha | wc -l` if [ "$CHKDBS" -eq "0" ]; then echo "Unable to find koha related databases. Aborting!" exit 1 fi # Dump backups of the instances/database. echo "We will dump copies of the database(s), in case you make a mistake." while read DB2dump; do mysqldump $LOGIN --databases $DB2dump > "~/${DB2dump}.sql" RESULT=$? if [ "$RESULT" -ne "0" ]; then rm ~/${DB2dump}.sql echo "Problem dumping ~/${DB2dump}.sql" else echo "Dumped ~/${DB2dump}.sql" fi done < <($MYSQL -B -N -e "show databases;" $LOGIN | grep koha) # Prompt for confirmation TWICE. echo echo -n "Directories and files will be irrecoverably deleted. Are you sure (y/n)? " FLAG=0 while [ "$FLAG" -eq "0" ]; do read ANSWER if [[ "$(ANSWER:0:1)" =~ [yYnN] ]]; then FLAG=1 else echo "Please type 'y' or 'n' followed by enter/return" fi done if [[ "$ANSWER" =~ [nN] ]]; then echo "Aborting!" exit 1 else echo "There is no turning back. Are you sure (y/n)? " fi FLAG=0 while [ "$FLAG" -eq "0" ]; do read ANSWER if [[ "$(ANSWER:0:1)" =~ [yYnN] ]]; then FLAG=1 else echo "Please type 'y' or 'n' followed by enter/return" fi done if [[ "$ANSWER" =~ [nN] ]]; then echo "Aborting!" exit 1 else echo "Processing will continue." fi if [ ! -d /etc/apache2/sites-enabled ]; then echo "Apache2 sites-enabled is not configured as expected. Aborting!" exit 1 fi if [ ! -d /etc/apache2/sites-available ]; then echo "Apache2 sites-available is not configured as expected. Aborting!" exit 1 fi DAEMONCOUNT=`ls /etc/init.d/*koha* 2> /dev/null | wc -l` if [ "$DAEMONCOUNT" -eq "0" ]; then echo "Missing expected Zebra daemon files. Aborting!" exit 1 fi BIBZEBIDX=`sudo perl -e "use XML::LibXML; my \$parse = XML::LibXML->new();my \$xmldoc = \$parse->parse_file(\"$KOHA_CONF\");for my \$sample (\$xmldoc->findnodes('yazgfs/server/config')) { print \$sample->textContent(),\"\n\"; }" | sort -u | grep bib` if [ "${#BIBZEBIDX}" -eq "0" ]; then echo "Unable to find zebra biblio indexing configuration files. Aborting!" exit 1 fi AUTHZEBIDX=`sudo perl -e "use XML::LibXML; my \$parse = XML::LibXML->new();my \$xmldoc = \$parse->parse_file(\"$KOHA_CONF\");for my \$sample (\$xmldoc->findnodes('yazgfs/server/config')) { print \$sample->textContent(),\"\n\"; }" | sort -u | grep auth` if [ "${#AUTHZEBIDX}" -eq "0" ]; then echo "Unable to find zebra authorities indexing configuration files. Aborting!" exit 1 fi CHKCROND=`sudo ls /etc/cron.d/*koha* 2> /dev/null | wc -l` if [ "$CHKCROND" -eq "0" ]; then echo "Unable to find koha crontab files. Aborting!" exit 1 fi CHKDIRS=`grep koha $INSTALL_LOG | cut -f2 -d'=' | cut -f1-4 -d'/' | grep ^\/ | sort -u | wc -l` if [ "$CHKDIRS" -eq "0" ]; then echo "Unable to determine typical directories to delete. Aborting!" exit 1 fi # For packages, we better remove instances. if [ "$PKG" -eq "1" ]; then echo "koha instances being removed..." while read INSTANCE; do koha-remove $INSTANCE done < <(`koha-list`) fi # This shouldn't hurt for packages now, since it should all be blanked. # and if it isn't, then it probably needs help. ;) # First we'll handle apache. echo "Disabling and removing koha apache site(s)." while read SITE2DIS; do sudo a2dissite `basename $SITE2DIS` sudo rm /etc/apache2/sites-available/`basename $SITE2DIS` done < <(`sudo grep -l koha /etc/apache2/sites-enabled/*`) echo "Restarting apache2." sudo /etc/init.d/apache2 restart # Next we'll work on the koha-zebra-daemon. echo "Removing koha-zebra-daemon." while read DAEMON; do sudo $DAEMON stop &> /dev/null sudo update-rc.d `basename $DAEMON` remove &> /dev/null sudo rm $DAEMON &> /dev/null done < <(`ls /etc/init.d/*koha*`) # Next we'll drop the database echo "koha databases being removed..." while read DB2dump; do mysql $LOGIN -e "drop database $DB2dump;" &> /dev/null RESULT=$? if [ "$RESULT" -ne "0" ]; then echo "Problem dropping ${DB2dump}" else echo "Dropped ${DB2dump}" fi done < <($MYSQL -B -N -e "show databases;" $LOGIN | grep koha) # Cleaning up the Zebra Indexes echo "Cleaning up the Zebra Indexes" echo "Biblios..." sudo zebraidx -c $BIBZEBIDX -g iso2709 -d biblios init echo "Authorities..." sudo zebraidx -c $AUTHZEBIDX -g iso2709 -d authorities init # remove the crontab job echo "Removing Crontab jobs..." sudo rm `ls /etc/cron.d/*koha*` &> /dev/null # a whole bunch of installation directories. echo "The following directories to be deleted:" while read DIR2DEL; do echo $DIR2DEL rm -rf $DIR2DEL &> /dev/null done < <(`grep koha $INSTALL_LOG | cut -f2 -d'=' | cut -f1-4 -d'/' | grep ^\/ | sort -u`) # attempting to delete koha users echo -n "Removing koha users" while read KOHAUSER; do echo "Removing $KOHAUSER" sudo userdel $KOHAUSER &> /dev/null done < <(`sudo cat /etc/passwd | cut -f1 -d: | grep koha`)