summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xreplicate-db77
1 files changed, 77 insertions, 0 deletions
diff --git a/replicate-db b/replicate-db
new file mode 100755
index 0000000..264e782
--- /dev/null
+++ b/replicate-db
@@ -0,0 +1,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 -f)"
+ printf "CREATE USER 'replikat'@'%s' IDENTIFIED BY '%s';\n" \
+ "$(hostname -f)" \
+ "${replikat_pw}"
+ printf "GRANT REPLICATION SLAVE ON *.* TO 'replikat'@'%s';\n" \
+ "$(hostname -f)"
+ 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}"