3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010 Catalyst IT, Ltd
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Read configuration variable file if it is present
20 [ -r /etc
/default
/koha-common
] && .
/etc
/default
/koha-common
24 # include helper functions
25 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
26 .
"/usr/share/koha/bin/koha-functions.sh"
28 echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
37 Creates new Koha instances.
41 $scriptname [DB usage mode] [options] instancename
44 --create-db Create a new database on localhost. (default).
45 --request-db Creates a instancename-db-request.txt file where
46 you adjust your DB settings and re-run with --populate-db.
47 --populate-db Finish the installation you started with --request-db after
48 you adjusted the instancename-db-request.txt file.
49 --use-db Use this option if you already created and populated your DB.
52 --marcflavor flavor Set the MARC flavor. Valid values are marc21 (default),
54 --zebralang lang Choose the primary language for Zebra indexing. Valid
55 values are en (default), es, fr, gr, nb, ru and uk.
56 --auth-idx idx_mode Set the indexing mode for authority records. Valid
57 values are dom (default) and grs1.
58 --biblio-idx idx_mode Set the indexing mode for bibliographic records.
59 Valid values are dom (default) and grs1.
60 --use-memcached Set the instance to make use of memcache.
61 --memcached-servers str Set a comma-separated list of host:port memcached servers.
62 --memcached-prefix str Set the desired prefix for the instance memcached namespace.
63 --enable-sru Enable the Z39.50/SRU server (default: disabled).
64 --sru-port Specifiy a TCP port number for the Z39.50/SRU server
65 to listen on. (default: 7090).
66 --defaultsql some.sql Specify a default SQL file to be loaded on the DB.
67 --configfile cfg_file Specify an alternate config file for reading default values.
68 --passwdfile passwd Specify an alternate passwd file.
69 --database dbname Enforce the use of the specified DB name (64 char limit)
70 --adminuser n Explicit the admin user ID in the DB. Relevant in
71 conjunction with --defaultsql and --populate-db.
72 --upload-path dir Set a user defined upload_path. It defaults to
73 /var/lib/koha/<instance>/uploads
74 --help,-h Show this help.
76 Note: the instance name cannot be longer that 11 chars.
81 # UPPER CASE VARIABLES - from configfile or default value
82 # lower case variables - generated within this script
83 generate_config_file
() {
85 chown
"root:$username" "$2"
87 sed -e "s/__KOHASITE__/$name/g" \
88 -e "s/__OPACPORT__/$OPACPORT/g" \
89 -e "s/__INTRAPORT__/$INTRAPORT/g" \
90 -e "s/__OPACSERVER__/$opacdomain/g" \
91 -e "s/__INTRASERVER__/$intradomain/g" \
92 -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
93 -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
94 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
95 -e "s/__SRU_BIBLIOS_PORT__/$SRU_SERVER_PORT/g" \
96 -e "s/__START_SRU_PUBLICSERVER__/$START_SRU_PUBLICSERVER/g" \
97 -e "s/__END_SRU_PUBLICSERVER__/$END_SRU_PUBLICSERVER/g" \
98 -e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
99 -e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
100 -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
101 -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
102 -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
103 -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
104 -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
105 -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
106 -e "s/__DB_NAME__/$mysqldb/g" \
107 -e "s/__DB_HOST__/$mysqlhost/g" \
108 -e "s/__DB_USER__/$mysqluser/g" \
109 -e "s/__DB_PASS__/$mysqlpwd/g" \
110 -e "s/__UNIXUSER__/$username/g" \
111 -e "s/__UNIXGROUP__/$username/g" \
112 -e "s#__UPLOAD_PATH__#$UPLOAD_PATH#g" \
113 -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
114 -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
115 -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
116 "/etc/koha/$1" > "$2"
122 /^\[/ { inclient = 0 }
123 /^\[client\]/ { inclient = 1 }
124 inclient && /^ *host *=/ { print $3 }' \
125 /etc
/mysql
/koha-common.cnf
128 getinstancemysqlpassword
() {
129 xmlstarlet sel
-t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
132 getinstancemysqluser
() {
133 xmlstarlet sel
-t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
136 getinstancemysqldatabase
() {
137 xmlstarlet sel
-t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
140 check_apache_config
()
143 # Check that mpm_itk is installed and enabled
144 if ! /usr
/sbin
/apachectl
-M 2> /dev
/null |
grep -q 'mpm_itk'; then
145 # Check Apache version
146 APACHE_DISABLE_MPM_MSG
=""
147 if /usr
/sbin
/apache2ctl
-v |
grep -q "Server version: Apache/2.4"; then
148 # mpm_event or mpm_worker need to be disabled first. mpm_itk depends
149 # on mpm_prefork, which is enabled if needed. See
150 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734865
151 if /usr
/sbin
/apachectl
-M 2> /dev
/null |
grep -q 'mpm_event'; then
152 APACHE_DISABLE_MPM_MSG
=" sudo a2dismod mpm_event ;"
153 elif /usr
/sbin
/apachectl
-M 2> /dev
/null |
grep -q 'mpm_worker'; then
154 APACHE_DISABLE_MPM_MSG
=" sudo a2dismod mpm_worker ;"
155 # else mpm_prefork: a2enmod mpm_itk works
157 # else Apache 2.2: a2enmod mpm_itk works
162 Koha requires mpm_itk to be enabled within Apache in order to run.
163 Typically this can be enabled with:
165 $APACHE_DISABLE_MPM_MSG sudo a2enmod mpm_itk
171 # Check that mod_rewrite is installed and enabled.
172 if ! /usr
/sbin
/apachectl
-M 2> /dev
/null |
grep -q 'rewrite_module'; then
175 Koha requires mod_rewrite to be enabled within Apache in order to run.
176 Typically this can be enabled with:
183 # Check that the CGI module is installed and enabled
184 # (Apache 2.4 may not have it by default.)
185 if ! /usr
/sbin
/apachectl
-M 2> /dev
/null |
grep -q 'cgi_module'; then
187 Koha requires mod_cgi to be enabled within Apache in order to run.
188 Typically this can be enabled with:
197 set_biblios_indexing_mode
()
199 local indexing_mode
=$1
202 case $indexing_mode in
204 START_BIBLIOS_RETRIEVAL_INFO
=`cat <<EOF
205 <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
206 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
211 END_BIBLIOS_RETRIEVAL_INFO
=`cat <<EOF
216 BIBLIOS_INDEXING_MODE
="dom"
217 ZEBRA_BIBLIOS_CFG
="zebra-biblios-dom.cfg"
220 START_BIBLIOS_RETRIEVAL_INFO
=" <retrievalinfo>"
221 END_BIBLIOS_RETRIEVAL_INFO
=" <\/retrievalinfo>"
222 BIBLIOS_INDEXING_MODE
="grs1"
223 ZEBRA_BIBLIOS_CFG
="zebra-biblios.cfg"
226 die
"Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
232 set_authorities_indexing_mode
()
234 local indexing_mode
=$1
237 case $indexing_mode in
239 START_AUTHORITIES_RETRIEVAL_INFO
=`cat <<EOF
240 <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
241 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
246 END_AUTHORITIES_RETRIEVAL_INFO
=`cat <<EOF
251 AUTHORITIES_INDEXING_MODE
="dom"
252 ZEBRA_AUTHORITIES_CFG
="zebra-authorities-dom.cfg"
255 START_AUTHORITIES_RETRIEVAL_INFO
=" <retrievalinfo>"
256 END_AUTHORITIES_RETRIEVAL_INFO
=" <\/retrievalinfo>"
257 AUTHORITIES_INDEXING_MODE
="grs1"
258 ZEBRA_AUTHORITIES_CFG
="zebra-authorities.cfg"
261 die
"Error: '$indexing_mode' is not a valid indexing mode for authority records."
271 if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
272 MEMCACHED_SERVERS
=$CLO_MEMCACHED_SERVERS
274 if [ "$MEMCACHED_SERVERS" = "" ]; then
275 MEMCACHED_SERVERS
=$DEFAULT_MEMCACHED_SERVERS
276 # else: was set by the koha-sites.conf file
280 if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
281 MEMCACHED_NAMESPACE
="$CLO_MEMCACHED_PREFIX$instance"
283 if [ "$MEMCACHED_PREFIX" != "" ]; then
284 MEMCACHED_NAMESPACE
="$MEMCACHED_PREFIX$instance"
286 MEMCACHED_NAMESPACE
="$DEFAULT_MEMCACHED_PREFIX$instance"
296 if [ "$CLO_UPLOAD_PATH" != "" ]; then
297 UPLOAD_PATH
=$CLO_UPLOAD_PATH
299 UPLOAD_PATH
="$UPLOAD_PATH_BASE/$instance/$UPLOAD_DIR"
305 # remove the commenting symbols
306 START_SRU_PUBLICSERVER
=""
307 END_SRU_PUBLICSERVER
=""
308 if [ "$SRU_SERVER_PORT" = "" ]; then
309 # --sru-port not passed, use the default
310 SRU_SERVER_PORT
=$DEFAULT_SRU_SERVER_PORT
314 # Set defaults and read config file, if it exists.
323 ZEBRA_MARC_FORMAT
="marc21"
326 PASSWDFILE
="/etc/koha/passwd"
328 # memcached variables
332 # hardcoded memcached defaults
333 DEFAULT_MEMCACHED_SERVERS
="127.0.0.1:11211"
334 DEFAULT_MEMCACHED_PREFIX
="koha_"
335 # hardcoded upload_path
336 UPLOAD_PATH_BASE
="/var/lib/koha"
340 # SRU server variables
343 # hardcoded default SRU server port
344 DEFAULT_SRU_SERVER_PORT
="7090"
345 START_SRU_PUBLICSERVER
="<!--"
346 END_SRU_PUBLICSERVER
="-->"
348 # Indexing mode variables (default is DOM)
349 BIBLIOS_INDEXING_MODE
="dom"
350 AUTHORITIES_INDEXING_MODE
="dom"
352 START_BIBLIOS_RETRIEVAL_INFO
=""
353 END_BIBLIOS_RETRIEVAL_INFO
=""
354 START_AUTHORITIES_RETRIEVAL_INFO
=""
355 END_AUTHORITIES_RETRIEVAL_INFO
=""
357 if [ -e /etc
/koha
/koha-sites.conf
]
359 .
/etc
/koha
/koha-sites.conf
362 [ $# -ge 1 ] && [ $# -le 16 ] ||
( usage
; die
"Error: wrong parameters" )
364 TEMP
=`getopt -o chrpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,use-memcached,enable-sru,sru-port:,help,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser:,memcached-servers:,memcached-prefix:,upload-path:, \
367 # Note the quotes around `$TEMP': they are essential!
370 # Temporary variables for the command line options
371 CLO_ZEBRA_MARC_FORMAT
=""
372 CLO_ZEBRA_LANGUAGE
=""
375 CLO_BIBLIOS_INDEXING_MODE
=""
376 CLO_AUTHORITIES_INDEXING_MODE
=""
377 CLO_MEMCACHED_SERVERS
=""
378 CLO_MEMCACHED_PREFIX
=""
387 op
=request
; shift ;;
389 op
=populate
; shift ;;
393 USE_MEMCACHED
="yes" ; shift ;;
395 CLO_MEMCACHED_SERVERS
="$2" ; shift 2 ;;
397 CLO_MEMCACHED_PREFIX
="$2" ; shift 2;;
399 CLO_ZEBRA_MARC_FORMAT
="$2" ; shift 2 ;;
401 CLO_ZEBRA_LANGUAGE
="$2" ; shift 2 ;;
403 CLO_AUTHORITIES_INDEXING_MODE
="$2" ; shift 2 ;;
405 CLO_BIBLIOS_INDEXING_MODE
="$2" ; shift 2 ;;
407 CLO_DEFAULTSQL
="$2" ; shift 2 ;;
409 configfile
="$2" ; shift 2 ;;
411 CLO_PASSWDFILE
="$2" ; shift 2 ;;
413 CLO_DATABASE
="$2" ; shift 2 ;;
415 CLO_ADMINUSER
="$2" ; shift 2 ;;
417 ENABLE_SRU
="yes" ; shift ;;
419 SRU_SERVER_PORT
="$2" ; shift 2 ;;
421 CLO_UPLOAD_PATH
="$2" ; shift 2 ;;
427 die
"Internal error processing command line arguments" ;;
431 # Load the configfile given on the command line
432 if [ "$configfile" != "" ]
434 if [ -e "$configfile" ]
438 die
"$configfile does not exist.";
442 # Make sure options from the command line get the highest precedence
443 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
445 ZEBRA_MARC_FORMAT
="$CLO_ZEBRA_MARC_FORMAT"
447 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
449 ZEBRA_LANGUAGE
="$CLO_ZEBRA_LANGUAGE"
451 if [ "$CLO_DEFAULTSQL" != "" ]
453 DEFAULTSQL
="$CLO_DEFAULTSQL"
455 if [ "$CLO_ADMINUSER" != "" ]
457 ADMINUSER
="$CLO_ADMINUSER"
459 if [ "$CLO_PASSWDFILE" != "" ]
461 PASSWDFILE
="$CLO_PASSWDFILE"
464 if [ "$CLO_BIBLIOS_INDEXING_MODE" != "" ]; then
465 BIBLIOS_INDEXING_MODE
=$CLO_BIBLIOS_INDEXING_MODE
468 set_biblios_indexing_mode
$BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
470 if [ "$ENABLE_SRU" != "no" ]; then
474 if [ "$CLO_AUTHORITIES_INDEXING_MODE" != "" ]; then
475 AUTHORITIES_INDEXING_MODE
=$CLO_AUTHORITIES_INDEXING_MODE
478 set_authorities_indexing_mode
$AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
482 set_upload_path
$name
484 if [ "$USE_MEMCACHED" = "yes" ]; then
486 elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
487 [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
491 Error: you provided memcached configuration switches but memcached is not enabled.
492 Please set USE_MEMCACHED="yes" on /etc/koha/koha-sites.conf or use the
493 --use-memcached option switch to enable it.
499 # Unset memcached-related variables
504 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
505 # we look into this first.
508 die
"This script must be run with root privileges."
511 # Check everything is ok with Apache, die otherwise
514 opacdomain
="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
515 intradomain
="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
518 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
520 passwdline
=`cat $PASSWDFILE | grep "^$name:"`
521 mysqluser
=`echo $passwdline | cut -d ":" -f 2`
522 mysqlpwd
=`echo $passwdline | cut -d ":" -f 3`
523 mysqldb
=`echo $passwdline | cut -d ":" -f 4`
526 # The order of precedence for MySQL database name is:
527 # default < passwd file < command line
528 if [ "$mysqldb" = "" ]
532 if [ "$CLO_DATABASE" != "" ]
534 mysqldb
="$CLO_DATABASE"
537 if [ "$mysqluser" = "" ]
539 mysqluser
="koha_$name"
541 mysqlhost
="$(getmysqlhost)"
543 if [ "$op" = create
] ||
[ "$op" = request
] ||
[ "$op" = use
]
545 if [ "$mysqlpwd" = "" ]
547 mysqlpwd
="$(pwgen -s 16 1)"
550 mysqlpwd
="$(getinstancemysqlpassword $name)"
554 if [ "$op" = create
] ||
[ "$op" = request
] ||
[ "$op" = use
]
556 # Create new user and group.
557 username
="$name-koha"
558 if getent passwd
"$username" > /dev
/null
560 die
"User $username already exists."
562 if getent group
"$username" > /dev
/null
564 die
"Group $username already exists."
566 adduser
--no-create-home --disabled-login \
567 --gecos "Koha instance $username" \
568 --home "/var/lib/koha/$name" \
571 # Create the site-specific directories.
572 koha-create-dirs
"$name"
574 # Generate Zebra database password.
575 zebrapwd
="$(pwgen -s 16 1)"
576 # Future enhancement: make this configurable for when your db is on
578 mysql_hostname
="localhost"
579 # Set up MySQL database for this instance.
580 if [ "$op" = create
]
582 mysql
--defaults-extra-file=/etc
/mysql
/koha-common.cnf
<<eof
583 CREATE DATABASE \`$mysqldb\`;
584 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
585 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
586 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
593 mysql
--defaults-extra-file=/etc
/mysql
/koha-common.cnf
--force <<eof
594 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
595 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
596 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
601 # Generate and install Apache site-available file and log dir.
602 generate_config_file apache-site.conf.
in \
603 "/etc/apache2/sites-available/$name.conf"
604 mkdir
"/var/log/koha/$name"
605 chown
"$username:$username" "/var/log/koha/$name"
608 # Generate and install main Koha config file.
609 generate_config_file koha-conf-site.xml.
in \
610 "/etc/koha/sites/$name/koha-conf.xml"
612 # Generate and install the log4perl config file.
613 generate_config_file log4perl-site.conf.
in \
614 "/etc/koha/sites/$name/log4perl.conf"
616 # Generate and install Zebra config files.
617 generate_config_file zebra-biblios-site.cfg.
in \
618 "/etc/koha/sites/$name/zebra-biblios.cfg"
619 generate_config_file zebra-biblios-dom-site.cfg.
in \
620 "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
621 generate_config_file zebra-authorities-site.cfg.
in \
622 "/etc/koha/sites/$name/zebra-authorities.cfg"
623 generate_config_file zebra-authorities-dom-site.cfg.
in \
624 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
625 generate_config_file zebra.passwd.
in \
626 "/etc/koha/sites/$name/zebra.passwd"
629 # Create a GPG-encrypted file for requesting a DB to be set up.
630 if [ "$op" = request
]
632 touch "$name-db-request.txt"
633 chmod 0600 "$name-db-request.txt"
634 cat > "$name-db-request.txt" << eof
635 Please create a MySQL database and user on $mysqlhost as follows:
637 database name: $mysqldb
638 database user: $mysqluser
644 echo "See $name-db-request.txt for database creation request."
645 echo "Please forward it to the right person, and then run"
646 echo "$0 --populate-db $name"
652 if [ "$op" = create
] ||
[ "$op" = populate
]
654 # Re-fetch the passwords from the config we've generated, allows it
655 # to be different from what we set, in case the user had to change
657 mysqluser
=$
(getinstancemysqluser
$name)
658 mysqldb
=$
(getinstancemysqldatabase
$name)
659 # Use the default database content if that exists.
660 if [ -e "$DEFAULTSQL" ]
662 # Populate the database with default content.
664 sed "s/__KOHASITE__/$name/g" |
665 mysql
--host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
668 # Change the default user's password.
669 staffpass
="$(pwgen 12 1)"
670 staffdigest
=$
(echo -n "$staffpass" |
672 use Digest::MD5 qw(md5_base64);
673 while (<>) { print md5_base64($_), "\n"; }')
674 mysql
--host="$mysqlhost" --user="$mysqluser" \
675 --password="$mysqlpwd" <<eof
678 SET password = '$staffdigest'
679 WHERE borrowernumber = $ADMINUSER;
682 echo "staff user password is '$staffpass' but keep that secret"
684 # Upgrade the database schema, just in case the dump was from an
686 koha-upgrade-schema
"$name"
688 echo "Koha instance is empty, no staff user created."
693 if [ "$op" = create
] ||
[ "$op" = populate
] ||
[ "$op" = use
]
695 # Reconfigure Apache.
697 a2ensite
"$name" > /dev
/null
2>&1 ||
698 a2ensite
"${name}.conf" > /dev
/null
2>&1
700 echo "Warning: problem enabling $name in Apache" >&2
702 service apache2 restart
705 koha-start-zebra
"$name"
707 if [ "$USE_INDEXER_DAEMON" = "yes" ]; then
708 # Start Indexer daemon
709 koha-indexer
--start "$name"
714 if [ "$op" = request
]
721 Email for this instance is disabled. When you're ready to enable it, use:
722 koha-email-enable $name