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
##         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
				echo "Stopping all connections."
41
				remote_to_stop="ALL"
42
				break
43
				;;
44
			--name | -n)
45
				echo "Stopping connection to $2."
46
				remote_to_stop="$2"
47
				shift
48
				break
49
				;;
50
			--help | -h)
51
				usage
52
				return 2
53
				;;
54
			--)
55
				# End of args
56
				break
57
				;;
58
			*)
59
				echo "error: unknown $1" >&2
60
				return 3
61
				break
62
				;;
63
		esac
64
		shift
65
	done
66
}
67
 
68
# Execute SQL query
69
# $1: query
70
# $2: user (default: root)
71
# $3: password (default: root pwd)
72
# $4: host (default: localhost)
73
# $5: port (default: 3306)
74
exec_query() {
75
	# Check args
76
	if [ $# -lt 1 ]
77
	then
78
		echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
79
		return 2
80
	fi
81
 
82
	# Execute the query
83
	/usr/bin/mariadb --user="root" --password="$DB_ROOT_PWD" --execute="$1"
84
}
85
 
86
# Stop all or specific replication with remotes
87
# $1: remote name to stop
88
stop_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
	echo "Stopping replication with '$name'..."
100
	exec_query "STOP REPLICA '$name'" || return
101
 
102
	# Skip if the connection is received (like on a primary)
103
	if [ -f "$service_file" ]
104
	then
105
		echo "Stopping SSH tunnel with '$name'..."
106
		/usr/bin/systemctl stop "replication-$name"
107
		/usr/bin/systemctl disable "replication-$name"
108
 
109
		# Get remote IP and port from its name
110
		ip="$(/usr/local/bin/alcasar-replication-list.sh --name=$name | grep "Master_Host" | cut -d ' ' -f 2-)"
111
		port="$(grep "ExecStart" "$service_file" | cut -d ' ' -f 9)"
112
 
113
		echo "Disabling outbound connection to remote SSH..."
114
		sed -i "/^REPLICATION_TO=/s/$ip:$port,//" /usr/local/etc/alcasar.conf
115
		/usr/local/bin/alcasar-iptables.sh
116
	fi
117
}
118
 
119
# Print help message
120
usage() {
121
	echo "usage: $0 OPTIONS"
122
	echo
123
	echo "OPTIONS"
124
	echo "	--all, -a"
125
	echo "		stop all connections"
126
	echo "	--name=NAME, -n NAME"
127
	echo "		remote name connection to stop"
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_stop" == "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
                stop_remote "$name"
145
        done
146
else
147
        stop_remote "$remote_to_stop"
148
fi