summaryrefslogtreecommitdiff
path: root/wip/mariadb-push-replica.sh
blob: dcbabc18935e5f69ab5c2122ebae9caebe7bdfde (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
set -e
PATH=./src:$PATH
source rpc.bash

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" ]

primarydb_host=$our_host
replicadb_host=$remote_host

replication_user=replication
replication_password=$master_password

run_primary()
{
	BASH_RPC_REMOTE_DEST=$primarydb_host remote_run_function "$@"
}

run_replica()
{
	BASH_RPC_REMOTE_DEST=$replicadb_host remote_run_function "$@"
}

primarydb()
{
	run_primary mariadb -t
}

replicadb()
{
	run_replica mariadb -t
}

syscheck()
{
	printf \
		"==> %s %s <==\n    %s\n" \
		"$(hostname -A)" \
		"$(hostname -I)" \
		"$(uptime)"
}

dbcheck()
{
	syscheck
	mariadb -t <<END | sed 's/^/   /'
select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin;
END
}

create_backup()
{
	mariabackup_target_dir=/var/mariadb/backup
	mkdir -p "$mariabackup_target_dir"
	mariabackup -u root --backup  --target-dir="$mariabackup_target_dir"
	mariabackup -u root --prepare --target-dir="$mariabackup_target_dir"
}

send_backup()
{
	mariabackup_target_dir=/var/mariadb/backup
	rsync -zRaP -- /./"$mariabackup_target_dir" "$1":/
}

enable_replication()
{
	our_host=$1
	replication_user=$2
	replication_password=$3
	mariabackup_target_dir=/var/mariadb/backup
	binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info 
	read  master_log_file master_log_pos gtid_slave_pos < $binlog_info_file || return
	mariadb -t <<END
	SET GLOBAL gtid_slave_pos = "$gtid_slave_pos";
	CHANGE MASTER TO
	  MASTER_HOST='$our_host',
	  MASTER_USER='$replication_user',
	  MASTER_PASSWORD='$replication_password',
	  MASTER_USE_GTID=slave_pos;
	START SLAVE;
END
}

create_replication_user()
{
	mariadb -t <<END
	CREATE OR REPLACE USER '$1' IDENTIFIED BY '$2';
	GRANT REPLICATION SLAVE ON *.*  TO '$1';
END
}

showvars()
{
	for db in primarydb replicadb
	do
		$db <<END
			select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE 
			from INFORMATION_SCHEMA.SYSTEM_VARIABLES
			where VARIABLE_NAME='sql_log_bin' or VARIABLE_NAME='HOSTNAME' \G
END
	done
}

checkboth()
{
	for h in primary replica
	do
		run_$h dbcheck
	done
}

if false
then
	checkboth
	showvars
fi
mariabackup_target_dir=/var/mariadb/backup
if [ ! -d "$mariabackup_target_dir" ]
then
	run_primary create_replication_user \
		"$replication_user'@'$replicadb_host" \
		"$replication_password"
	run_primary create_backup "$replicadb_host"
fi
run_primary send_backup "$replicadb_host"
run_replica enable_replication "$our_host" "$replication_user" "$replication_password"