github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/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 cp /mnt/ddev_config/mysql/*.cnf /etc/mysql/conf.d 78 # Ignore errors on files such as .gitmanaged 79 chmod -f -R ugo-w /etc/mysql/conf.d/* 80 fi 81 82 83 # If mariadb has not been initialized, copy in the base image from either the default starter image (/mysqlbase/base_db.gz) 84 # or from a provided $snapshot_dir. 85 if [ ! -f "/var/lib/mysql/db_mariadb_version.txt" ]; then 86 # If snapshot_dir is not set, this is a normal startup, so 87 # tell healthcheck to wait by touching /tmp/initializing 88 if [ -z "${snapshot:-}" ] ; then 89 touch /tmp/initializing 90 fi 91 if [ "${target:-}" = "" ]; then 92 target=${snapshot:-/var/tmp/base_db} 93 mkdir -p ${target} && cd ${target} 94 snapshot=/mysqlbase/base_db.gz 95 gunzip -c ${snapshot} | ${STREAMTOOL} -x 96 fi 97 name=$(basename $target) 98 99 rm -rf /var/lib/mysql/* /var/lib/mysql/.[a-z]* 100 ${BACKUPTOOL} --prepare --skip-innodb-use-native-aio --target-dir "$target" --user=root --password=root 2>&1 | tee "/var/log/mariabackup_prepare_$name.log" 101 ${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" 102 echo $server_db_version >/var/lib/mysql/db_mariadb_version.txt 103 echo "Database initialized from ${target}" 104 rm -f /tmp/initializing 105 fi 106 107 # db_mariadb_version.txt may be "mariadb_10.5" or "mysql_8.0" or old "10.0" or "8.0" 108 database_db_version=$(cat /var/lib/mysql/db_mariadb_version.txt) 109 # If we have an old-style reference, like "10.5", prefix it with the database type 110 if [ "${database_db_version#*_}" = "${database_db_version}" ]; then 111 database_db_version="${server_db_version%_*}_${database_db_version}" 112 fi 113 114 if [ "${server_db_version}" != "${database_db_version}" ]; then 115 echo "Starting with db server version=${server_db_version} but database was created with '${database_db_version}'." 116 echo "Attempting upgrade, but it may not work, you may need to export your database, 'ddev delete --omit-snapshot', start, and reimport". 117 118 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 & 119 pid=$! 120 set +x 121 if ! serverwait ; then 122 echo "Failed to get mysqld running to run mysql_upgrade" 123 exit 103 124 fi 125 set -x 126 echo "Attempting mysql_upgrade because db server version ${server_db_version} is not the same as database db version ${database_db_version}" 127 mysql_upgrade --socket=$SOCKET 128 kill $pid 129 fi 130 131 # And update the server db version we have here. 132 echo $server_db_version >/var/lib/mysql/db_mariadb_version.txt 133 134 cp -r /home/{.my.cnf,.bashrc} ~/ 135 mkdir -p /mnt/ddev-global-cache/{bashhistory,mysqlhistory}/${HOSTNAME} || true 136 137 echo 138 echo 'MySQL init process done. Ready for start up.' 139 echo 140 141 echo "Starting mysqld." 142 tail -f /var/log/mysqld.log & 143 exec mysqld --server-id=0