-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-update-db-url
executable file
·179 lines (151 loc) · 4.94 KB
/
koha-update-db-url
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
#!/bin/bash
# This script updates the URL of your Koha database server.
function usage() {
cat <<EOF
Usage: $0 [OPTIONS]
This script updates the URL of your Koha database server.
OPTIONS:
-i, --instance NAME1 NAME2... Names of Koha instances to update.
-u, --username USERNAME Username of the database root user.
-p, --password PASSWORD Password of the database root user.
-h, --hostname HOSTNAME Hostname or URL of the new database server.
-c, --config-file FILEPATH Path to a config file containing options.
-h, --help Display this help message.
EXAMPLES:
$0 --instance koha1 koha2 --hostname db.example.com --username root --password mypassword
$0 -c myconfig.ini
$0 -h
To use a config file, create a file with the following format and pass its path to the --config-file option:
# Example config file for update_db.sh
[options]
instance = koha1 koha2
hostname = db.example.com
new_db_root = root
new_db_pass = mypassword
EOF
}
# Parse arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-i|--instance)
instance="$2"
shift
shift
;;
-u|--username)
new_db_root="$2"
shift
shift
;;
-p|--password)
new_db_pass="$2"
shift
shift
;;
-h|--hostname)
hostname="$2"
shift
shift
;;
-c|--config-file)
config_file="$2"
shift
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
# If config file provided, load options from it
if [[ -n "$config_file" ]]
then
if [[ ! -f "$config_file" ]]
then
echo "Config file not found: $config_file"
usage
exit 1
fi
instance=$(python3 -c "import configparser; config=configparser.ConfigParser(); config.read('$config_file'); print(config.get('options', 'instance'))")
hostname=$(python3 -c "import configparser; config=configparser.ConfigParser(); config.read('$config_file'); print(config.get('options', 'hostname'))")
new_db_root=$(python3 -c "import configparser; config=configparser.ConfigParser(); config.read('$config_file'); print(config.get('options', 'new_db_root'))")
new_db_pass=$(python3 -c "import configparser; config=configparser.ConfigParser(); config.read('$config_file'); print(config.get('options', 'new_db_pass'))")
fi
# Prompt for missing options
if [[ -z "$instance" ]]
then
read -p "Enter the name(s) of the Koha instance(s) you want to migrate separated by a space: " -a instance
fi
if [[ -z "$hostname" ]]
then
read -p "Enter the URL/endpoint of your database server: " hostname
fi
if [[ -z "$new_db_root" ]]
then
read -p "Enter the username of your database root user: " new_db_root
fi
if [[ -z "$new_db_pass" ]]
then
echo "Enter the password for the root user of your database server: "
read -s new_db_pass
fi
# Enable and start Koha instances
for instancename in "${instance[@]}"; do
# Set the path to koha_conf.xml
koha_conf=/etc/koha/sites/$instancename/koha-conf.xml
# Backup the koha-conf.xml file
sudo rsync -a $koha_conf $koha_conf.$(date +%d_%b_%Hh%M_%Z)
# Retrieve the Koha user password in koha-conf.xml
kohadbpwd=$(sudo xmlstarlet sel -t -v 'yazgfs/config/pass' $koha_conf)
echo "The current DB hostname for $instancename is $(sudo xmlstarlet sel -t -v 'yazgfs/config/hostname' $koha_conf)"
# Replace database endpoint with remote db endpoint
sudo xmlstarlet edit -L --update 'yazgfs/config/hostname' --value "$hostname" $koha_conf
echo "The new DB hostname for $instancename is $(sudo xmlstarlet sel -t -v 'yazgfs/config/hostname' $koha_conf)"
# Recreate DB user
mysql -h ${hostname} -u ${new_db_root} -p${new_db_pass} -e "
CREATE DATABASE IF NOT EXISTS koha_${instancename};
DROP USER IF EXISTS 'koha_${instancename}'@'%';
CREATE USER IF NOT EXISTS 'koha_${instancename}'@'%' IDENTIFIED BY '${kohadbpwd}';
GRANT ALL PRIVILEGES ON koha_${instancename}.* to 'koha_${instancename}'@'%';
FLUSH PRIVILEGES;"
done
# Backup the koha-common.cnf and debian.cnf file
sudo rsync -a /etc/mysql/debian.cnf /etc/mysql/debian.cnf.$(date +%d_%b_%Hh%M_%Z)
sudo rsync -a /etc/mysql/koha-common.cnf /etc/mysql/koha-common.cnf.$(date +%d_%b_%Hh%M_%Z)
# Update koha-common.cnf with config for remote db
echo "
[client]
host = ${hostname}
user = ${new_db_root}
password = ${new_db_pass}
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = ${hostname}
user = ${new_db_root}
password = ${new_db_pass}
socket = /var/run/mysqld/mysqld.sock
" | sudo tee /etc/mysql/koha-common.cnf >/dev/null
# Update debian.cnf with config for remote db
echo "
[client]
host = ${hostname}
user = ${new_db_root}
password = ${new_db_pass}
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = ${hostname}
user = ${new_db_root}
password = ${new_db_pass}
socket = /var/run/mysqld/mysqld.sock
" | sudo tee /etc/mysql/debian.cnf >/dev/null
# Reload koha-common and memcached
sudo systemctl restart koha-common.service memcached.service
echo "Success"