summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@vps-18a7a2b7.vps.ovh.ca>2024-01-15 20:39:32 -0500
committerroot <root@vps-18a7a2b7.vps.ovh.ca>2024-01-15 21:59:50 -0500
commit77fc2a3f346001d572b64d198a0ba7db46889e40 (patch)
tree52c7d6f3792b5224223268ca11a9d671faafc5fd
parent306f3977f5c0addc8fb0af231d54bf14bde6f639 (diff)
worked!
-rwxr-xr-xmariadb-push-replica.sh44
-rw-r--r--src/rpc.bash1
-rwxr-xr-xwip/mariadb-push-replica.sh137
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
2set -ex
3
4default_slave_remote_file=data/default-slave-remote
5master_password_file=data/mysql-master-password
6
7if [ -r "$default_slave_remote_file" ]
8then
9 read default_remote_host < "$default_slave_remote_file"
10fi
11
12gen_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
20if ! [ -e "$master_password_file" ]
21then
22 gen_password > "$master_password_file"
23fi
24
25if [ -r "$master_password_file" ]
26then
27 read master_password < "$master_password_file"
28 [ "${#master_password}" -ge 30 ]
29fi
30
31our_host=$(hostname --fqdn)
32remote_host=${1:-$default_remote_host}
33
34[ "$our_host" ]
35[ "$remote_host" ]
36
37master_password=$(false)
38
39ssh -l root "$remote_host" -- mariadb <<END
40CHANGE MASTER TO
41 MASTER_HOST='$our_host',
42 MASTER_USER='replication',
43 MASTER_PASSWORD='$master_password'
44END
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
2set -ex 2set -e
3PATH=./src:$PATH
4source rpc.bash
3 5
4default_slave_remote_file=data/default-slave-remote 6default_slave_remote_file=data/default-slave-remote
5master_password_file=data/mysql-master-password 7master_password_file=data/mysql-master-password
@@ -31,50 +33,127 @@ fi
31our_host=$(hostname --fqdn) 33our_host=$(hostname --fqdn)
32remote_host=${1:-$default_remote_host} 34remote_host=${1:-$default_remote_host}
33 35
34rpc_funcs=
35
36rpc()
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
47primarydb_create_user() 40primarydb_host=$our_host
41replicadb_host=$remote_host
42
43replication_user=replication
44replication_password=$master_password
45
46run_primary()
48{ 47{
49 : 48 BASH_RPC_REMOTE_DEST=$primarydb_host remote_run_function "$@"
50} 49}
51 50
52mariabackup_target_dir=/var/mariadb/backup 51run_replica()
53primarydb_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
59primarydb() 56primarydb()
60{ 57{
61 ssh -l root "$remote_host" -- "$@" 58 run_primary mariadb -t
59}
60
61replicadb()
62{
63 run_replica mariadb -t
62} 64}
63remote_user=root
64rpc uptime
65exit 1
66 65
67ssh -l root "$remote_host" -- mariadb -t <<END 66syscheck()
67{
68 printf \
69 "==> %s %s <==\n %s\n" \
70 "$(hostname -A)" \
71 "$(hostname -I)" \
72 "$(uptime)"
73}
74
75dbcheck()
76{
77 syscheck
78 mariadb -t <<END | sed 's/^/ /'
68select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin; 79select @@hostname, @@server_id, @@gtid_slave_pos, @@sql_log_bin;
69SELECT VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE FROM INFORMATION_SCHEMA.SYSTEM_VARIABLES WHERE VARIABLE_NAME='sql_log_bin' OR VARIABLE_NAME='HOSTNAME' \G
70SELECT VARIABLE_NAME,SESSION_VALUE,GLOBAL_VALUE FROM INFORMATION_SCHEMA.SYSTEM_VARIABLES \G
71END 80END
81}
82
83create_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
91send_backup()
92{
93 mariabackup_target_dir=/var/mariadb/backup
94 rsync -zRaP -- /./"$mariabackup_target_dir" "$1":/
95}
96
97enable_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;
113END
114}
72 115
73exit 0 116create_replication_user()
117{
118 mariadb -t <<END
119 CREATE OR REPLACE USER '$1' IDENTIFIED BY '$2';
120 GRANT REPLICATION SLAVE ON *.* TO '$1';
121END
122}
74 123
75ssh -l root "$remote_host" -- mariadb <<END 124showvars()
76CHANGE 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
80END 132END
133 done
134}
135
136checkboth()
137{
138 for h in primary replica
139 do
140 run_$h dbcheck
141 done
142}
143
144if false
145then
146 checkboth
147 showvars
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
157run_primary send_backup "$replicadb_host"
158run_replica enable_replication "$our_host" "$replication_user" "$replication_password"
159