diff options
Diffstat (limited to 'wip/mariadb-push-replica.sh')
-rwxr-xr-x | wip/mariadb-push-replica.sh | 137 |
1 files changed, 108 insertions, 29 deletions
diff --git a/wip/mariadb-push-replica.sh b/wip/mariadb-push-replica.sh index fbf9402..dcbabc1 100755 --- a/wip/mariadb-push-replica.sh +++ b/wip/mariadb-push-replica.sh | |||
@@ -1,5 +1,7 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | set -ex | 2 | set -e |
3 | PATH=./src:$PATH | ||
4 | source rpc.bash | ||
3 | 5 | ||
4 | default_slave_remote_file=data/default-slave-remote | 6 | default_slave_remote_file=data/default-slave-remote |
5 | master_password_file=data/mysql-master-password | 7 | master_password_file=data/mysql-master-password |
@@ -31,50 +33,127 @@ fi | |||
31 | our_host=$(hostname --fqdn) | 33 | our_host=$(hostname --fqdn) |
32 | remote_host=${1:-$default_remote_host} | 34 | remote_host=${1:-$default_remote_host} |
33 | 35 | ||
34 | rpc_funcs= | ||
35 | |||
36 | rpc() | ||
37 | { | ||
38 | read shellscript < <(declare -f $rpc_funcs; echo $*) | ||
39 | set -- $shellscript | ||
40 | ssh -l "$remote_user" -- "$remote_host" ${*@Q} | ||
41 | } | ||
42 | |||
43 | [ "$our_host" ] | 36 | [ "$our_host" ] |
44 | [ "$remote_host" ] | 37 | [ "$remote_host" ] |
45 | [ "$master_password" ] | 38 | [ "$master_password" ] |
46 | 39 | ||
47 | primarydb_create_user() | 40 | primarydb_host=$our_host |
41 | replicadb_host=$remote_host | ||
42 | |||
43 | replication_user=replication | ||
44 | replication_password=$master_password | ||
45 | |||
46 | run_primary() | ||
48 | { | 47 | { |
49 | : | 48 | BASH_RPC_REMOTE_DEST=$primarydb_host remote_run_function "$@" |
50 | } | 49 | } |
51 | 50 | ||
52 | mariabackup_target_dir=/var/mariadb/backup | 51 | run_replica() |
53 | primarydb_export_backup() | ||
54 | { | 52 | { |
55 | mkdir -p "$mariabackup_target_dir" | 53 | BASH_RPC_REMOTE_DEST=$replicadb_host remote_run_function "$@" |
56 | mariabackup --backup --target-dir="$mariabackup_target_dir" | ||
57 | } | 54 | } |
58 | 55 | ||
59 | primarydb() | 56 | primarydb() |
60 | { | 57 | { |
61 | ssh -l root "$remote_host" -- "$@" | 58 | run_primary mariadb -t |
59 | } | ||
60 | |||
61 | replicadb() | ||
62 | { | ||
63 | run_replica mariadb -t | ||
62 | } | 64 | } |
63 | remote_user=root | ||
64 | rpc uptime | ||
65 | exit 1 | ||
66 | 65 | ||
67 | ssh -l root "$remote_host" -- mariadb -t <<END | 66 | syscheck() |
67 | { | ||
68 | printf \ | ||
69 | "==> %s %s <==\n %s\n" \ | ||
70 | "$(hostname -A)" \ | ||
71 | "$(hostname -I)" \ | ||
72 | "$(uptime)" | ||
73 | } | ||
74 | |||
75 | dbcheck() | ||
76 | { | ||
77 | syscheck | ||
78 | mariadb -t <<END | sed 's/^/ /' | ||
68 | select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin; | 79 | select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin; |
69 | SELECT VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE FROM INFORMATION_SCHEMA.SYSTEM_VARIABLES WHERE VARIABLE_NAME='sql_log_bin' OR VARIABLE_NAME='HOSTNAME' \G | ||
70 | SELECT VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE FROM INFORMATION_SCHEMA.SYSTEM_VARIABLES \G | ||
71 | END | 80 | END |
81 | } | ||
82 | |||
83 | create_backup() | ||
84 | { | ||
85 | mariabackup_target_dir=/var/mariadb/backup | ||
86 | mkdir -p "$mariabackup_target_dir" | ||
87 | mariabackup -u root --backup --target-dir="$mariabackup_target_dir" | ||
88 | mariabackup -u root --prepare --target-dir="$mariabackup_target_dir" | ||
89 | } | ||
90 | |||
91 | send_backup() | ||
92 | { | ||
93 | mariabackup_target_dir=/var/mariadb/backup | ||
94 | rsync -zRaP -- /./"$mariabackup_target_dir" "$1":/ | ||
95 | } | ||
96 | |||
97 | enable_replication() | ||
98 | { | ||
99 | our_host=$1 | ||
100 | replication_user=$2 | ||
101 | replication_password=$3 | ||
102 | mariabackup_target_dir=/var/mariadb/backup | ||
103 | binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info | ||
104 | read master_log_file master_log_pos gtid_slave_pos < $binlog_info_file || return | ||
105 | mariadb -t <<END | ||
106 | SET GLOBAL gtid_slave_pos = "$gtid_slave_pos"; | ||
107 | CHANGE MASTER TO | ||
108 | MASTER_HOST='$our_host', | ||
109 | MASTER_USER='$replication_user', | ||
110 | MASTER_PASSWORD='$replication_password', | ||
111 | MASTER_USE_GTID=slave_pos; | ||
112 | START SLAVE; | ||
113 | END | ||
114 | } | ||
72 | 115 | ||
73 | exit 0 | 116 | create_replication_user() |
117 | { | ||
118 | mariadb -t <<END | ||
119 | CREATE OR REPLACE USER '$1' IDENTIFIED BY '$2'; | ||
120 | GRANT REPLICATION SLAVE ON *.* TO '$1'; | ||
121 | END | ||
122 | } | ||
74 | 123 | ||
75 | ssh -l root "$remote_host" -- mariadb <<END | 124 | showvars() |
76 | CHANGE MASTER TO | 125 | { |
77 | MASTER_HOST='$our_host', | 126 | for db in primarydb replicadb |
78 | MASTER_USER='replication', | 127 | do |
79 | MASTER_PASSWORD='$master_password' | 128 | $db <<END |
129 | select VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE | ||
130 | from INFORMATION_SCHEMA.SYSTEM_VARIABLES | ||
131 | where VARIABLE_NAME='sql_log_bin' or VARIABLE_NAME='HOSTNAME' \G | ||
80 | END | 132 | END |
133 | done | ||
134 | } | ||
135 | |||
136 | checkboth() | ||
137 | { | ||
138 | for h in primary replica | ||
139 | do | ||
140 | run_$h dbcheck | ||
141 | done | ||
142 | } | ||
143 | |||
144 | if false | ||
145 | then | ||
146 | checkboth | ||
147 | showvars | ||
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 | ||
157 | run_primary send_backup "$replicadb_host" | ||
158 | run_replica enable_replication "$our_host" "$replication_user" "$replication_password" | ||
159 | |||