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
##        delete       ##
6
#########################
7
# The script is designed to delete 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_delete=""
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 "Removing all connections."
41
				remote_to_delete="ALL"
42
				break
43
				;;
44
			--name | -n)
45
				echo "Removing connection to $2."
46
				remote_to_delete="$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
exec_query() {
71
	# Check args
72
	if [ $# -lt 1 ]
73
	then
74
		echo "usage: $0 \"SQL query\" <DB user> <DB password> <SQL server address> <SQL server port>"
75
		return 2
76
	fi
77
 
78
	# Execute the query
79
	/usr/bin/mariadb --user="root" --password="$DB_ROOT_PWD" --execute="$1"
80
}
81
 
82
# Delete replication with a remote
83
# $1: remote name to delete
84
delete_remote() {
85
	# Check args
86
	if [ $# -ne 1 ]
87
	then
88
		echo "error: need to pass remote name"
89
		return 1
90
	fi
91
	name="$1"
3318 rexy 92
	# Check for replication
93
	attributes="$(/usr/local/bin/alcasar-replication-list.sh --name=$name)"
94
	if ! echo "$attributes" | grep -q "Master_Host"; then
95
		echo "no repliqua to $name"
96
		return 1
97
	fi
3311 rexy 98
	echo "Stop slave in '$name'..."
99
	exec_query "STOP SLAVE '$name'" || return
3294 rexy 100
	echo "Resetting replication with '$name'..."
101
	exec_query "RESET REPLICA '$name' ALL" || return
3313 rexy 102
	service_file="replication-$name.service"
103
	service_path="/etc/systemd/system/$service_file"
3318 rexy 104
	if [ -f "$service_path" ]; then # for slaves only
105
		echo "Deleting SSH tunnel with '$name'..."
106
		systemctl stop "$service_file"
107
		systemctl disable "$service_file"
108
		port="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 9)"
109
		ip="$(grep "ExecStart" "$service_path" | cut -d ' ' -f 14 | cut -d '@' -f2)"
110
		rm "$service_path"
111
		echo "Disabling outbound connection to remote SSH ($ip:$port)"
112
		sed -i "/^REPLICATION_TO=/s/$ip:$port,//" /usr/local/etc/alcasar.conf
113
		/usr/local/bin/alcasar-iptables.sh
114
	fi
3294 rexy 115
}
116
 
117
# Print help message
118
usage() {
119
	echo "usage: $0 OPTIONS"
120
	echo
121
	echo "OPTIONS"
122
	echo "	--all, -a"
123
	echo "		delete all connections"
124
	echo "	--name=NAME, -n NAME"
125
	echo "		remote name connection to delete"
126
	echo "	--help, -h"
127
	echo "		print this help message"
128
}
129
 
130
# Main
131
check_args "$@" || exit
132
 
133
# Delete all or specific remote
134
if [ "$remote_to_delete" == "ALL" ]
135
then
136
	# Get remotes names from database
137
	remotes_names=$(/usr/local/bin/alcasar-replication-list.sh --all | grep "Connection_name" | cut -d ':' -f 2-)
138
 
139
	# Loop over names
140
	for name in $remotes_names
141
	do
142
		delete_remote "$name"
143
	done
144
else
145
	delete_remote "$remote_to_delete"
146
fi