| Line 197... |
Line 197... |
| 197 |
return 6
|
197 |
return 6
|
| 198 |
;;
|
198 |
;;
|
| 199 |
esac
|
199 |
esac
|
| 200 |
}
|
200 |
}
|
| 201 |
|
201 |
|
| 202 |
# Test connection to remote system and remote database
|
202 |
# Test connection to remote system and remote database before creating SSH tunnel.
|
| 203 |
# before making SSH tunnel.
|
- |
|
| 204 |
check_primary_credentials() {
|
203 |
check_primary_credentials() {
|
| 205 |
# Test SSH credentials
|
204 |
# Test SSH credentials
|
| 206 |
if ! /usr/bin/ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" exit
|
205 |
if ! /usr/bin/ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" exit
|
| 207 |
then
|
206 |
then
|
| 208 |
echo "error: cannot SSH with '$remote_ssh_user' to $remote_addr:$remote_ssh_port" >&2
|
207 |
echo "error: cannot SSH with '$remote_ssh_user' to $remote_addr:$remote_ssh_port" >&2
|
| 209 |
echo "hint: have you deployed root pubkey on the remote?"
|
208 |
echo "hint: have you deployed root pubkey on the remote?"
|
| 210 |
return 7
|
209 |
return 7
|
| 211 |
fi
|
210 |
fi
|
| 212 |
echo "Successfully connected with '$remote_ssh_user' to primary ($remote_addr:$remote_ssh_port)"
|
211 |
echo "Successfully connected with '$remote_ssh_user' to primary ($remote_addr:$remote_ssh_port)"
|
| 213 |
|
212 |
|
| 214 |
# exchange of db_replication pass
|
213 |
# Retrieve remote db_replication pwd
|
| 215 |
if ! /usr/bin/scp -q -P "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr":db_replication_user_pass.txt /tmp/db_replication_user_pass.txt
|
214 |
if ! /usr/bin/scp -q -P "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr":local-db_replication-pwd.txt /tmp/primary-db_replication-pwd.txt
|
| 216 |
then
|
215 |
then
|
| 217 |
echo "error: cannot retrieve remote primary db_user pass" >&2
|
216 |
echo "error: cannot retrieve remote primary db_user pwd" >&2
|
| 218 |
return 7
|
217 |
return 7
|
| 219 |
fi
|
218 |
fi
|
| - |
|
219 |
remote_db_pwd=$(cat /tmp/primary-db_replication-pwd.txt)
|
| - |
|
220 |
rm /tmp/primary-db_replication-pwd.txt
|
| 220 |
echo "Successfully retrieve remote primary db_user pass"
|
221 |
echo "Successfully retrieve remote primary db_user pass"
|
| 221 |
remote_db_pwd=$(cat /tmp/db_replication_user_pass.txt)
|
- |
|
| 222 |
rm /tmp/db_replication_user_pass.txt
|
- |
|
| 223 |
hostname="$(/usr/local/bin/alcasar-replication-ssh-keys-management.sh --show-pubkey | cut -d' ' -f3 | cut -d'@' -f2)"
|
- |
|
| 224 |
echo $hostname
|
- |
|
| 225 |
if ! /usr/bin/scp -q -P "$remote_ssh_port" /home/replication/db_replication_user_pass.txt "$remote_ssh_user"@"$remote_addr":"$hostname"_db_replication_user_pass.txt
|
- |
|
| 226 |
then
|
- |
|
| 227 |
echo "error: cannot send primary db_user pass" >&2
|
- |
|
| 228 |
return 7
|
- |
|
| 229 |
fi
|
- |
|
| 230 |
echo "Successfully send primary db_user pass"
|
- |
|
| 231 |
|
222 |
|
| 232 |
# Test database credentials
|
223 |
# Test database credentials
|
| 233 |
if ! /usr/bin/ssh -q -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" -- /usr/bin/mariadb --user="$remote_db_user" --password="$remote_db_pwd" --execute="QUIT"
|
224 |
if ! /usr/bin/ssh -q -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" -- /usr/bin/mariadb --user="$remote_db_user" --password="$remote_db_pwd" --execute="QUIT"
|
| 234 |
then
|
225 |
then
|
| 235 |
echo "error: cannot connect with '$remote_db_user' to remote database" >&2
|
226 |
echo "error: cannot connect with '$remote_db_user' to remote database" >&2
|
| Line 246... |
Line 237... |
| 246 |
return 9
|
237 |
return 9
|
| 247 |
fi
|
238 |
fi
|
| 248 |
echo "Successfully connected with '$remote_db_user' to remote secondary database on port $bind_port"
|
239 |
echo "Successfully connected with '$remote_db_user' to remote secondary database on port $bind_port"
|
| 249 |
}
|
240 |
}
|
| 250 |
|
241 |
|
| 251 |
# Make a SSH tunnel to remote host
|
242 |
# Add a systemd unit to create SSH tunnel to remote primary
|
| 252 |
create_ssh_tunnel() {
|
243 |
create_ssh_tunnel() {
|
| 253 |
# Find a common binding port
|
244 |
# Find a common binding port
|
| 254 |
find_common_free_port || return 11
|
245 |
find_common_free_port || return 11
|
| 255 |
service_file="replication-$remote_name.service"
|
246 |
service_file="replication-$remote_name.service"
|
| 256 |
service_path="/etc/systemd/system/$service_file"
|
247 |
service_path="/etc/systemd/system/$service_file"
|
| Line 291... |
Line 282... |
| 291 |
fi
|
282 |
fi
|
| 292 |
# Execute the query
|
283 |
# Execute the query
|
| 293 |
/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
|
284 |
/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
|
| 294 |
}
|
285 |
}
|
| 295 |
|
286 |
|
| - |
|
287 |
# create, retrieve & import remote primary database
|
| 296 |
retrieve_primary_database() {
|
288 |
retrieve_primary_database() {
|
| 297 |
# creation of a fresh dump
|
289 |
# creation of a fresh dump
|
| 298 |
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"
|
290 |
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"
|
| 299 |
then
|
291 |
then
|
| 300 |
echo "error: cannot create a fresh primary database dump" >&2
|
292 |
echo "error: cannot create a fresh primary database dump" >&2
|
| Line 309... |
Line 301... |
| 309 |
echo "Primary database dump locally copied"
|
301 |
echo "Primary database dump locally copied"
|
| 310 |
alcasar-mariadb.sh --import /tmp/alcasar-users-database-primary.sql.gz
|
302 |
alcasar-mariadb.sh --import /tmp/alcasar-users-database-primary.sql.gz
|
| 311 |
rm -f /tmp/alcasar-users-database-primary.sql.gz
|
303 |
rm -f /tmp/alcasar-users-database-primary.sql.gz
|
| 312 |
}
|
304 |
}
|
| 313 |
|
305 |
|
| - |
|
306 |
push_local_info_to_primary() {
|
| - |
|
307 |
hostname="$(/usr/local/bin/alcasar-replication-ssh-keys-management.sh --show-pubkey | cut -d' ' -f3 | cut -d'@' -f2)"
|
| - |
|
308 |
active_db_port="$(/usr/local/bin/alcasar-replication-list.sh --all |grep Master_Port|cut -d" " -f2)"
|
| - |
|
309 |
if ! /usr/bin/scp -q -P "$remote_ssh_port" /home/replication/local-db_replication-pwd.txt "$remote_ssh_user"@"$remote_addr":secondary-"$hostname"-"$active_db_port"-db_replication-pwd.txt
|
| - |
|
310 |
then
|
| - |
|
311 |
echo "error: cannot send primary db_user pass" >&2
|
| - |
|
312 |
return 7
|
| - |
|
313 |
fi
|
| - |
|
314 |
echo "Successfully send primary db_user pass"
|
| - |
|
315 |
}
|
| - |
|
316 |
|
| 314 |
find_common_free_port() {
|
317 |
find_common_free_port() {
|
| 315 |
remote_busy_ports_file=/tmp/remote_busy_ports
|
318 |
remote_busy_ports_file=/tmp/remote_busy_ports
|
| 316 |
local_busy_ports_file=/tmp/local_busy_ports
|
319 |
local_busy_ports_file=/tmp/local_busy_ports
|
| 317 |
common_busy_ports_file=/tmp/common_busy_ports
|
320 |
common_busy_ports_file=/tmp/common_busy_ports
|
| 318 |
ports_list_file=/tmp/ports_list
|
321 |
ports_list_file=/tmp/ports_list
|
| Line 420... |
Line 423... |
| 420 |
tmp_allow_outbound_connection || abort "$?" || exit
|
423 |
tmp_allow_outbound_connection || abort "$?" || exit
|
| 421 |
check_primary_credentials || abort "$?" || exit
|
424 |
check_primary_credentials || abort "$?" || exit
|
| 422 |
create_ssh_tunnel || abort "$?" || exit
|
425 |
create_ssh_tunnel || abort "$?" || exit
|
| 423 |
retrieve_primary_database || abort "$?" || exit
|
426 |
retrieve_primary_database || abort "$?" || exit
|
| 424 |
add_remote_as_primary || abort "$?" || exit
|
427 |
add_remote_as_primary || abort "$?" || exit
|
| - |
|
428 |
push_local_info_to_primary || abort "$?" || exit
|
| 425 |
echo -n "Allowing outbound connection to remote SSH "
|
429 |
echo -n "Allowing outbound connection to remote SSH "
|
| 426 |
# Get remote IP and port from its name
|
430 |
# Get remote IP and port from its name
|
| 427 |
port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
|
431 |
port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
|
| 428 |
ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
|
432 |
ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
|
| 429 |
echo "($ip:$port)"
|
- |
|
| 430 |
/usr/bin/sed -i -E "/^REPLICATION_TO=/s/=(.*)/=\1$ip:$port,/" /usr/local/etc/alcasar.conf
|
433 |
/usr/bin/sed -i -E "/^REPLICATION_TO=/s/=(.*)/=\1$ip:$port,/" /usr/local/etc/alcasar.conf
|
| 431 |
/usr/local/bin/alcasar-iptables.sh
|
434 |
/usr/local/bin/alcasar-iptables.sh
|
| 432 |
;;
|
435 |
;;
|
| 433 |
secondary)
|
436 |
secondary)
|
| 434 |
check_secondary_credentials || exit
|
437 |
check_secondary_credentials || exit
|