Subversion Repositories ALCASAR

Rev

Rev 3319 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
3294 rexy 1
#!/bin/bash
2
 
3
#########################
4
## ALCASAR replication ##
5
##       connect       ##
6
#########################
7
# The script is designed to connect instance to a remote ALCASAR.
8
 
9
# Constants
10
readonly ALCASAR_PWD="/root/ALCASAR-passwords.txt"
11
readonly LOCALHOST="127.0.0.1"
12
readonly DB_PORT=3306
13
 
14
# Dynamically generated constants
15
DB_ROOT_PWD="$(grep db_root "$ALCASAR_PWD" | cut -d '=' -f 2-)"
3318 rexy 16
REPL_DB_USER_PWD="$(grep db_replication_pwd "$ALCASAR_PWD" | cut -d '=' -f 2-)"
3294 rexy 17
readonly DB_ROOT_PWD;
3318 rexy 18
readonly REPL_DB_USER=db_replication
19
readonly REPL_DB_USER_PWD;
3294 rexy 20
 
21
# Variables
22
remote_name=""
23
remote_addr=""
24
remote_ssh_port=""
25
remote_ssh_user=""
26
remote_db_user=""
27
remote_db_pwd=""
28
remote_role=""
29
bind_port=""
30
 
31
 
32
# Revert modifications already made while adding remote
33
# $1: previous error code
34
abort() {
35
	error_code="$1"
3318 rexy 36
	echo "Abort script with $error_code error code"
3294 rexy 37
	# Revert FW
38
	tmp_disable_outbound_connection
3318 rexy 39
	# Remove REPLICA
40
	del_remote_as_primary
3294 rexy 41
	# Delete SSH tunnel service file
42
	service_file="replication-$remote_name.service"
43
	service_path="/etc/systemd/system/$service_file"
44
	[ -f "$service_file" ] && rm "$service_file"
45
	return "$error_code"
46
}
47
 
48
# Add remote as primary
49
add_remote_as_primary() {
50
	echo "Adding '$remote_name' as primary..."
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"
52
}
53
 
3318 rexy 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
 
3294 rexy 60
# Verify hostname and IP are not already used by other primary servers
61
check_availability() {
62
	attributes="$(/usr/local/bin/alcasar-replication-list.sh --all)"
63
 
64
	# Check for remote name availability
65
	echo "$attributes" | grep -q "$remote_name"
66
	if [ "$?" -eq 0 ]
67
	then
68
		echo "error: name '$remote_name' already used" >&2
69
		return 15
70
	fi
71
 
72
	# Check for remote IP availability
73
	echo "$attributes" | grep -q "$remote_addr"
74
	if [ "$?" -eq 0 ] && [ -n "$remote_addr" ]
75
	then
76
		echo "error: address '$remote_addr' already used" >&2
77
		return 16
78
	fi
79
 
80
	# Check for binding port availability
81
	echo "$attributes" | grep -q "$bind_port"
82
	if [ "$?" -eq 0 ] && [ -n "$bind_port" ]
83
	then
84
		echo "error: binding port '$bind_port' already used" >&2
85
		return 17
86
	fi
87
}
88
 
89
# Check script args
90
# $@: script args
91
check_args() {
92
	# Parse args
93
	args="$(getopt --longoptions "to-primary,to-secondary,name:,address:,port:,user:,db-user:,db-password:,bind-port:,help" --options "n:,a:,p:,u:,h" -- "$@")"
94
 
95
	# Reset script args list
96
	eval set -- "$args"
97
 
98
	# Print help
99
	if [ "$#" -eq 1 ]
100
	then
101
		usage
102
		return 1
103
	fi
104
 
105
	# Loop over all args
106
	while true
107
	do
108
		case "$1" in
109
			--to-primary)
110
				echo "Remote role: primary"
111
				remote_role="primary"
112
				;;
113
			--to-secondary)
114
				echo "Remote role: secondary"
115
				remote_role="secondary"
116
				;;
117
			--name | -n)
118
				echo "Remote name: $2"
119
				remote_name="$2"
120
				shift
121
				;;
122
			--address | -a)
123
				echo "Remote address: $2"
