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