Subversion Repositories ALCASAR

Rev

Rev 3318 | Rev 3321 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 3318 Rev 3319
Line 207... Line 207...
207
	then
207
	then
208
		echo "error: cannot SSH with '$remote_ssh_user' to $remote_addr:$remote_ssh_port" >&2
208
		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?"
209
		echo "hint: have you deployed root pubkey on the remote?"
210
		return 7
210
		return 7
211
	fi
211
	fi
212
 
-
 
213
	echo "Successfully connected with '$remote_ssh_user' to primary ($remote_addr:$remote_ssh_port)"
212
	echo "Successfully connected with '$remote_ssh_user' to primary ($remote_addr:$remote_ssh_port)"
214
 
213
 
-
 
214
	# exchange of db_replication pass
-
 
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
-
 
216
	then
-
 
217
		echo "error: cannot retrieve remote primary db_user pass" >&2
-
 
218
		return 7
-
 
219
	fi
-
 
220
	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
 
215
	# Test database credentials
232
	# Test database credentials
216
	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"
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"
217
	then
234
	then
218
		echo "error: cannot connect with '$remote_db_user' to remote database" >&2
235
		echo "error: cannot connect with '$remote_db_user' to remote database" >&2
219
		return 8
236
		return 8
220
	fi
237
	fi
221
 
-
 
222
	echo "Successfully connected with '$remote_db_user' to remote database"
238
	echo "Successfully connected with '$remote_db_user' to remote database"
223
}
239
}
224
 
240
 
225
# Test connection to remote database through SSH tunnel
241
# Test connection to remote database through SSH tunnel
226
check_secondary_credentials() {
242
check_secondary_credentials() {
Line 275... Line 291...
275
	fi
291
	fi
276
	# Execute the query
292
	# Execute the query
277
	/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
293
	/usr/bin/mariadb --host="${4:-localhost}" --port="${5:-$DB_PORT}" --user="${2:-root}" --password="${3:-$DB_ROOT_PWD}" --execute="$1"
278
}
294
}
279
 
295
 
280
retrieve_primary_data() {
296
retrieve_primary_database() {
281
	# creation of a fresh dump
297
	# 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"
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"
283
	then
299
	then
284
		echo "error: cannot create a fresh primary database dump" >&2
300
		echo "error: cannot create a fresh primary database dump" >&2
285
		return 18
301
		return 18
286
	fi
302
	fi
287
	echo "Primary database dump created"
303
	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
304
	if ! scp -q -P "$remote_ssh_port" "$remote_ssh_user"@"$remote_addr":alcasar-users-database-primary.sql.gz /tmp/
289
	then
305
	then
290
		echo "error: cannot retrieve localy the fresh primary database dump" >&2
306
		echo "error: cannot retrieve localy the fresh primary database dump" >&2
291
		return 19
307
		return 19
292
	fi
308
	fi
293
	echo "Primary database dump locally copied"
309
	echo "Primary database dump locally copied"
294
	readalcasar-mariadb.sh --import /tmp/alcasar-users-database-primary.sql.gz
310
	alcasar-mariadb.sh --import /tmp/alcasar-users-database-primary.sql.gz
295
	rm -f /tmp/alcasar-users-database-primary.sql.gz
311
	rm -f /tmp/alcasar-users-database-primary.sql.gz
296
}
312
}
297
 
313
 
298
find_common_free_port() {
314
find_common_free_port() {
299
	remote_busy_ports_file=/tmp/remote_busy_ports
315
	remote_busy_ports_file=/tmp/remote_busy_ports
Line 402... Line 418...
402
case "$remote_role" in
418
case "$remote_role" in
403
	primary)
419
	primary)
404
		tmp_allow_outbound_connection || abort "$?" || exit
420
		tmp_allow_outbound_connection || abort "$?" || exit
405
		check_primary_credentials || abort "$?" || exit
421
		check_primary_credentials || abort "$?" || exit
406
		create_ssh_tunnel || abort "$?" || exit
422
		create_ssh_tunnel || abort "$?" || exit
407
		retrieve_primary_data || abort "$?" || exit
423
		retrieve_primary_database || abort "$?" || exit
408
		add_remote_as_primary || abort "$?" || exit
424
		add_remote_as_primary || abort "$?" || exit
409
		echo -n "Allowing outbound connection to remote SSH "
425
		echo -n "Allowing outbound connection to remote SSH "
410
		# Get remote IP and port from its name
426
		# Get remote IP and port from its name
411
		port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
427
		port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
412
		ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
428
		ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"