Subversion Repositories ALCASAR

Rev

Rev 3308 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
3294 rexy 1
#!/bin/bash
2
 
3
#########################
4
## ALCASAR replication ##
5
##       install       ##
6
#########################
7
# The script is designed to setup replication between ALCASAR instances.
8
 
9
# Constants
10
readonly PASSWD_FILE="/root/ALCASAR-passwords.txt"
11
readonly REPL_USER="replication"
3308 rexy 12
readonly REPL_DB_USER="db_replication"
3294 rexy 13
readonly REPL_PWD_LENGTH=16
14
readonly REPL_DB_PWD_LENGTH=16
15
readonly DB_CONF=/etc/my.cnf.d/server.cnf
16
readonly DB_REPL_CONF=/etc/my.cnf.d/replication.cnf
17
readonly EXPIRE_BINLOG_DAYS=31
18
readonly ALCASAR_CONF=/usr/local/etc/alcasar.conf
19
 
20
# Dynamically generated constants
21
DB_ROOT_PWD="$(grep db_root "$PASSWD_FILE" | cut -d '=' -f 2-)"
22
readonly DB_ROOT_PWD
23
REPL_PWD="$(tr -dc "a-zA-Z0-9" < /dev/random | head -c "$REPL_PWD_LENGTH")"
24
readonly REPL_PWD
25
REPL_DB_PWD="$(tr -dc "a-zA-Z0-9" < /dev/random | head -c "$REPL_DB_PWD_LENGTH")"
26
readonly REPL_DB_PWD
27
 
28
# Execute SQL queries on local server
29
exec_query() {
30
	if [ $# -ne 1 ]
31
	then
32
		echo "A SQL query must be given." >&2
33
		return 1
34
	fi
35
 
36
	# Execute the query
37
	/usr/bin/mariadb --user=root --password="$DB_ROOT_PWD" --execute="$1"
38
}
39
 
40
if grep -q "REPLICATION=on" "$ALCASAR_CONF"
41
then
42
	echo "error: replication is already installed" >&2
43
	exit 2
44
fi
45
 
46
# Save generated credentials for system user
47
echo "# Replication account for remote access" >> "$PASSWD_FILE"
48
echo "replication=$REPL_USER" >> "$PASSWD_FILE"
49
echo "replication_pwd=$REPL_PWD" >> "$PASSWD_FILE"
50
echo "Replication user: $REPL_USER"
51
echo "Replication password: $REPL_PWD"
52
 
53
# Save generated credentials for database user
54
echo "# Database replication account" >> "$PASSWD_FILE"
55
echo "db_replication=$REPL_DB_USER" >> "$PASSWD_FILE"
56
echo "db_replication_pwd=$REPL_DB_PWD" >> "$PASSWD_FILE"
57
echo "Database replication user: $REPL_DB_USER"
58
echo "Database replication password: $REPL_DB_PWD"
59
 
60
# Create local replication user
61
echo "Creating '$REPL_DB_USER' user on database..."
62
exec_query "CREATE USER '$REPL_DB_USER'@'%' IDENTIFIED BY '$REPL_DB_PWD'" || exit
63
echo "Granting '$REPL_DB_USER' user for replication..."
64
exec_query "GRANT REPLICATION REPLICA ON *.* TO '$REPL_DB_USER'@'%'" || exit
65
exec_query "FLUSH PRIVILEGES" || exit
66
 
67
# Remove forbidden characters for MariaDB
68
safe_hostname=$(echo "$HOSTNAME" | tr "[./]" '-')
69
 
70
# Enable binary logging
71
echo "Enabling binary logging..."
72
echo -n "[mariadb]
73
log-bin
74
report_host=$safe_hostname
75
log-basename=$safe_hostname
76
binlog-format=mixed
77
expire_logs_days=$EXPIRE_BINLOG_DAYS
78
master_retry_count=0
79
log_slave_updates=1
80
binlog-ignore-db=information_schema
81
binlog-ignore-db=mysql
82
binlog-ignore-db=performance_schema
83
" > "$DB_REPL_CONF"
84
 
85
# Listen on localhost
86
sed -i "s?^.*skip-networking.*?#skip-networking?" "$DB_CONF"
87
sed -i "s?^#bind-address.*?bind-address=127.0.0.1?" "$DB_CONF"
88
 
89
# Apply binary logging
90
echo "Restarting MariaDB..."
91
/usr/bin/systemctl restart mariadb.service
3309 rexy 92
echo "initializing binary logging"
93
exec_query "RESET MASTER" || exit
3294 rexy 94
 
95
# User for SSH tunneling
96
echo "Creating replication user..."
97
/usr/sbin/useradd -s /bin/sh -m "$REPL_USER"
98
echo -e "$REPL_PWD\n$REPL_PWD" | passwd "$REPL_USER"
99
mkdir "/home/$REPL_USER/.ssh"
100
touch "/home/$REPL_USER/.ssh/authorized_keys"
101
chown replication:replication -R "/home/$REPL_USER/.ssh"
102
 
103
# Generate user SSH key
104
if ! ls ~/.ssh/id_* &> /dev/null
105
then
106
	echo "Generating SSH key..."
107
	mkdir ~/.ssh
108
	/usr/bin/ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
109
fi
110
 
111
echo "Setting replication state to 'on'..."
112
sed -i "/^REPLICATION=/s/off/on/" "$ALCASAR_CONF"
113
 
114
echo "Database replication succesfully installed."