124
				remote_addr="$2"
125
				shift
126
				;;
127
			--port | -p)
128
				echo "Remote SSH port: $2"
129
				remote_ssh_port="$2"
130
				shift
131
				;;
132
			--user | -u)
133
				echo "Remote user: $2"
134
				remote_ssh_user="$2"
135
				shift
136
				;;
137
			--db-user)
138
				echo "Remote database user: $2"
139
				remote_db_user="$2"
140
				shift
141
				;;
142
			--db-password)
143
				echo "Remote database user password: $2"
144
				remote_db_pwd="$2"
145
				shift
146
				;;
147
			--bind-port)
148
				echo "Local binding port: $2"
149
				bind_port="$2"
150
				shift
151
				;;
152
			--help | -h)
153
				usage
154
				return 2
155
				;;
156
			--)
157
				# End of args
158
				break
159
				;;
160
			*)
161
				echo "error: unknown $1" >&2
162
				return 3
163
				break
164
				;;
165
		esac
166
		shift
167
	done
168
 
169
	# All fields must be filled
170
	case "$remote_role" in
171
		primary)
172
			# Needed args to be passed
173
			if [ -z "$remote_name"     ] ||
174
			   [ -z "$remote_addr"     ] ||
175
			   [ -z "$remote_ssh_port" ] ||
176
			   [ -z "$remote_ssh_user" ] ||
177
			   [ -z "$remote_db_user"  ] ||
178
			   [ -z "$remote_db_pwd"   ]
179
			then
180
				echo "error: some args are missing" >&2
181
				return 4
182
			fi
183
			;;
184
		secondary)
185
			# Needed args to be passed
186
			if [ -z "$remote_name"     ] ||
187
			   [ -z "$bind_port"       ] ||
188
			   [ -z "$remote_db_user"  ] ||
189
			   [ -z "$remote_db_pwd"   ]
190
			then
191
				echo "error: some args are missing" >&2
192
				return 5
193
			fi
194
			;;
195
		*)
196
			echo "error: remote role is missing" >&2
197
			return 6
198
			;;
199
	esac
200
}
201
 
3321 rexy 202
# Test connection to remote system and remote database before creating SSH tunnel.
3294 rexy 203
check_primary_credentials() {
204
	# Test SSH credentials
205
	if ! /usr/bin/ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" exit
206
	then
207
		echo "error: cannot SSH with '$remote_ssh_user' to $remote_addr:$remote_ssh_port" >&2
208
		echo "hint: have you deployed root pubkey on the remote?"
209
		return 7
210
	fi
3314 rexy 211
	echo "Successfully connected with '$remote_ssh_user' to primary ($remote_addr:$remote_ssh_port)"
3294 rexy 212
 
3321 rexy 213
	# Retrieve remote db_replication pwd
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
3319 rexy 215
	then
3321 rexy 216
		echo "error: cannot retrieve remote primary db_user pwd" >&2
3319 rexy 217
		return 7
218
	fi
3321 rexy 219
	remote_db_pwd=$(cat /tmp/primary-db_replication-pwd.txt)
220
	rm /tmp/primary-db_replication-pwd.txt
3319 rexy 221
	echo "Successfully retrieve remote primary db_user pass"
222
 
3294 rexy 223
	# Test database credentials
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"
225
	then
226
		echo "error: cannot connect with '$remote_db_user' to remote database" >&2
227
		return 8
228
	fi
229
	echo "Successfully connected with '$remote_db_user' to remote database"
230
}
231
 
232
# Test connection to remote database through SSH tunnel
233
check_secondary_credentials() {
234
	if ! /usr/bin/mariadb --host="$LOCALHOST" --port="$bind_port" --user="$remote_db_user" --password="$remote_db_pwd" --execute="QUIT"
235
	then
236
		echo "error: cannot connect with '$remote_db_user' to remote database" >&2
237
		return 9
238
	fi
3314 rexy 239
	echo "Successfully connected with '$remote_db_user' to remote secondary database on port $bind_port"
3294 rexy 240
}
241
 
