-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-remove-instance
executable file
·188 lines (164 loc) · 5.5 KB
/
koha-remove-instance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# This script is used to remove a Koha instance from the system.
# This script is licensed under the terms of the MIT License.
# See https://opensource.org/licenses/MIT for more information.
set -euo pipefail
# Define the help function
function help {
echo "Usage: $0 [OPTIONS] INSTANCE1 [INSTANCE2 INSTANCE3 ...]"
echo
echo "Options:"
echo " -k, --keep-mysql Keeps the MySQL database and user for the specified instances."
echo " -p, --purge-all Purges all instance files in /var/lib/koha and /var/spool/koha for the specified instances."
echo
echo "Arguments:"
echo " INSTANCE1 The name of the first Koha instance to remove."
echo " INSTANCE2, INSTANCE3, etc. Optional additional instance names to remove."
echo
echo "This script requires root privileges to run."
echo
echo "Before running this script, make sure that you have stopped all Koha-related services for the instances you want to remove."
echo
echo "If you specify the --keep-mysql option, the script will keep the MySQL database and user for the specified instances. Otherwise, the script will remove the database, user, and associated privileges."
echo
echo "If you specify the --purge-all option, the script will remove all instance files in /var/lib/koha and /var/spool/koha for the specified instances. Otherwise, the script will only remove the configuration files and related data, and keep the instance data."
echo
echo "Examples:"
echo " Remove instance1 and instance2, keeping the MySQL database and user:"
echo " $0 -k instance1 instance2"
echo
echo " Remove instance3 and instance4, purging all instance files:"
echo " $0 -p instance3 instance4"
echo
echo " Remove instance5, purging all instance files and removing the MySQL database and user:"
echo " $0 -pk instance5"
echo
exit 0
}
function is_service_running {
local service="$1"
local name="$2"
if systemctl is-active "$service@$name" >/dev/null; then
return 0
else
return 1
fi
}
function get_tmpdir {
if [ -z "${TMPDIR:-}" ]; then
echo "/tmp"
else
echo "$TMPDIR"
fi
}
# Parse the command-line arguments using getopt
OPTS=$(getopt -o kph --long keep-mysql,purge-all,help -n "$(basename "$0")" -- "$@")
if [ $? != 0 ] || [ $# -eq 0 ]; then
help
fi
eval set -- "$OPTS"
KEEPMYSQL=false
PURGEALL=false
while true; do
case "$1" in
-k | --keep-mysql )
KEEPMYSQL=true
shift
;;
-p | --purge-all )
PURGEALL=true
shift
;;
-h | --help )
help
;;
-- )
shift
break
;;
* )
break
;;
esac
done
NAMES="$@"
SITECONFDIR="/etc/koha/sites"
TEMPDIR=$(get_tmpdir)
# include helper functions
if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
. "/usr/share/koha/bin/koha-functions.sh"
else
echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
exit 1
fi
if [ -z "$NAMES" ] ; then
echo "Please specify a Koha instance name. Your choices are:"
INSTANCES=$(get_instances)
echo "$INSTANCES"
exit 1
fi
for NAME in $NAMES
do
# Does the directory (ie instance) name exist?
if [ ! -d "$SITECONFDIR/$NAME" ] ; then
echo "Koha configuration directory for instance "$NAME" does not exist, please specify a valid Koha instance"
exit 1
fi
echo "Removing Koha instance $NAME"
MYSQL_HOSTNAME="localhost"
if [ "$KEEPMYSQL" != true ]; then
mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
DROP USER IF EXISTS `koha_$NAME`@`%`;
DROP USER IF EXISTS `koha_$NAME`@`$MYSQL_HOSTNAME`;
DROP DATABASE IF EXISTS `koha_$NAME`;
FLUSH PRIVILEGES;
eof
fi
# Stop the Koha-related services
SERVICES=( "koha-zebra" "koha-indexer" "koha-worker" "koha-plack" "koha-sip" )
for SERVICE in "${SERVICES[@]}"; do
if is_service_running "$SERVICE" "$NAME"; then
$SERVICE --stop "$NAME" || true
fi
done
# Remove configuration files
INSTANCEFILE=$(get_apache_config_for "$NAME")
LE_OPACDOMAIN=$(letsencrypt_get_opacdomain_for "$NAME")
for FILE in "$INSTANCEFILE" "/etc/koha/sites/$NAME/koha-conf.xml" "/etc/koha/sites/$NAME/log4perl.conf" "/etc/koha/sites/$NAME/zebra-biblios.cfg" "/etc/koha/sites/$NAME/zebra-biblios-dom.cfg" "/etc/koha/sites/$NAME/zebra-authorities.cfg" "/etc/koha/sites/$NAME/zebra-authorities-dom.cfg" "/etc/koha/sites/$NAME/zebra.passwd" "/etc/apache2/sites-enabled/$NAME.conf"; do
if [ -f "$FILE" ]; then
rm "$FILE"
fi
done
# Remove temporary files
if [ -d "$TEMPDIR/koha_${NAME}upload" ] && [ "$PURGEALL" != true ]; then
rm -r "$TEMPDIR/koha${NAME}_upload"
fi
# Remove instance files
DIRECTORIES=( "/var/lib/koha/$NAME/letsencrypt.enabled" "/etc/letsencrypt/renewal/$LE_OPACDOMAIN.conf" "/etc/koha/sites/$NAME" "/var/lock/koha/$NAME" "/var/log/koha/$NAME" "/var/cache/koha/$NAME" "/var/run/koha/$NAME" )
if [ "$PURGEALL" = true ]; then
DIRECTORIES+=( "/var/lib/koha/$NAME" "/var/spool/koha/$NAME" )
fi
for DIRECTORY in "${DIRECTORIES[@]}"; do
if [ -e "$DIRECTORY" ]; then
rm -r "$DIRECTORY"
fi
done
# Remove user
if getent passwd "$NAME-koha" > /dev/null; then
# Find all processes using the username and stop or kill them
pids=$(pgrep -U "$NAME-koha")
if [ -n "$pids" ]; then
echo "Stopping or killing processes for user $NAME-koha..."
# Try stopping the processes gracefully first
kill -s SIGTERM $pids || true
# If the processes are still running, force kill them
kill -s SIGKILL $pids || true
fi
# Delete the user
deluser --quiet "$NAME-koha"
fi
# Disable site
a2dissite "$NAME" > /dev/null 2>&1 || a2dissite "${NAME}.conf" > /dev/null 2>&1 || true
done
# Restart Apache
systemctl restart apache2