github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/containers/ddev-dbserver/files/docker-entrypoint.sh (about)

     1  #!/bin/bash
     2  set -x
     3  set -eu
     4  set -o pipefail
     5  
     6  SOCKET=/var/tmp/mysql.sock
     7  rm -f /tmp/healthy
     8  
     9  # We can't just switch on database type here, because early versions
    10  # of mariadb used xtrabackup
    11  export BACKUPTOOL=mariabackup
    12  export STREAMTOOL=mbstream
    13  if command -v xtrabackup >/dev/null 2>&1 ; then
    14    BACKUPTOOL="xtrabackup"
    15    STREAMTOOL="xbstream"
    16  fi
    17  
    18  
    19  # Wait for mysql server to be ready.
    20  function serverwait {
    21  	for i in {60..0};
    22  	do
    23          if mysqladmin ping -uroot --socket=$SOCKET >/dev/null 2>&1; then
    24              return 0
    25          fi
    26          # Test to make sure we got it started in the first place. kill -s 0 just tests to see if process exists.
    27          if ! kill -s 0 $pid 2>/dev/null; then
    28              echo "MariaDB initialization startup failed"
    29              return 2
    30          fi
    31          sleep 1
    32  	done
    33  	return 1
    34  }
    35  
    36  # There may be a snapshots volume mounted on /mnt/snapshots
    37  # But if not, it means we can use snapshots from /mnt/ddev_config/snapshots
    38  if [ ! -d /mnt/snapshots ]; then
    39    ln -s /mnt/ddev_config/ddev_snapshots /mnt/snapshots
    40  fi
    41  # If we have a restore_snapshot arg, get the snapshot file/directory
    42  # otherwise, fail and abort startup
    43  if [ $# = "2" ] && [ "${1:-}" = "restore_snapshot" ] ; then
    44    snapshot_basename=${2:-nothingthere}
    45    snapshot="/mnt/snapshots/${snapshot_basename}"
    46    # If a gzipped snapshot is passed in, unzip it
    47    if [ -f "$snapshot" ] && [ "${snapshot_basename##*.}" = "gz" ]; then
    48      echo "Restoring from snapshot file $snapshot"
    49      target="/var/tmp/${snapshot_basename}"
    50      mkdir -p "${target}"
    51      cd "${target}"
    52      gunzip -c ${snapshot} | ${STREAMTOOL} -x
    53      rm -rf /var/lib/mysql/*
    54    # Otherwise use it as is from the directory
    55    elif [ -d "$snapshot" ] ; then
    56      echo "Restoring from snapshot directory $snapshot"
    57      target="${snapshot}"
    58      # Ugly macOS .DS_Store in this directory can break the restore
    59      find ${snapshot} -name .DS_Store -print0 | xargs rm -f
    60      rm -rf /var/lib/mysql/*
    61    else
    62      echo "$snapshot does not exist, not attempting restore of snapshot"
    63      unset snapshot
    64      exit 101
    65    fi
    66  fi
    67  
    68  PATH=$PATH:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin mysqld -V 2>/dev/null  | awk '{print $3}' > /tmp/raw_mysql_version.txt
    69  # mysqld -V gives us the version in the form of 5.7.28-log for mysql or
    70  # 5.5.64-MariaDB-1~trusty for MariaDB. Detect database type and version and output
    71  # mysql-8.0 or mariadb-10.5.
    72  server_db_version=$(awk -F- '{ sub( /\.[0-9]+(-.*)?$/, "", $1); server_type="mysql"; if ($2 ~ /^MariaDB/) { server_type="mariadb" }; print server_type "_" $1 }' /tmp/raw_mysql_version.txt)
    73  rm -f /tmp/raw_mysql_version.txt
    74  
    75  # If we have extra mariadb cnf files, copy them to where they go.
    76  if [ -d /mnt/ddev_config/mysql ] && [ "$(echo /mnt/ddev_config/mysql/*.cnf)" != "/mnt/ddev_config/mysql/*.cnf" ] ; then
    77    echo "!includedir /mnt/ddev_config/mysql" >/etc/mysql/conf.d/ddev.cnf
    78  fi
    79  
    80  
    81  # If mariadb has not been initialized, copy in the base image from either the default starter image (/mysqlbase/base_db.gz)
    82  # or from a provided $snapshot_dir.
    83  if [ ! -f "/var/lib/mysql/db_mariadb_version.txt" ]; then
    84      # If snapshot_dir is not set, this is a normal startup, so
    85      # tell healthcheck to wait by touching /tmp/initializing
    86      if [ -z "${snapshot:-}" ] ; then
    87        touch /tmp/initializing
    88      fi
    89      if [ "${target:-}" = "" ]; then
    90        target=${snapshot:-/var/tmp/base_db}
    91        mkdir -p ${target} && cd ${target}
    92        snapshot=/mysqlbase/base_db.gz
    93        gunzip -c ${snapshot} | ${STREAMTOOL} -x
    94      fi
    95      name=$(basename $target)
    96  
    97      rm -rf /var/lib/mysql/* /var/lib/mysql/.[a-z]*
    98      ${BACKUPTOOL} --prepare --skip-innodb-use-native-aio --target-dir "$target" --user=root --password=root 2>&1 | tee "/var/log/mariabackup_prepare_$name.log"
    99      ${BACKUPTOOL} --copy-back --skip-innodb-use-native-aio --force-non-empty-directories --target-dir "$target" --user=root --password=root 2>&1 | tee "/var/log/mariabackup_copy_back_$name.log"
   100      echo $server_db_version >/var/lib/mysql/db_mariadb_version.txt
   101      echo "Database initialized from ${target}"
   102      rm -f /tmp/initializing
   103  fi
   104  
   105  # db_mariadb_version.txt may be "mariadb_10.5" or "mysql_8.0" or old "10.0" or "8.0"
   106  database_db_version=$(cat /var/lib/mysql/db_mariadb_version.txt)
   107  # If we have an old-style reference, like "10.5", prefix it with the database type
   108  if [ "${database_db_version#*_}" = "${database_db_version}" ]; then
   109    database_db_version="${server_db_version%_*}_${database_db_version}"
   110  fi
   111  
   112  if [ "${server_db_version}" != "${database_db_version}" ]; then
   113     echo "Starting with db server version=${server_db_version} but database was created with '${database_db_version}'."
   114     echo "Attempting upgrade, but it may not work, you may need to export your database, 'ddev delete --omit-snapshot', start, and reimport".
   115  
   116      PATH=$PATH:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin mysqld --skip-networking --skip-grant-tables --socket=$SOCKET >/tmp/mysqld_temp_startup.log 2>&1 &
   117      pid=$!
   118      set +x
   119      if ! serverwait ; then
   120          echo "Failed to get mysqld running to run mysql_upgrade"
   121          exit 103
   122      fi
   123      set -x
   124      echo "Attempting mysql_upgrade because db server version ${server_db_version} is not the same as database db version ${database_db_version}"
   125      mysql_upgrade --socket=$SOCKET
   126      kill $pid
   127  fi
   128  
   129  # And update the server db version we have here.
   130  echo $server_db_version >/var/lib/mysql/db_mariadb_version.txt
   131  
   132  cp -r /home/{.my.cnf,.bashrc} ~/
   133  mkdir -p /mnt/ddev-global-cache/{bashhistory,mysqlhistory}/${HOSTNAME} || true
   134  
   135  echo
   136  echo 'MySQL init process done. Ready for start up.'
   137  echo
   138  
   139  echo "Starting mysqld."
   140  tail -f /var/log/mysqld.log &
   141  exec mysqld --server-id=0