Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/replicate-db
blob: e7699b8010a553c651a9fb1c60768a04b7f39185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash

set -e

if [ -d '/var/lib/mysql' ]; then
  read -p 'mariadb seems to be installed already - I will remove it first.' -r s
  if [ -n "${s}" ]; then
    echo 'Aborted.'
    exit
  fi
  sudo systemctl stop mysqld || true
  sudo pacman -Rs mariadb || true
  sudo rm -rf --one-file-system '/var/lib/mysql' '/etc/mysql'
fi

sudo pacman -S --noconfirm mariadb
sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
sudo sed -i '
  /myisam_sort_buffer_size/ a group_concat_max_len = 4096
  /^#server-id/ {
    s/^#//
    b
  }
  s/^server-id/#\0/
' '/etc/mysql/my.cnf'
sudo systemctl start mysqld

read -p 'enter our new root-pw: ' -s -r our_root_pw
printf '\n'
read -p 'enter remote root-pw: '  -s -r remote_root_pw
printf '\n'

replikat_pw=$(
  head -c12 /dev/urandom | \
    base64 -w0
)

printf '\n\n%s\n%s\n\n\n\n\n' "${our_root_pw}" "${our_root_pw}" | \
  sudo mysql_secure_installation

tmp_file=$(mktemp)
trap 'rm "${tmp_file}"' EXIT

printf '%s\n' "${remote_root_pw}" | \
  ssh buildmaster '
    echo "logged in" >&2;
    mysql buildmaster -u root -p -e "'"$(
      printf "DROP USER IF EXISTS 'replikat'@'%s';\n" \
        "$(hostname)"
      printf "CREATE USER 'replikat'@'%s' IDENTIFIED BY '%s';\n" \
        "$(hostname)" \
        "${replikat_pw}"
      printf "GRANT REPLICATION SLAVE ON *.* TO 'replikat'@'%s';\n" \
        "$(hostname)"
      printf 'FLUSH PRIVILEGES;\n'
      printf 'QUIT\n'
    )"'" >&2;
    echo "created user" >&2;
    cd /var/backup;
    ls -t | grep -m1 '"'"'^database-.*\.xz$'"'"' | xargs pv
    echo "copied dump" >&2;
  ' > "${tmp_file}"

{
  printf 'SHOW SLAVE STATUS;\n'
  printf "CHANGE MASTER TO MASTER_HOST='%s', MASTER_PORT=%s, MASTER_USER='%s', MASTER_PASSWORD='%s';\n" \
    'buildmaster.archlinux32.org' \
    3306 \
    'replikat' \
    "${replikat_pw}"
  xzcat "${tmp_file}" | pv
  xzgrep -- '^-- CHANGE MASTER TO ' "${tmp_file}" | \
    sed 's/^-- //'
  printf 'START SLAVE;\n'
  printf 'SHOW SLAVE STATUS;\n'
} | \
  mysql -u root -p"${our_root_pw}"