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
##         stop        ##
6
#########################
7
# The script is designed to stop 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_stop=""
18
 
19
# Check script args
20
# $@: script args
21
check_args() {
22
	# Parse args
23
	args="$(getopt --longoptions "all,name:,help" --options "a,n:,h" -- "$@")"
24
 
25
	# Reset script args list
26
	eval set -- "$args"
27
 
28
	# Print help
29
	if [ "$#" -eq 1 ]
30
	then
31
		usage
32
		return 1
33
	fi
34
 
35
	# Loop over all args
36
	while true
37
	do
38
		case "$1" in
39
			--all | -a)
40
				remote_to_stop="ALL"
41
				break
42
				;;
43
			--name | -n)
44
				remote_to_stop="$2"
45
				shift
46
				break
47
				;;
48
			--help | -h)
49
				usage
50
				return 2
51
				;;
52
			--)
53
				# End of args
54
				break
55
				;;
56
			*)
57
				echo "error: unknown $1" >&2
58
				return 3
59
				break
60
				;;
61
		esac
62
		shift
63
	done
64
}
65
 
66
# Execute SQL query
67
# $1: query
68
# $2: user (default: root)
69
# $3: password (default: root pwd)
70
# $4: host (default: localhost)
71
# $5: port (default: 3306)
72
exec_query() {
73
	# Check args
74
	if [ $# -lt 1 ]
75
	then
76
		echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
77
		return 2
78
	fi
79
	# Execute the query
80
	/usr/bin/mariadb --user="root" --password="$DB_ROOT_PWD" --execute="$1"
81
}
82
 
83
# Stop all or specific replication with remotes
84
# $1: remote name to stop
85
stop_remote() {
86
	# Check args
87
	if [ $# -ne 1 ]
88
	then
89
		echo "error: need to pass remote name"
90
		return 1
91
	fi
92
 
93
	name="$1"
3318 rexy 94
	attributes="$(/usr/local/bin/alcasar-replication-list.sh --name=$name)"
95
	if echo "$attributes" | grep -q "Master_Host";
3294 rexy 96
	then
3313 rexy 97
		echo "Stopping replication with '$name'"
98
		exec_query "STOP REPLICA '$name'" || return
99
	else
100
		echo "Remote $name doesn't exists !"
3294 rexy 101
	fi
102
}
103
 
104
# Print help message
105
usage() {
106
	echo "usage: $0 OPTIONS"
107
	echo
108
	echo "OPTIONS"
109
	echo "	--all, -a"
110
	echo "		stop all connections"
111
	echo "	--name=NAME, -n NAME"
112
	echo "		remote name connection to stop"
113
	echo "	--help, -h"
114
	echo "		print this help message"
115
}
116
 
117
# Main
118
check_args "$@" || exit
119
 
120
# Stop all or specific remote
121
if [ "$remote_to_stop" == "ALL" ]
122
then
123
        # Get remotes names from database
124
        remotes_names=$(/usr/local/bin/alcasar-replication-list.sh --all | grep "Connection_name" | cut -d ':' -f 2-)
125
        # Loop over names
126
        for name in $remotes_names
127
        do
128
                stop_remote "$name"
129
        done
130
else
131
        stop_remote "$remote_to_stop"
132
fi