Subversion Repositories ALCASAR

Rev

Rev 3313 | Details | Compare with Previous | 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
				remote_to_start="ALL"
42
				break
43
				;;
44
			--name | -n)
45
				remote_to_start="$2"
46
				shift
47
				break
48
				;;
49
			--help | -h)
50
				usage
51
				return 2
52
				;;
53
			--)
54
				# End of args
55
				break
56
				;;
57
			*)
58
				echo "error: unknown $1" >&2
59
				return 3
60
				break
61
				;;
62
		esac
63
		shift
64
	done
65
}
66
 
67
# Execute SQL query
68
# $1: query
69
# $2: user (default: root)
70
# $3: password (default: root pwd)
71
# $4: host (default: localhost)
72
# $5: port (default: 3306)
73
exec_query() {
74
	# Check args
75
	if [ $# -lt 1 ]
76
	then
77
		echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
78
		return 2
79
	fi
80
 
81
	# Execute the query
82
	/usr/bin/mariadb --user="root" --password="$DB_ROOT_PWD" --execute="$1"
83
}
84
 
85
# Start all or specific replication with remotes
86
start_remote() {
87
	# Check args
88
	if [ $# -ne 1 ]
89
	then
90
		echo "error: need to pass remote name"
91
		return 1
92
	fi
93
 
94
	name="$1"
3318 rexy 95
	attributes="$(/usr/local/bin/alcasar-replication-list.sh --name=$name)"
96
	if echo "$attributes" | grep -q "Master_Host";
3294 rexy 97
	then
3318 rexy 98
		echo "Starting replication with '$name'"
99
		exec_query "START REPLICA '$name'" || return
3313 rexy 100
	else
101
		echo "Remote $name doesn't exists !"
3294 rexy 102
	fi
103
}
104
 
105
# Print help message
106
usage() {
107
	echo "usage: $0 OPTIONS"
108
	echo
109
	echo "OPTIONS"
110
	echo "	--all, -a"
111
	echo "		start all connections"
112
	echo "	--name=NAME, -n NAME"
113
	echo "		remote name connection to start"
114
	echo "	--help, -h"
115
	echo "		print this help message"
116
}
117
 
118
# Main
119
check_args "$@" || exit
120
 
121
# Stop all or specific remote
122
if [ "$remote_to_start" == "ALL" ]
123
then
124
	# Get remotes names from database
125
	remotes_names=$(/usr/local/bin/alcasar-replication-list.sh --all | grep "Connection_name" | cut -d ':' -f 2-)
126
	# Loop over names
127
	for name in $remotes_names
128
	do
129
		start_remote "$name"
130
	done
131
else
132
	start_remote "$remote_to_start"
133
fi