Blame view

stack/erp5/instance-mariadb-resiliency-after-import-script.sh.in 2.17 KB
Vincent Pelletier committed
1
#!{{ dash }}
2 3 4 5 6 7 8 9 10 11 12

# 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)

13 14
set -e

Vincent Pelletier committed
15 16 17 18 19 20
mysql_executable='{{ mysql_executable }}'
mariadb_data_directory='{{ mariadb_data_directory }}'
mariadb_backup_directory='{{ mariadb_backup_directory }}'
pid_file='{{ pid_file }}'
binlog_path='{{ binlog_path }}'
server_executable='{{ server_executable }}'
21 22 23

# Make sure mariadb is not already running
if [ -e "$pid_file" ]; then
Julien Muchembled committed
24
  if ! pid=$(cat "$pid_file"); then
Vincent Pelletier committed
25 26 27
    echo "Cannot read Mariadb pidfile, assuming running. Aborting."
    exit 1
  fi
28 29 30 31 32 33 34
  if kill -0 "$pid"; then
    echo "Mariadb is already running with pid $pid. Aborting."
    exit 1
  fi
fi

echo "Deleting existing database..."
Julien Muchembled committed
35
find "$mariadb_data_directory" -mindepth 1 -delete
36

Nicolas Wavrant committed
37 38
# $binlog_path can be empty if incremental_backup_retention_days <= -1
if [ -n "$binlog_path" ]; then
Vincent Pelletier committed
39
  new_binlog_directory="$(dirname "$binlog_path")"
Nicolas Wavrant committed
40 41 42 43 44 45 46
  binlog_index_file="$new_binlog_directory/binlog.index"
  if [ -e "$binlog_index_file" ]; then
    echo "Adapting binlog database to new paths..."
    old_binlog_directory="$(dirname $(head -n 1 $binlog_index_file))"
    sed -e "s|$old_binlog_directory|$new_binlog_directory|g" $binlog_index_file > $binlog_index_file
  fi
fi
47 48

echo "Starting mariadb..."
Vincent Pelletier committed
49
"$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 &
50
mysqld_pid=$!
51 52
trap "kill $mysqld_pid" EXIT TERM INT
sleep 30
53
# If mysql has stopped, abort
54
if ! [ -d /proc/$mysql_pid ]; then
55 56 57
  echo "mysqld exited, aborting."
  exit 1
fi
58 59 60

echo "Importing data..."
# Use latest dump XXX can contain funny characters
Vincent Pelletier committed
61
dump=$(ls -r "$mariadb_backup_directory" | head -1)
Julien Muchembled committed
62 63
zcat "$mariadb_backup_directory/$dump" | $mysql_executable -u root || {
  RESTORE_EXIT_CODE=$?
64
  echo 'Backup restoration failed.'
Julien Muchembled committed
65 66
  exit $RESTORE_EXIT_CODE
}
67

Julien Muchembled committed
68
echo 'Backup restoration successfully completed.'