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
##        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
 
92
	name="$1"
93
 
94
	echo "Resetting replication with '$name'..."
95
	exec_query "RESET REPLICA '$name' ALL" || return
96
 
97
	echo "Deleting SSH tunnel with '$name'..."
98
	service_file="/etc/systemd/system/replication-$name.service"
99
	[ -f "$service_file" ] && rm "$service_file"
100
}
101
 
102
# Print help message
103
usage() {
104
	echo "usage: $0 OPTIONS"
105
	echo
106
	echo "OPTIONS"
107
	echo "	--all, -a"
108
	echo "		delete all connections"
109
	echo "	--name=NAME, -n NAME"
110
	echo "		remote name connection to delete"
111
	echo "	--help, -h"
112
	echo "		print this help message"
113
}
114
 
115
# Main
116
check_args "$@" || exit
117
 
118
# Delete all or specific remote
119
if [ "$remote_to_delete" == "ALL" ]
120
then
121
	# Get remotes names from database
122
	remotes_names=$(/usr/local/bin/alcasar-replication-list.sh --all | grep "Connection_name" | cut -d ':' -f 2-)
123
 
124
	# Loop over names
125
	for name in $remotes_names
126
	do
127
		delete_remote "$name"
128
	done
129
else
130
	delete_remote "$remote_to_delete"
131
fi