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"
|
|
|
12 |
readonly REPL_DB_USER="$REPL_USER"
|
|
|
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 |
# SRV_ID must be < 2^32, see server_id variable of MariaDB
|
|
|
28 |
SRV_ID="$(cat /dev/urandom | tr -dc "0-9" | head -c 9)"
|
|
|
29 |
readonly SRV_ID
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
# Execute SQL queries on local server
|
|
|
33 |
exec_query() {
|
|
|
34 |
if [ $# -ne 1 ]
|
|
|
35 |
then
|
|
|
36 |
echo "A SQL query must be given." >&2
|
|
|
37 |
return 1
|
|
|
38 |
fi
|
|
|
39 |
|
|
|
40 |
# Execute the query
|
|
|
41 |
/usr/bin/mariadb --user=root --password="$DB_ROOT_PWD" --execute="$1"
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
if grep -q "REPLICATION=on" "$ALCASAR_CONF"
|
|
|
45 |
then
|
|
|
46 |
echo "error: replication is already installed" >&2
|
|
|
47 |
exit 2
|
|
|
48 |
fi
|
|
|
49 |
|
|
|
50 |
# Save generated credentials for system user
|
|
|
51 |
echo "# Replication account for remote access" >> "$PASSWD_FILE"
|
|
|
52 |
echo "replication=$REPL_USER" >> "$PASSWD_FILE"
|
|
|
53 |
echo "replication_pwd=$REPL_PWD" >> "$PASSWD_FILE"
|
|
|
54 |
echo "Replication user: $REPL_USER"
|
|
|
55 |
echo "Replication password: $REPL_PWD"
|
|
|
56 |
|
|
|
57 |
# Save generated credentials for database user
|
|
|
58 |
echo "# Database replication account" >> "$PASSWD_FILE"
|
|
|
59 |
echo "db_replication=$REPL_DB_USER" >> "$PASSWD_FILE"
|
|
|
60 |
echo "db_replication_pwd=$REPL_DB_PWD" >> "$PASSWD_FILE"
|
|
|
61 |
echo "Database replication user: $REPL_DB_USER"
|
|
|
62 |
echo "Database replication password: $REPL_DB_PWD"
|
|
|
63 |
|
|
|
64 |
# Create local replication user
|
|
|
65 |
echo "Creating '$REPL_DB_USER' user on database..."
|
|
|
66 |
exec_query "CREATE USER '$REPL_DB_USER'@'%' IDENTIFIED BY '$REPL_DB_PWD'" || exit
|
|
|
67 |
echo "Granting '$REPL_DB_USER' user for replication..."
|
|
|
68 |
exec_query "GRANT REPLICATION REPLICA ON *.* TO '$REPL_DB_USER'@'%'" || exit
|
|
|
69 |
exec_query "FLUSH PRIVILEGES" || exit
|
|
|
70 |
|
|
|
71 |
# Remove forbidden characters for MariaDB
|
|
|
72 |
safe_hostname=$(echo "$HOSTNAME" | tr "[./]" '-')
|
|
|
73 |
|
|
|
74 |
# Enable binary logging
|
|
|
75 |
echo "Enabling binary logging..."
|
|
|
76 |
echo -n "[mariadb]
|
|
|
77 |
log-bin
|
|
|
78 |
server_id=$SRV_ID
|
|
|
79 |
report_host=$safe_hostname
|
|
|
80 |
log-basename=$safe_hostname
|
|
|
81 |
binlog-format=mixed
|
|
|
82 |
expire_logs_days=$EXPIRE_BINLOG_DAYS
|
|
|
83 |
master_retry_count=0
|
|
|
84 |
log_slave_updates=1
|
|
|
85 |
binlog-ignore-db=information_schema
|
|
|
86 |
binlog-ignore-db=mysql
|
|
|
87 |
binlog-ignore-db=performance_schema
|
|
|
88 |
" > "$DB_REPL_CONF"
|
|
|
89 |
|
|
|
90 |
# Listen on localhost
|
|
|
91 |
sed -i "s?^.*skip-networking.*?#skip-networking?" "$DB_CONF"
|
|
|
92 |
sed -i "s?^#bind-address.*?bind-address=127.0.0.1?" "$DB_CONF"
|
|
|
93 |
|
|
|
94 |
# Apply binary logging
|
|
|
95 |
echo "Restarting MariaDB..."
|
|
|
96 |
/usr/bin/systemctl restart mariadb.service
|
|
|
97 |
|
|
|
98 |
# User for SSH tunneling
|
|
|
99 |
echo "Creating replication user..."
|
|
|
100 |
/usr/sbin/useradd -s /bin/sh -m "$REPL_USER"
|
|
|
101 |
echo -e "$REPL_PWD\n$REPL_PWD" | passwd "$REPL_USER"
|
|
|
102 |
mkdir "/home/$REPL_USER/.ssh"
|
|
|
103 |
touch "/home/$REPL_USER/.ssh/authorized_keys"
|
|
|
104 |
chown replication:replication -R "/home/$REPL_USER/.ssh"
|
|
|
105 |
|
|
|
106 |
# Generate user SSH key
|
|
|
107 |
if ! ls ~/.ssh/id_* &> /dev/null
|
|
|
108 |
then
|
|
|
109 |
echo "Generating SSH key..."
|
|
|
110 |
mkdir ~/.ssh
|
|
|
111 |
/usr/bin/ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
|
|
|
112 |
fi
|
|
|
113 |
|
|
|
114 |
echo "Setting replication state to 'on'..."
|
|
|
115 |
sed -i "/^REPLICATION=/s/off/on/" "$ALCASAR_CONF"
|
|
|
116 |
|
|
|
117 |
echo "Database replication succesfully installed."
|