#!/bin/bash set -e PATH=$(dirname "$0"):$PATH source rpc.bash datadir=data default_replica_host_file=$datadir/default-replica-host replication_password_file=$datadir/replication-password if [ -r "$default_replica_host_file" ] then read default_replica_host < "$default_replica_host_file" fi gen_password() { generated_password_length=32 generated_password=$(tr -cd a-zA-Z0-9 < /dev/urandom | head -c "$generated_password_length") [ "${#generated_password}" -eq "$generated_password_length" ] printf '%s\n' "$generated_password" } if ! [ -e "$replication_password_file" ] then gen_password > "$replication_password_file" fi if [ -r "$replication_password_file" ] then read replication_password < "$replication_password_file" [ "${#replication_password}" -ge 30 ] fi primary_host=$(hostname --fqdn) replica_host=${1:-$default_replica_host} replication_user=replication [ "$primary_host" ] [ "$replica_host" ] [ "$replication_password" ] run_primary() { (set -x : primary : $1 $2 ${3:+ ...}) BASH_RPC_REMOTE_DEST=$primary_host remote_run_function "$@" } run_replica() { (set -x : replica : $1 $2 ${3:+ ...}) BASH_RPC_REMOTE_DEST=$replica_host remote_run_function "$@" } show_hostnames() { printf \ "==> %s %s <==\n %s\n" \ "$(hostname -A)" \ "$(hostname -I)" \ "$(uptime)" } check_db() { show_hostnames mariadb --skip-reconnect -t <"$marialog" 2>&1 set -x mariabackup --backup --rsync "$@" mariabackup --prepare "$@") || e=$? if [ "$e" = 0 -a -e "$binlog_info_file" ] then tail -v -n2 "$marialog" >&2 rm "$marialog" else cat "$marialog" >&2 false fi } delete_backup() { set -x rm -r /var/mariadb/backup } send_backup() { mariabackup_target_dir=/var/mariadb/backup set -x rsync -zaR -- /./"${mariabackup_target_dir#/}" "$1":/ } stop_database_server_and_remove_database_files() { livedb=/var/lib/mysql set -e if [ -e "$livedb" ] then if [ "$(systemctl is-active mariadb)" = active ] then systemctl stop mariadb fi livedb_backup=$livedb~$(date -Ins) mv -v -T -- "$livedb" "$livedb_backup" mkdir "$livedb" chown --reference="$livedb_backup" "$livedb" chmod --reference="$livedb_backup" "$livedb" fi } restore_from_backup() { mariabackup_target_dir=/var/mariadb/backup set -e stop_database_server_and_remove_database_files marialog=$(mktemp) exec >"$marialog" 2>&1 set -- \ -u root \ --target-dir="$mariabackup_target_dir" if mariabackup --move-back "$@" then tail -v -n2 "$marialog" >&2 # Can't believe mariabackup # is so primitive as to # recommend this chown in its # documentation chown -R mysql:mysql /var/lib/mysql systemctl start mariadb else cat "$marialog" >&2 fi } enable_replication() { primary_host=$1 replication_user=$2 replication_password=$3 mariabackup_target_dir=/var/mariadb/backup binlog_info_file=$mariabackup_target_dir/xtrabackup_binlog_info set -e read master_log_file master_log_pos gtid_slave_pos < $binlog_info_file tee /dev/stderr <