diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-15 20:39:32 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-15 21:59:50 -0500 |
commit | 77fc2a3f346001d572b64d198a0ba7db46889e40 (patch) | |
tree | 52c7d6f3792b5224223268ca11a9d671faafc5fd | |
parent | 306f3977f5c0addc8fb0af231d54bf14bde6f639 (diff) |
worked!
-rwxr-xr-x | mariadb-push-replica.sh | 44 | ||||
-rw-r--r-- | src/rpc.bash | 1 | ||||
-rwxr-xr-x | wip/mariadb-push-replica.sh | 137 |
3 files changed, 108 insertions, 74 deletions
diff --git a/mariadb-push-replica.sh b/mariadb-push-replica.sh deleted file mode 100755 index d645c91..0000000 --- a/mariadb-push-replica.sh +++ /dev/null | |||
@@ -1,44 +0,0 @@ | |||
1 | #!/bin/bash | ||
2 | set -ex | ||
3 | |||
4 | default_slave_remote_file=data/default-slave-remote | ||
5 | master_password_file=data/mysql-master-password | ||
6 | |||
7 | if [ -r "$default_slave_remote_file" ] | ||
8 | then | ||
9 | read default_remote_host < "$default_slave_remote_file" | ||
10 | fi | ||
11 | |||
12 | gen_password() | ||
13 | { | ||
14 | generated_password_length=32 | ||
15 | generated_password=$(tr -cd a-zA-Z0-9 < /dev/urandom | head -c "$generated_password_length") | ||
16 | [ "${#generated_password}" -eq "$generated_password_length" ] | ||
17 | printf '%s\n' "$generated_password" | ||
18 | } | ||
19 | |||
20 | if ! [ -e "$master_password_file" ] | ||
21 | then | ||
22 | gen_password > "$master_password_file" | ||
23 | fi | ||
24 | |||
25 | if [ -r "$master_password_file" ] | ||
26 | then | ||
27 | read master_password < "$master_password_file" | ||
28 | [ "${#master_password}" -ge 30 ] | ||
29 | fi | ||
30 | |||
31 | our_host=$(hostname --fqdn) | ||
32 | remote_host=${1:-$default_remote_host} | ||
33 | |||
34 | [ "$our_host" ] | ||
35 | [ "$remote_host" ] | ||
36 | |||
37 | master_password=$(false) | ||
38 | |||
39 | ssh -l root "$remote_host" -- mariadb <<END | ||
40 | CHANGE MASTER TO | ||
41 | MASTER_HOST='$our_host', | ||
42 | MASTER_USER='replication', | ||
43 | MASTER_PASSWORD='$master_password' | ||
44 | END | ||
diff --git a/src/rpc.bash b/src/rpc.bash index 853120f..4908767 100644 --- a/src/rpc.bash +++ b/src/rpc.bash | |||
@@ -14,7 +14,6 @@ __bashrpc__remote_run_script() | |||
14 | local script="$1" | 14 | local script="$1" |
15 | shift | 15 | shift |
16 | BASH_RPC_SHELL_OPTIONS=(--norc --noprofile) | 16 | BASH_RPC_SHELL_OPTIONS=(--norc --noprofile) |
17 | set -x | ||
18 | # printf 'DEBUG:<%s>\n' "${BASH_RPC_SSH_OPTIONS[@]}" >&2 | 17 | # printf 'DEBUG:<%s>\n' "${BASH_RPC_SSH_OPTIONS[@]}" >&2 |
19 | exec ssh \ | 18 | exec ssh \ |
20 | "${BASH_RPC_SSH_OPTIONS[@]}" -- \ | 19 | "${BASH_RPC_SSH_OPTIONS[@]}" -- \ |
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 | |||