3321 rexy 242
# Add a systemd unit to create SSH tunnel to remote primary
3294 rexy 243
create_ssh_tunnel() {
244
	# Find a common binding port
245
	find_common_free_port || return 11
246
	service_file="replication-$remote_name.service"
247
	service_path="/etc/systemd/system/$service_file"
248
 
249
	# Write down SSH tunnel service file
250
	echo "[Unit]
251
Description=Setup a secure bidirectional tunnel with $remote_name
252
After=network.target
253
 
254
[Service]
255
ExecStart=/usr/bin/ssh -NT -4 -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -p $remote_ssh_port -L $bind_port:localhost:$DB_PORT -R $bind_port:localhost:$DB_PORT $remote_ssh_user@$remote_addr
256
RestartSec=5
257
Restart=always
258
 
259
[Install]
260
WantedBy=multi-user.target
261
" > "$service_path"
262
 
263
	# Start and enable SSH tunnel
264
	echo "Enabling $remote_name service..."
265
	/usr/bin/systemctl enable "$service_file"
266
	echo "Starting $remote_name service..."
267
	/usr/bin/systemctl start "$service_file"
268
}
269
 
270
# Execute SQL query
271
# $1: query
272
# $2: user (default: root)
273
# $3: password (default: root pwd)
274
# $4: host (default: localhost)
275
# $5: port (default: 3306)
276
exec_query() {
277
	# Check args
278
	if [ $# -lt 1 ]
279
	then
280
		echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
281
		return 12
282
	fi
283
	# Execute the query
284
	/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
285
}
286
 
3321 rexy 287
# create, retrieve & import remote primary database
3319 rexy 288
retrieve_primary_database() {
3318 rexy 289
	# creation of a fresh dump
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"
291
	then
292
		echo "error: cannot create a fresh primary database dump" >&2
293
		return 18
294
	fi
295
	echo "Primary database dump created"
3319 rexy 296
	if ! scp -q -P "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr":alcasar-users-database-primary.sql.gz /tmp/
3318 rexy 297
	then
298
		echo "error: cannot retrieve localy the fresh primary database dump" >&2
299
		return 19
300
	fi
301
	echo "Primary database dump locally copied"
3319 rexy 302
	alcasar-mariadb.sh --import /tmp/alcasar-users-database-primary.sql.gz
3318 rexy 303
	rm -f /tmp/alcasar-users-database-primary.sql.gz
304
}
305
 
3321 rexy 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
 
3294 rexy 317
find_common_free_port() {
318
	remote_busy_ports_file=/tmp/remote_busy_ports
319
	local_busy_ports_file=/tmp/local_busy_ports
320
	common_busy_ports_file=/tmp/common_busy_ports
321
	ports_list_file=/tmp/ports_list
322
	free_ports_file=/tmp/free_ports
323
 
324
	# Get remote busy ports
325
	/usr/bin/ssh -q -p "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr" -- /usr/sbin/ss --listening --numeric --ipv4 | tail -n +2 | cut -d ':' -f 2 | cut -d ' ' -f 1 | sort -u > "$remote_busy_ports_file"
326
	if [ "$?" -ne 0 ]
327
	then
328
		echo "error: cannot SSH with '$remote_ssh_user' to $remote_addr:$remote_ssh_port" >&2
329
		return 13
330
	fi
331
 
332
	# Get local busy ports
333
	/usr/sbin/ss --listening --numeric --ipv4 | tail -n +2 | cut -d ':' -f 2 | cut -d ' ' -f 1 | sort -u > "$local_busy_ports_file"
334
 
335
	# List ports range from system
336
	read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
337
 
338
	# Write ports in a file
339
	echo -n > "$ports_list_file"
340
	for port in $(seq "$lower_port" "$upper_port")
341
	do
342
		echo "$port" >> "$ports_list_file"
343
	done
344
 
345
	# Merge busy ports
346
	/usr/bin/cat "$remote_busy_ports_file" "$local_busy_ports_file" > "$common_busy_ports_file"
347
	# Sorts ports
348
	/usr/bin/sort -u -o "$common_busy_ports_file" "$common_busy_ports_file"
349
	/usr/bin/sort -o "$ports_list_file" "$ports_list_file"
350
	# Substract available ports in common
351
	/usr/bin/comm --check-order -3 "$ports_list_file" "$common_busy_ports_file" | cut -f 1 | sed "/^$/d" > "$free_ports_file"
352
 
353
	# Verify at least one free port have been found
354
	if [ ! -s "$free_ports_file" ]
355
	then
356
		echo "error: no common port found for binding" >&2
357
		return 14
358
	fi
359
 
360
	# Pick the first common port
361
	bind_port="$(head -n 1 "$free_ports_file")"
362
	echo "Both machines binded on port $bind_port->$DB_PORT"
363
	echo "Please take note about the binding port for primary's connection setup."
364
 
365
	# Remove tmp files
366
	rm "$remote_busy_ports_file"
367
	rm "$local_busy_ports_file"
368
	rm "$common_busy_ports_file"
369
	rm "$ports_list_file"
370
	rm "$free_ports_file"
371
}
372
 
373
# Allow outbound connection for testing connection
374
tmp_allow_outbound_connection() {
375
	/usr/sbin/iptables -A OUTPUT -d "$remote_addr" -p tcp --dport "$remote_ssh_port" -j ACCEPT
376
}
377
 
3313 rexy 378
# Disable outbound connection which that was used to test test connection
3294 rexy 379
tmp_disable_outbound_connection() {
380
	/usr/sbin/iptables -D OUTPUT -d "$remote_addr" -p tcp --dport "$remote_ssh_port" -j ACCEPT
381
}
382
 
383
# Print help message
384
usage() {
385
	echo "usage: $0 ROLE OPTIONS"
386
	echo
387
	echo "ROLE"
388
	echo "	--to-primary"
389
	echo "		remote server is a primary"
390
	echo "	--to-secondary"
391
	echo "		remote server is a secondary"
392
	echo
393
	echo "OPTIONS"
394
	echo "	--name=NAME, -n NAME"
395
	echo "		friendly name given to the remote"
396
	echo "	--address=ADDRESS, -a ADDRESS"
397
	echo "		remote IP address"
398
	echo "	--port=PORT, -p PORT"
399
	echo "		remote SSH port"
400
	echo "	--user=USER, -u USER"
401
	echo "		remote SSH user"
402
	echo "	--db-user=USER"
403
	echo "		remote database replication user"
404
	echo "	--db-password=PASSWORD"
405
	echo "		remote database replication user password"
406
	echo "	--bind-port=PORT"
407
	echo "		used from primary: local port binded to remote database. It has been displayed during secondary connection to primary"
408
	echo "	--help, -h"
409
	echo "		print this help message"
410
	echo
411
	echo "ROLE OPTIONS"
412
	echo "	--to-primary: needs name, address, port, user, db-user, db-password"
413
	echo "	--to-secondary: needs name, bind-port, db-user, db-password"
414
}
415
 
416
# Main
417
check_args "$@" || exit
418
 
419
check_availability || exit
420
 
421
case "$remote_role" in
422
	primary)
423
		tmp_allow_outbound_connection || abort "$?" || exit
424
		check_primary_credentials || abort "$?" || exit
425
		create_ssh_tunnel || abort "$?" || exit
3319 rexy 426
		retrieve_primary_database || abort "$?" || exit
3314 rexy 427
		add_remote_as_primary || abort "$?" || exit
3321 rexy 428
		push_local_info_to_primary || abort "$?" || exit
3314 rexy 429
		echo -n "Allowing outbound connection to remote SSH "
430
		# Get remote IP and port from its name
431
		port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
432
		ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
433
		/usr/bin/sed -i -E "/^REPLICATION_TO=/s/=(.*)/=\1$ip:$port,/" /usr/local/etc/alcasar.conf
434
		/usr/local/bin/alcasar-iptables.sh
3294 rexy 435
		;;
436
	secondary)
437
		check_secondary_credentials || exit
3314 rexy 438
		add_remote_as_primary || abort "$?" || exit # In a federation, primary/secondary is define by SSH role (sshd-server=primary; ssh-client=secondary)
3294 rexy 439
		;;
440
esac
441
 
3313 rexy 442
# Set Netfilter
443
 
3314 rexy 444