Commit 341c36d5 authored by Vincent Pelletier's avatar Vincent Pelletier

XXX: erp5: Reduce differences between both backup restoration commands.

XXX: should actually factorise code.
XXX: test
parent f5d7b18f
......@@ -14,7 +14,7 @@
# not need these here).
[mariadb-resiliency-after-import-script]
filename = instance-mariadb-resiliency-after-import-script.sh.in
md5sum = 85ce1e2f3d251aa435fef8118dca8a63
md5sum = f97ad39b3c9f539e80ac86cf0c0e145b
[mariadb-slow-query-report-script]
filename = mysql-querydigest.sh.in
......@@ -22,11 +22,11 @@ md5sum = 6457ab192d709aa2c9014e9a3e91ca20
[mariadb-start-clone-from-backup]
filename = instance-mariadb-start-clone-from-backup.sh.in
md5sum = d10b8e35b02b5391cf46bf0c7dbb1196
md5sum = 710b812d41c46d5a44b8b487a4f54f8f
[template-mariadb]
filename = instance-mariadb.cfg.in
md5sum = 93b2277185e4949a3d17be79d3710d2d
md5sum = 43acff9f4df025318a4cab0dd2bd4e32
[template-kumofs]
filename = instance-kumofs.cfg.in
......
#!{{ dash }}
set -eu
# DO NOT RUN THIS SCRIPT ON PRODUCTION INSTANCE
# OR MYSQL DATA WILL BE ERASED.
# This script will import the dump of the mysql database to the real
# database. It is launched by the clone (importer) instance of webrunner
# in the end of the import script.
# Depending on the output, it will create a file containing
# the status of the restoration (success or failure)
set -e
if [ $# -ne 1 ]; then
echo "Restaure a mariadb instance from available backup data."
echo " $0 <BACKUP FILE>"
exit 1
fi
dump=$1
mysql_executable='{{ mysql_executable }}'
mariadb_data_directory='{{ mariadb_data_directory }}'
mariadb_backup_directory='{{ mariadb_backup_directory }}'
mariadb_data_directory='{{ data_directory }}'
pid_file='{{ pid_file }}'
binlog_path='{{ binlog_path }}'
server_executable='{{ server_executable }}'
server_socket='{{ socket }}'
# Make sure mariadb is not already running
if [ -e "$pid_file" ]; then
......@@ -31,7 +27,16 @@ if [ -e "$pid_file" ]; then
fi
fi
echo "Deleting existing database..."
echo "EXISTING DATABASE CONTENT WILL BE DESTROYED"
echo "You have 5 seconds to interrupt this script..."
if sleep 5; then
echo "Expired, proceeding"
else
echo "Interrupted, aborting"
exit 1
fi
echo "Emptying data directory..."
find "$mariadb_data_directory" -mindepth 1 -delete
# $binlog_path can be empty if incremental_backup_retention_days <= -1
......@@ -45,24 +50,35 @@ if [ -n "$binlog_path" ]; then
fi
fi
echo "Starting mariadb..."
echo -n "Starting mariadb for backup restoration"
"$server_executable" --innodb-flush-method=nosync --skip-innodb-doublewrite --innodb-flush-log-at-trx-commit=0 --sync-frm=0 --slow-query-log=0 --skip-log-bin &
mysqld_pid=$!
trap "kill $mysqld_pid" EXIT TERM INT
sleep 30
# If mysql has stopped, abort
if ! [ -d /proc/$mysql_pid ]; then
echo "mysqld exited, aborting."
exit 1
fi
trap "kill $mysqld_pid; wait; exit 1" EXIT TERM INT
while true; do
if [ ! -e "/proc/$mysqld_pid" ]; then
trap EXIT TERM INT
echo "Service exited, check logs"
wait
exit 1
fi
if [ -e "$server_socket" ]; then
echo "started."
break
fi
echo -n .
sleep 0.5
done
echo "Importing data..."
# Use latest dump XXX can contain funny characters
dump=$(ls -r "$mariadb_backup_directory" | head -1)
zcat "$mariadb_backup_directory/$dump" | $mysql_executable || {
echo "Importing $dump ..."
zcat "$dump" | "$mysql_executable" || {
RESTORE_EXIT_CODE=$?
echo 'Backup restoration failed.'
exit $RESTORE_EXIT_CODE
}
echo 'Backup restoration successfully completed.'
echo "Stopping mariadb..."
trap EXIT TERM INT
kill $mysqld_pid
wait
echo "Done. Start mariadb normally."
......@@ -7,50 +7,48 @@ if [ $# -ne 7 ]; then
exit 1
fi
BACKUP=$1
MASTER_HOST=$2
MASTER_PORT=$3
MASTER_USER=$4
MASTER_SSL_CA=$5
MASTER_SSL_CERT=$6
MASTER_SSL_KEY=$7
dump=$1
master_host=$2
master_port=$3
master_user=$4
master_ssl_ca=$5
master_ssl_cert=$6
master_ssl_key=$7
CLIENT='{{ client }}'
DATA_DIRECTORY='{{ data_directory }}'
PID_FILE='{{ pid_file }}'
SERVER='{{ server }}'
UPDATE='{{ update }}'
SOCKET='{{ socket }}'
mysql_executable='{{ mysql_executable }}'
mariadb_data_directory='{{ data_directory }}'
pid_file='{{ pid_file }}'
server_executable='{{ server_executable }}'
server_socket='{{ socket }}'
# Make sure mariadb is not already running
if [ -e "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if [ $? -ne 0 ]; then
if [ -e "$pid_file" ]; then
if ! pid=$(cat "$pid_file"); then
echo "Cannot read Mariadb pidfile, assuming running. Aborting."
exit 1
fi
if kill -0 "$PID"; then
echo "Mariadb is already running with pid $PID. Aborting."
if kill -0 "$pid"; then
echo "Mariadb is already running with pid $pid. Aborting."
exit 1
fi
fi
BACKUP_HEAD="$(zcat "$BACKUP" | head -n 100)"
backup_head="$(zcat "$dump" | head -n 100)"
SQL_CHANGE_MASTER=$(echo "$BACKUP_HEAD" | grep "^--\s*CHANGE MASTER TO " | sed "s/^--\s*//")
if [ -z "$SQL_CHANGE_MASTER" ]; then
sql_change_master=$(printf "%s" "$backup_head" | grep "^--\s*CHANGE MASTER TO " | sed "s/^--\s*//")
if [ -z "$sql_change_master" ]; then
echo "'CHANGE MASTER TO' statement not found in given backup file."
echo "Is replication enabled on future master ?"
exit 1
fi
SQL_SET_GTID="$(echo "$BACKUP_HEAD" | grep "^--\s*SET GLOBAL gtid_slave_pos=" | sed "s/^--\s*//")"
if [ -z "$SQL_SET_GTID" ]; then
sql_set_gtid="$(printf "%s" "$backup_head" | grep "^--\s*SET GLOBAL gtid_slave_pos=" | sed "s/^--\s*//")"
if [ -z "$sql_set_gtid" ]; then
echo "Info: GTID not found in backup, it will not be enabled."
MASTER_USE_GTID=0
master_use_gtid=0
else
echo "Info: GTID found in backup, it will be enabled."
MASTER_USE_GTID=1
master_use_gtid=1
fi
echo "EXISTING DATABASE CONTENT WILL BE DESTROYED"
......@@ -63,61 +61,66 @@ else
fi
echo "Emptying data directory..."
find "$DATA_DIRECTORY" -mindepth 1 -delete
find "$mariadb_data_directory" -mindepth 1 -delete
echo -n "Starting mariadb for backup restoration"
"$SERVER" --innodb-flush-method=nosync --skip-innodb-doublewrite --innodb-flush-log-at-trx-commit=0 --sync-frm=0 --slow-query-log=0 --skip-log-bin &
PID=$!
trap "kill $PID; wait; exit 1" EXIT
"$server_executable" --innodb-flush-method=nosync --skip-innodb-doublewrite --innodb-flush-log-at-trx-commit=0 --sync-frm=0 --slow-query-log=0 --skip-log-bin &
mysqld_pid=$!
trap "kill $mysqld_pid; wait; exit 1" EXIT TERM INT
while true; do
if [ ! -e "/proc/$PID" ]; then
trap EXIT
if [ ! -e "/proc/$mysqld_pid" ]; then
trap EXIT TERM INT
echo "Service exited, check logs"
wait
exit 1
fi
test -e "$SOCKET" && break
if [ -e "$server_socket" ]; then
echo "started."
break
fi
echo -n .
sleep 0.5
done
"$UPDATE"
echo "Importing $BACKUP ..."
zcat "$BACKUP" | "$CLIENT"
echo "Importing $dump ..."
zcat "$dump" | "$mysql_executable" || {
RESTORE_EXIT_CODE=$?
echo 'Backup restoration failed.'
exit $RESTORE_EXIT_CODE
}
echo "Configuring server as slave..."
if [ "$MASTER_USE_GTID" -eq 1 ]; then
"$CLIENT" -e "$SQL_SET_GTID"
MASTER_USE_GTID_SQL="slave_pos"
if [ "$master_use_gtid" -eq 1 ]; then
"$mysql_executable" -e "$sql_set_gtid"
maser_use_gtid_sql="slave_pos"
else
MASTER_USE_GTID_SQL="NO"
maser_use_gtid_sql="NO"
fi
"$CLIENT" -e "
"$mysql_executable" -e "
CHANGE MASTER TO
MASTER_HOST='$MASTER_HOST',
MASTER_USER='$MASTER_USER',
MASTER_PORT=$MASTER_PORT,
MASTER_HOST='$master_host',
MASTER_USER='$master_user',
MASTER_PORT=$master_port,
MASTER_SSL=1,
MASTER_SSL_CA='$MASTER_SSL_CA',
MASTER_SSL_CERT='$MASTER_SSL_CERT',
MASTER_SSL_KEY='$MASTER_SSL_KEY',
MASTER_SSL_CA='$master_ssl_ca',
MASTER_SSL_CERT='$master_ssl_cert',
MASTER_SSL_KEY='$master_ssl_key',
MASTER_SSL_VERIFY_SERVER_CERT=1,
MASTER_USE_GTID=$MASTER_USE_GTID_SQL;
MASTER_USE_GTID=$maser_use_gtid_sql;
"
if [ "$MASTER_USE_GTID" -eq 0 ]; then
if [ "$master_use_gtid" -eq 0 ]; then
# No GTID, use binlog name & offset as provided by backup file.
# Example: CHANGE MASTER TO MASTER_LOG_FILE='binlog.003447', MASTER_LOG_POS=360;
# Notes:
# - Must happen after setting MASTER_HOST & MASTER_PORT.
# - Implicitly sets MASTER_USE_GTID=NO if it was set before.
"$CLIENT" -e "$SQL_CHANGE_MASTER"
# - Must happen after setting master_host & master_port.
# - Implicitly sets master_use_gtid=NO if it was set before.
"$mysql_executable" -e "$sql_change_master"
fi
"$CLIENT" -e "START SLAVE;"
"$mysql_executable" -e "START SLAVE;"
echo "Stopping mariadb..."
trap EXIT
kill $PID
trap EXIT TERM INT
kill $mysqld_pid
wait
echo "Done. Start mariadb normally. You may use 'show slave status' SQL command to monitor progress."
......@@ -275,11 +275,11 @@ url = {{ parameter_dict['mariadb-start-clone-from-backup'] }}
output = ${directory:bin}/start-clone-from-backup
context =
key dash dash:dash
key client binary-wrap-mysql:wrapper-path
key mysql_executable binary-wrap-mysql:wrapper-path
key data_directory my-cnf-parameters:data-directory
key pid_file my-cnf-parameters:pid-file
key server mysqld:output
key update update-mysql:output
key server_executable mysqld:output
key update_executable update-mysql:output
key socket my-cnf-parameters:socket
[{{ section('resiliency-after-import-script') }}]
......@@ -290,11 +290,13 @@ output = ${directory:bin}/restore-from-backup
context =
key dash dash:dash
key mysql_executable binary-wrap-mysql:wrapper-path
key mariadb_data_directory my-cnf-parameters:data-directory
key mariadb_backup_directory directory:mariadb-backup-full
key data_directory my-cnf-parameters:data-directory
key backup_directory directory:mariadb-backup-full
key pid_file my-cnf-parameters:pid-file
key binlog_path my-cnf-parameters:binlog-path
key server_executable mysqld:output
key update_executable update-mysql:output
key socket my-cnf-parameters:socket
[{{ section('monitor-generate-mariadb-slow-query-report') }}]
recipe = slapos.cookbook:cron.d
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment