| Line 8... |
Line 8... |
| 8 |
|
8 |
|
| 9 |
# Constants
|
9 |
# Constants
|
| 10 |
readonly ALCASAR_PWD="/root/ALCASAR-passwords.txt"
|
10 |
readonly ALCASAR_PWD="/root/ALCASAR-passwords.txt"
|
| 11 |
readonly LOCALHOST="127.0.0.1"
|
11 |
readonly LOCALHOST="127.0.0.1"
|
| 12 |
readonly DB_PORT=3306
|
12 |
readonly DB_PORT=3306
|
| 13 |
readonly ALCASAR_CONF=/usr/local/etc/alcasar.conf
|
- |
|
| 14 |
|
13 |
|
| 15 |
# Dynamically generated constants
|
14 |
# Dynamically generated constants
|
| 16 |
DB_ROOT_PWD="$(grep db_root "$ALCASAR_PWD" | cut -d '=' -f 2-)"
|
15 |
DB_ROOT_PWD="$(grep db_root "$ALCASAR_PWD" | cut -d '=' -f 2-)"
|
| - |
|
16 |
REPL_DB_USER_PWD="$(grep db_replication_pwd "$ALCASAR_PWD" | cut -d '=' -f 2-)"
|
| 17 |
readonly DB_ROOT_PWD;
|
17 |
readonly DB_ROOT_PWD;
|
| - |
|
18 |
readonly REPL_DB_USER=db_replication
|
| - |
|
19 |
readonly REPL_DB_USER_PWD;
|
| 18 |
|
20 |
|
| 19 |
# Variables
|
21 |
# Variables
|
| 20 |
remote_name=""
|
22 |
remote_name=""
|
| 21 |
remote_addr=""
|
23 |
remote_addr=""
|
| 22 |
remote_ssh_port=""
|
24 |
remote_ssh_port=""
|
| Line 29... |
Line 31... |
| 29 |
|
31 |
|
| 30 |
# Revert modifications already made while adding remote
|
32 |
# Revert modifications already made while adding remote
|
| 31 |
# $1: previous error code
|
33 |
# $1: previous error code
|
| 32 |
abort() {
|
34 |
abort() {
|
| 33 |
error_code="$1"
|
35 |
error_code="$1"
|
| - |
|
36 |
echo "Abort script with $error_code error code"
|
| 34 |
# Revert FW
|
37 |
# Revert FW
|
| 35 |
tmp_disable_outbound_connection
|
38 |
tmp_disable_outbound_connection
|
| - |
|
39 |
# Remove REPLICA
|
| - |
|
40 |
del_remote_as_primary
|
| 36 |
# Delete SSH tunnel service file
|
41 |
# Delete SSH tunnel service file
|
| 37 |
service_file="replication-$remote_name.service"
|
42 |
service_file="replication-$remote_name.service"
|
| 38 |
service_path="/etc/systemd/system/$service_file"
|
43 |
service_path="/etc/systemd/system/$service_file"
|
| 39 |
[ -f "$service_file" ] && rm "$service_file"
|
44 |
[ -f "$service_file" ] && rm "$service_file"
|
| 40 |
return "$error_code"
|
45 |
return "$error_code"
|
| Line 44... |
Line 49... |
| 44 |
add_remote_as_primary() {
|
49 |
add_remote_as_primary() {
|
| 45 |
echo "Adding '$remote_name' as primary..."
|
50 |
echo "Adding '$remote_name' as primary..."
|
| 46 |
exec_query "CHANGE MASTER '$remote_name' TO MASTER_HOST='$LOCALHOST', MASTER_PORT=$bind_port, MASTER_USER='$remote_db_user', MASTER_PASSWORD='$remote_db_pwd', MASTER_USE_GTID=replica_pos"
|
51 |
exec_query "CHANGE MASTER '$remote_name' TO MASTER_HOST='$LOCALHOST', MASTER_PORT=$bind_port, MASTER_USER='$remote_db_user', MASTER_PASSWORD='$remote_db_pwd', MASTER_USE_GTID=replica_pos"
|
| 47 |
}
|
52 |
}
|
| 48 |
|
53 |
|
| - |
|
54 |
# Delete remote as primary
|
| - |
|
55 |
del_remote_as_primary() {
|
| - |
|
56 |
echo "Removing '$remote_name' as primary..."
|
| - |
|
57 |
exec_query "RESET REPLICA '$remote_name' ALL"
|
| - |
|
58 |
}
|
| - |
|
59 |
|
| 49 |
# Verify hostname and IP are not already used by other primary servers
|
60 |
# Verify hostname and IP are not already used by other primary servers
|
| 50 |
check_availability() {
|
61 |
check_availability() {
|
| 51 |
attributes="$(/usr/local/bin/alcasar-replication-list.sh --all)"
|
62 |
attributes="$(/usr/local/bin/alcasar-replication-list.sh --all)"
|
| 52 |
|
63 |
|
| 53 |
# Check for remote name availability
|
64 |
# Check for remote name availability
|
| Line 223... |
Line 234... |
| 223 |
|
234 |
|
| 224 |
# Make a SSH tunnel to remote host
|
235 |
# Make a SSH tunnel to remote host
|
| 225 |
create_ssh_tunnel() {
|
236 |
create_ssh_tunnel() {
|
| 226 |
# Find a common binding port
|
237 |
# Find a common binding port
|
| 227 |
find_common_free_port || return 11
|
238 |
find_common_free_port || return 11
|
| 228 |
|
- |
|
| 229 |
service_file="replication-$remote_name.service"
|
239 |
service_file="replication-$remote_name.service"
|
| 230 |
service_path="/etc/systemd/system/$service_file"
|
240 |
service_path="/etc/systemd/system/$service_file"
|
| 231 |
|
241 |
|
| 232 |
# Write down SSH tunnel service file
|
242 |
# Write down SSH tunnel service file
|
| 233 |
echo "[Unit]
|
243 |
echo "[Unit]
|
| Line 261... |
Line 271... |
| 261 |
if [ $# -lt 1 ]
|
271 |
if [ $# -lt 1 ]
|
| 262 |
then
|
272 |
then
|
| 263 |
echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
|
273 |
echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
|
| 264 |
return 12
|
274 |
return 12
|
| 265 |
fi
|
275 |
fi
|
| 266 |
|
- |
|
| 267 |
# Execute the query
|
276 |
# Execute the query
|
| 268 |
/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
|
277 |
/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
|
| 269 |
}
|
278 |
}
|
| 270 |
|
279 |
|
| - |
|
280 |
retrieve_primary_data() {
|
| - |
|
281 |
# creation of a fresh dump
|
| - |
|
282 |
if ! /usr/bin/ssh -q -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" -- "sudo /usr/local/bin/alcasar-mariadb.sh -d && cp -f /var/Save/base/\$(ls -t /var/Save/base/ | head -n 1) /home/replication/alcasar-users-database-primary.sql.gz"
|
| - |
|
283 |
then
|
| - |
|
284 |
echo "error: cannot create a fresh primary database dump" >&2
|
| - |
|
285 |
return 18
|
| - |
|
286 |
fi
|
| - |
|
287 |
echo "Primary database dump created"
|
| - |
|
288 |
if ! scp -q -P "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr":alcasar-users-database-primary.sql.gz "$remote_ssh_user"@"$remote_addr":db_replication_user_pass.txt /tmp
|
| - |
|
289 |
then
|
| - |
|
290 |
echo "error: cannot retrieve localy the fresh primary database dump" >&2
|
| - |
|
291 |
return 19
|
| - |
|
292 |
fi
|
| - |
|
293 |
echo "Primary database dump locally copied"
|
| - |
|
294 |
readalcasar-mariadb.sh --import /tmp/alcasar-users-database-primary.sql.gz
|
| - |
|
295 |
rm -f /tmp/alcasar-users-database-primary.sql.gz
|
| - |
|
296 |
}
|
| - |
|
297 |
|
| 271 |
find_common_free_port() {
|
298 |
find_common_free_port() {
|
| 272 |
remote_busy_ports_file=/tmp/remote_busy_ports
|
299 |
remote_busy_ports_file=/tmp/remote_busy_ports
|
| 273 |
local_busy_ports_file=/tmp/local_busy_ports
|
300 |
local_busy_ports_file=/tmp/local_busy_ports
|
| 274 |
common_busy_ports_file=/tmp/common_busy_ports
|
301 |
common_busy_ports_file=/tmp/common_busy_ports
|
| 275 |
ports_list_file=/tmp/ports_list
|
302 |
ports_list_file=/tmp/ports_list
|
| Line 375... |
Line 402... |
| 375 |
case "$remote_role" in
|
402 |
case "$remote_role" in
|
| 376 |
primary)
|
403 |
primary)
|
| 377 |
tmp_allow_outbound_connection || abort "$?" || exit
|
404 |
tmp_allow_outbound_connection || abort "$?" || exit
|
| 378 |
check_primary_credentials || abort "$?" || exit
|
405 |
check_primary_credentials || abort "$?" || exit
|
| 379 |
create_ssh_tunnel || abort "$?" || exit
|
406 |
create_ssh_tunnel || abort "$?" || exit
|
| - |
|
407 |
retrieve_primary_data || abort "$?" || exit
|
| 380 |
add_remote_as_primary || abort "$?" || exit
|
408 |
add_remote_as_primary || abort "$?" || exit
|
| 381 |
echo -n "Allowing outbound connection to remote SSH "
|
409 |
echo -n "Allowing outbound connection to remote SSH "
|
| 382 |
# Get remote IP and port from its name
|
410 |
# Get remote IP and port from its name
|
| 383 |
port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
|
411 |
port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
|
| 384 |
ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
|
412 |
ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
|