You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

replace-db.sh 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #! /bin/sh
  2. #
  3. if [ "$#" -ne 1 ]; then
  4. echo "Usage: $0 <sql dump file>" >&2
  5. exit 1
  6. fi
  7. if ! [ -e "${1}" ]; then
  8. echo "$1 not found" >&2
  9. exit 1
  10. fi
  11. set -a
  12. . "${PWD}/.env"
  13. set +a
  14. mkdir --parents "${PWD}/tmp"
  15. dump_filename_path="${1}"
  16. docker compose up db -d
  17. while ! docker ps -q -f name="${PREFIX}-db"; do
  18. echo "Waiting for the db container to be up and running..."
  19. sleep 1
  20. done
  21. while ! docker exec "${PREFIX}-db" /bin/sh -c "mysqladmin ping -h 127.0.0.1 -P 3306 --protocol=tcp -u root -p${DB_ROOT_PASSWORD} --silent"; do
  22. echo "Waiting for the mysql server in ${PREFIX}-db to be up and running..."
  23. sleep 1
  24. done
  25. echo "copying mysql dump to db container"
  26. docker cp "${dump_filename_path}" "${PREFIX}"-db:/tmp/dump.sql
  27. echo "dropping ${DB_NAME}"
  28. docker exec "${PREFIX}-db" /bin/sh -c "mysqladmin --force -uroot -p${DB_ROOT_PASSWORD} drop ${DB_NAME}"
  29. echo "creating ${DB_NAME}"
  30. docker exec "${PREFIX}-db" /bin/sh -c "mysqladmin -uroot -p${DB_ROOT_PASSWORD} create ${DB_NAME}"
  31. echo "run this command in another terminal/window to monitor progress:"
  32. echo "docker exec -it watch -n1 ${PREFIX}-db du /var/lib/mysql/${DB_NAME}-sh"
  33. echo "importing ${dump_filename_path} (${PREFIX}-db/tmp/dump.sql) into ${DB_NAME}"
  34. docker exec "${PREFIX}-db" /bin/sh -c "mysql -uroot -p${DB_ROOT_PASSWORD} ${DB_NAME} < /tmp/dump.sql"