summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@vps-18a7a2b7.vps.ovh.ca>2024-01-16 13:44:07 -0500
committerroot <root@vps-18a7a2b7.vps.ovh.ca>2024-01-16 13:44:07 -0500
commit6c71a8a4ed771d8430e1747a6274312fb812498d (patch)
treeeb3b993506af908de7376ac2fd6219e65d40ee66
parent77fc2a3f346001d572b64d198a0ba7db46889e40 (diff)
make maria db replication idempotent
also use primary/replica terminology
-rwxr-xr-xwip/mariadb-push-replica.sh141
1 files changed, 66 insertions, 75 deletions
diff --git a/wip/mariadb-push-replica.sh b/wip/mariadb-push-replica.sh
index dcbabc1..495873e 100755
--- a/wip/mariadb-push-replica.sh
+++ b/wip/mariadb-push-replica.sh
@@ -3,12 +3,12 @@ set -e
3PATH=./src:$PATH 3PATH=./src:$PATH
4source rpc.bash 4source rpc.bash
5 5
6default_slave_remote_file=data/default-slave-remote 6default_replica_host_file=data/default-replica-host
7master_password_file=data/mysql-master-password 7replication_password_file=data/replication-password
8 8
9if [ -r "$default_slave_remote_file" ] 9if [ -r "$default_replica_host_file" ]
10then 10then
11 read default_remote_host < "$default_slave_remote_file" 11 read default_replica_host < "$default_replica_host_file"
12fi 12fi
13 13
14gen_password() 14gen_password()
@@ -19,51 +19,40 @@ gen_password()
19 printf '%s\n' "$generated_password" 19 printf '%s\n' "$generated_password"
20} 20}
21 21
22if ! [ -e "$master_password_file" ] 22if ! [ -e "$replication_password_file" ]
23then 23then
24 gen_password > "$master_password_file" 24 gen_password > "$replication_password_file"
25fi 25fi
26 26
27if [ -r "$master_password_file" ] 27if [ -r "$replication_password_file" ]
28then 28then
29 read master_password < "$master_password_file" 29 read replication_password < "$replication_password_file"
30 [ "${#master_password}" -ge 30 ] 30 [ "${#replication_password}" -ge 30 ]
31fi 31fi
32 32
33our_host=$(hostname --fqdn) 33primary_host=$(hostname --fqdn)
34remote_host=${1:-$default_remote_host} 34replica_host=${1:-$default_replica_host}
35
36[ "$our_host" ]
37[ "$remote_host" ]
38[ "$master_password" ]
39
40primarydb_host=$our_host
41replicadb_host=$remote_host
42
43replication_user=replication 35replication_user=replication
44replication_password=$master_password 36
37[ "$primary_host" ]
38[ "$replica_host" ]
39[ "$replication_password" ]
45 40
46run_primary() 41run_primary()
47{ 42{
48 BASH_RPC_REMOTE_DEST=$primarydb_host remote_run_function "$@" 43 (set -x
44 : primary : $1 $2 ${3:+ ...})
45 BASH_RPC_REMOTE_DEST=$primary_host remote_run_function "$@"
49} 46}
50 47
51run_replica() 48run_replica()
52{ 49{
53 BASH_RPC_REMOTE_DEST=$replicadb_host remote_run_function "$@" 50 (set -x
54} 51 : replica : $1 $2 ${3:+ ...})
55 52 BASH_RPC_REMOTE_DEST=$replica_host remote_run_function "$@"
56primarydb()
57{
58 run_primary mariadb -t
59}
60
61replicadb()
62{
63 run_replica mariadb -t
64} 53}
65 54
66syscheck() 55show_hostnames()
67{ 56{
68 printf \ 57 printf \
69 "==> %s %s <==\n %s\n" \ 58 "==> %s %s <==\n %s\n" \
@@ -72,9 +61,9 @@ syscheck()
72 "$(uptime)" 61 "$(uptime)"
73} 62}
74 63
75dbcheck() 64check_db()
76{ 65{
77 syscheck 66 show_hostnames
78 mariadb -t <<END | sed 's/^/ /' 67 mariadb -t <<END | sed 's/^/ /'
79select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin; 68select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin;
80END 69END
@@ -82,78 +71,80 @@ END
82 71
83create_backup() 72create_backup()
84{ 73{
74 set -e
85 mariabackup_target_dir=/var/mariadb/backup 75 mariabackup_target_dir=/var/mariadb/backup
76 binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info
77 [ -e "$binlog_info_file" ] && return
86 mkdir -p "$mariabackup_target_dir" 78 mkdir -p "$mariabackup_target_dir"
87 mariabackup -u root --backup --target-dir="$mariabackup_target_dir" 79 set -- -u root --target-dir="$mariabackup_target_dir"
88 mariabackup -u root --prepare --target-dir="$mariabackup_target_dir" 80 mariabackup --backup --rsync "$@"
81 mariabackup --prepare "$@"
82 [ -e "$binlog_info_file" ]
89} 83}
90 84
91send_backup() 85send_backup()
92{ 86{
93 mariabackup_target_dir=/var/mariadb/backup 87 mariabackup_target_dir=/var/mariadb/backup
94 rsync -zRaP -- /./"$mariabackup_target_dir" "$1":/ 88 rsync -zaR -- /./"$mariabackup_target_dir" "$1":/
95} 89}
96 90
97enable_replication() 91enable_replication()
98{ 92{
99 our_host=$1 93 replica_host=$1
100 replication_user=$2 94 replication_user=$2
101 replication_password=$3 95 replication_password=$3
102 mariabackup_target_dir=/var/mariadb/backup 96 mariabackup_target_dir=/var/mariadb/backup
103 binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info 97 binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info
104 read master_log_file master_log_pos gtid_slave_pos < $binlog_info_file || return 98 set -x
99 [ -e "$binlog_info_file" ] || return 0
100 read master_log_file master_log_pos gtid_slave_pos < $binlog_info_file
105 mariadb -t <<END 101 mariadb -t <<END
106 SET GLOBAL gtid_slave_pos = "$gtid_slave_pos"; 102STOP SLAVE;
107 CHANGE MASTER TO 103SET GLOBAL gtid_slave_pos = "$gtid_slave_pos";
108 MASTER_HOST='$our_host', 104CHANGE MASTER TO
109 MASTER_USER='$replication_user', 105 MASTER_HOST='$replica_host',
110 MASTER_PASSWORD='$replication_password', 106 MASTER_USER='$replication_user',
111 MASTER_USE_GTID=slave_pos; 107 MASTER_PASSWORD='$replication_password',
112 START SLAVE; 108 MASTER_USE_GTID=slave_pos;
109START SLAVE;
113END 110END
111 rm -r /var/mariadb/backup
114} 112}
115 113
116create_replication_user() 114create_replication_user()
117{ 115{
118 mariadb -t <<END 116 mariadb -t <<END
119 CREATE OR REPLACE USER '$1' IDENTIFIED BY '$2'; 117CREATE OR REPLACE USER '$2'@'$1' IDENTIFIED BY '$3';
120 GRANT REPLICATION SLAVE ON *.* TO '$1'; 118GRANT REPLICATION SLAVE ON *.* TO '$2'@'$1';
121END 119END
122} 120}
123 121
124showvars() 122showvars()
125{ 123{
126 for db in primarydb replicadb 124 for h in "$@"
127 do 125 do
128 $db <<END 126 run_$h mariadb -t <<END
129 select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE 127select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE
130 from INFORMATION_SCHEMA.SYSTEM_VARIABLES 128from INFORMATION_SCHEMA.SYSTEM_VARIABLES
131 where VARIABLE_NAME='sql_log_bin' or VARIABLE_NAME='HOSTNAME' \G 129where VARIABLE_NAME LIKE '%SLAVE%' \G
132END 130END
133 done 131 done
134} 132}
135 133
136checkboth() 134run_primary check_db
137{ 135run_replica check_db
138 for h in primary replica
139 do
140 run_$h dbcheck
141 done
142}
143
144if false 136if false
145then 137then
146 checkboth 138 showvars replica
147 showvars 139 exit
148fi
149mariabackup_target_dir=/var/mariadb/backup
150if [ ! -d "$mariabackup_target_dir" ]
151then
152 run_primary create_replication_user \
153 "$replication_user'@'$replicadb_host" \
154 "$replication_password"
155 run_primary create_backup "$replicadb_host"
156fi 140fi
157run_primary send_backup "$replicadb_host" 141run_primary create_replication_user \
158run_replica enable_replication "$our_host" "$replication_user" "$replication_password" 142 "$replica_host" \
159 143 "$replication_user" \
144 "$replication_password"
145run_primary create_backup
146
147run_primary send_backup "$replica_host"
148run_replica enable_replication "$replica_host" "$replication_user" "$replication_password"
149set -x
150rm -r /var/mariadb/backup