diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-16 13:44:07 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-16 13:44:07 -0500 |
commit | 6c71a8a4ed771d8430e1747a6274312fb812498d (patch) | |
tree | eb3b993506af908de7376ac2fd6219e65d40ee66 | |
parent | 77fc2a3f346001d572b64d198a0ba7db46889e40 (diff) |
make maria db replication idempotent
also use primary/replica terminology
-rwxr-xr-x | wip/mariadb-push-replica.sh | 141 |
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 | |||
3 | PATH=./src:$PATH | 3 | PATH=./src:$PATH |
4 | source rpc.bash | 4 | source rpc.bash |
5 | 5 | ||
6 | default_slave_remote_file=data/default-slave-remote | 6 | default_replica_host_file=data/default-replica-host |
7 | master_password_file=data/mysql-master-password | 7 | replication_password_file=data/replication-password |
8 | 8 | ||
9 | if [ -r "$default_slave_remote_file" ] | 9 | if [ -r "$default_replica_host_file" ] |
10 | then | 10 | then |
11 | read default_remote_host < "$default_slave_remote_file" | 11 | read default_replica_host < "$default_replica_host_file" |
12 | fi | 12 | fi |
13 | 13 | ||
14 | gen_password() | 14 | gen_password() |
@@ -19,51 +19,40 @@ gen_password() | |||
19 | printf '%s\n' "$generated_password" | 19 | printf '%s\n' "$generated_password" |
20 | } | 20 | } |
21 | 21 | ||
22 | if ! [ -e "$master_password_file" ] | 22 | if ! [ -e "$replication_password_file" ] |
23 | then | 23 | then |
24 | gen_password > "$master_password_file" | 24 | gen_password > "$replication_password_file" |
25 | fi | 25 | fi |
26 | 26 | ||
27 | if [ -r "$master_password_file" ] | 27 | if [ -r "$replication_password_file" ] |
28 | then | 28 | then |
29 | read master_password < "$master_password_file" | 29 | read replication_password < "$replication_password_file" |
30 | [ "${#master_password}" -ge 30 ] | 30 | [ "${#replication_password}" -ge 30 ] |
31 | fi | 31 | fi |
32 | 32 | ||
33 | our_host=$(hostname --fqdn) | 33 | primary_host=$(hostname --fqdn) |
34 | remote_host=${1:-$default_remote_host} | 34 | replica_host=${1:-$default_replica_host} |
35 | |||
36 | [ "$our_host" ] | ||
37 | [ "$remote_host" ] | ||
38 | [ "$master_password" ] | ||
39 | |||
40 | primarydb_host=$our_host | ||
41 | replicadb_host=$remote_host | ||
42 | |||
43 | replication_user=replication | 35 | replication_user=replication |
44 | replication_password=$master_password | 36 | |
37 | [ "$primary_host" ] | ||
38 | [ "$replica_host" ] | ||
39 | [ "$replication_password" ] | ||
45 | 40 | ||
46 | run_primary() | 41 | run_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 | ||
51 | run_replica() | 48 | run_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 "$@" | |
56 | primarydb() | ||
57 | { | ||
58 | run_primary mariadb -t | ||
59 | } | ||
60 | |||
61 | replicadb() | ||
62 | { | ||
63 | run_replica mariadb -t | ||
64 | } | 53 | } |
65 | 54 | ||
66 | syscheck() | 55 | show_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 | ||
75 | dbcheck() | 64 | check_db() |
76 | { | 65 | { |
77 | syscheck | 66 | show_hostnames |
78 | mariadb -t <<END | sed 's/^/ /' | 67 | mariadb -t <<END | sed 's/^/ /' |
79 | select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin; | 68 | select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin; |
80 | END | 69 | END |
@@ -82,78 +71,80 @@ END | |||
82 | 71 | ||
83 | create_backup() | 72 | create_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 | ||
91 | send_backup() | 85 | send_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 | ||
97 | enable_replication() | 91 | enable_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"; | 102 | STOP SLAVE; |
107 | CHANGE MASTER TO | 103 | SET GLOBAL gtid_slave_pos = "$gtid_slave_pos"; |
108 | MASTER_HOST='$our_host', | 104 | CHANGE 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; |
109 | START SLAVE; | ||
113 | END | 110 | END |
111 | rm -r /var/mariadb/backup | ||
114 | } | 112 | } |
115 | 113 | ||
116 | create_replication_user() | 114 | create_replication_user() |
117 | { | 115 | { |
118 | mariadb -t <<END | 116 | mariadb -t <<END |
119 | CREATE OR REPLACE USER '$1' IDENTIFIED BY '$2'; | 117 | CREATE OR REPLACE USER '$2'@'$1' IDENTIFIED BY '$3'; |
120 | GRANT REPLICATION SLAVE ON *.* TO '$1'; | 118 | GRANT REPLICATION SLAVE ON *.* TO '$2'@'$1'; |
121 | END | 119 | END |
122 | } | 120 | } |
123 | 121 | ||
124 | showvars() | 122 | showvars() |
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 | 127 | select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE |
130 | from INFORMATION_SCHEMA.SYSTEM_VARIABLES | 128 | from INFORMATION_SCHEMA.SYSTEM_VARIABLES |
131 | where VARIABLE_NAME='sql_log_bin' or VARIABLE_NAME='HOSTNAME' \G | 129 | where VARIABLE_NAME LIKE '%SLAVE%' \G |
132 | END | 130 | END |
133 | done | 131 | done |
134 | } | 132 | } |
135 | 133 | ||
136 | checkboth() | 134 | run_primary check_db |
137 | { | 135 | run_replica check_db |
138 | for h in primary replica | ||
139 | do | ||
140 | run_$h dbcheck | ||
141 | done | ||
142 | } | ||
143 | |||
144 | if false | 136 | if false |
145 | then | 137 | then |
146 | checkboth | 138 | showvars replica |
147 | showvars | 139 | exit |
148 | fi | ||
149 | mariabackup_target_dir=/var/mariadb/backup | ||
150 | if [ ! -d "$mariabackup_target_dir" ] | ||
151 | then | ||
152 | run_primary create_replication_user \ | ||
153 | "$replication_user'@'$replicadb_host" \ | ||
154 | "$replication_password" | ||
155 | run_primary create_backup "$replicadb_host" | ||
156 | fi | 140 | fi |
157 | run_primary send_backup "$replicadb_host" | 141 | run_primary create_replication_user \ |
158 | run_replica enable_replication "$our_host" "$replication_user" "$replication_password" | 142 | "$replica_host" \ |
159 | 143 | "$replication_user" \ | |
144 | "$replication_password" | ||
145 | run_primary create_backup | ||
146 | |||
147 | run_primary send_backup "$replica_host" | ||
148 | run_replica enable_replication "$replica_host" "$replication_user" "$replication_password" | ||
149 | set -x | ||
150 | rm -r /var/mariadb/backup | ||