summaryrefslogtreecommitdiff
path: root/mariadb-push-replica.sh
blob: d645c914aacc2087554b6f4658993b82bef5bd3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
set -ex

default_slave_remote_file=data/default-slave-remote
master_password_file=data/mysql-master-password

if [ -r "$default_slave_remote_file" ]
then
	read default_remote_host < "$default_slave_remote_file"
fi

gen_password()
{
	generated_password_length=32
	generated_password=$(tr -cd a-zA-Z0-9 < /dev/urandom | head -c "$generated_password_length")
	[ "${#generated_password}" -eq "$generated_password_length" ]
	printf '%s\n' "$generated_password"
}

if ! [ -e "$master_password_file" ]
then
	gen_password > "$master_password_file"
fi

if [ -r "$master_password_file" ]
then
	read master_password < "$master_password_file" 
	[ "${#master_password}" -ge 30 ]
fi

our_host=$(hostname --fqdn)
remote_host=${1:-$default_remote_host}

[ "$our_host" ]
[ "$remote_host" ]

master_password=$(false)

ssh -l root "$remote_host" -- mariadb <<END
CHANGE MASTER TO
  MASTER_HOST='$our_host',
  MASTER_USER='replication',
  MASTER_PASSWORD='$master_password'
END