Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
3294 rexy 1
#!/bin/bash
2
 
3
#########################
4
## ALCASAR replication ##
5
##        start        ##
6
#########################
7
# The script is designed to start a connection to a remote.
8
 
9
# Constants
10
readonly ALCASAR_PWD="/root/ALCASAR-passwords.txt"
11
 
12
# Dynamically generated constants
13
DB_ROOT_PWD="$(grep db_root "$ALCASAR_PWD" | cut -d '=' -f 2-)"
14
readonly DB_ROOT_PWD;
15
 
16
# Variables
17
remote_to_start=""
18
 
19
 
20
# Check script args
21
# $@: script args
22
check_args() {
23
	# Parse args
24
	args="$(getopt --longoptions "all,name:,help" --options "a,n:,h" -- "$@")"
25
 
26
	# Reset script args list
27
	eval set -- "$args"
28
 
29
	# Print help
30
	if [ "$#" -eq 1 ]
31
	then
32
		usage
33
		return 1
34
	fi
35
 
36
	# Loop over all args
37
	while true
38
	do
39
		case "$1" in
40
			--all | -a)
41
				echo "Starting all connections."
42
				remote_to_start="ALL"
43
				break
44
				;;
45
			--name | -n)
46
				echo "Starting connection to $2."
47
				remote_to_start="$2"
48
				shift
49
				break
50
				;;
51
			--help | -h)
52
				usage
53
				return 2
54
				;;
55
			--)
56
				# End of args
57
				break
58
				;;
59
			*)
60
				echo "error: unknown $1" >&2
61
				return 3
62
				break
63
				;;
64
		esac
65
		shift
66
	done
67
}
68
 
69
# Execute SQL query
70
# $1: query
71
# $2: user (default: root)
72
# $3: password (default: root pwd)
73
# $4: host (default: localhost)
74
# $5: port (default: 3306)
75
exec_query() {
76
	# Check args
77
	if [ $# -lt 1 ]
78
	then
79
		echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
80
		return 2
81
	fi
82
 
83
	# Execute the query
84
	/usr/bin/mariadb --user="root" --password="$DB_ROOT_PWD" --execute="$1"
85
}
86
 
87
# Start all or specific replication with remotes
88
start_remote() {
89
	# Check args
90
	if [ $# -ne 1 ]
91
	then
92
		echo "error: need to pass remote name"
93
		return 1
94
	fi
95
 
96
	name="$1"
97
	service_file="/etc/systemd/system/replication-$name.service"
98
 
99
	# Skip if the connection is received (like on a primary)
100
	if [ -f "$service_file" ]
101
	then
102
		echo "Starting SSH tunnel with '$name'..."
103
		/usr/bin/systemctl start "replication-$name"
104
		/usr/bin/systemctl enable "replication-$name"
105
 
106
		# Get remote IP and port from its name
107
		ip="$(/usr/local/bin/alcasar-replication-list.sh --name=$name | grep "Master_Host" | cut -d ' ' -f 2-)"
108
		port="$(grep "ExecStart" "$service_file" | cut -d ' ' -f 9)"
109
 
110
		echo "Allowing outbound connection to remote SSH..."
111
		/usr/bin/sed -i -E "/^REPLICATION_TO=/s/=(.*)/=\1$ip:$port,/" /usr/local/etc/alcasar.conf
112
		/usr/local/bin/alcasar-iptables.sh
113
	fi
114
 
115
	echo "Starting replication with '$name'..."
116
	exec_query "START REPLICA '$name'" || return
117
}
118
 
119
# Print help message
120
usage() {
121
	echo "usage: $0 OPTIONS"
122
	echo
123
	echo "OPTIONS"
124
	echo "	--all, -a"
125
	echo "		start all connections"
126
	echo "	--name=NAME, -n NAME"
127
	echo "		remote name connection to start"
128
	echo "	--help, -h"
129
	echo "		print this help message"
130
}
131
 
132
# Main
133
check_args "$@" || exit
134
 
135
# Stop all or specific remote
136
if [ "$remote_to_start" == "ALL" ]
137
then
138
	# Get remotes names from database
139
	remotes_names=$(/usr/local/bin/alcasar-replication-list.sh --all | grep "Connection_name" | cut -d ':' -f 2-)
140
 
141
	# Loop over names
142
	for name in $remotes_names
143
	do
144
		start_remote "$name"
145
	done
146
else
147
	start_remote "$remote_to_start"
148
fi