daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest Jul 21st, 2017 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. set -e
  3. set -o pipefail
  4. set -u
  5. # set -x
  6.  
  7. function !!
  8. {
  9.   echo "$@"
  10.   "$@"
  11.   return $?
  12. }
  13. function is_pipe
  14. {
  15.   if [ -p /dev/stdin ]; then
  16.   #if [ -p /dev/fd/0  ]; then
  17.   #if [ -p /proc/self/fd/0 ]; then
  18.   #if [ -t 0 ]; then
  19.     return 0
  20.   elif [ -p /dev/stdout ]; then
  21.     return 0
  22.   else
  23.     return 1
  24.   fi
  25. }
  26.  
  27. MYSQL_PORT=${MYSQL_PORT:-3306}
  28.  
  29. if [ -z "${MYSQL_DATA_DIR:-}" ];then
  30.   echo "mysql のデータディレクトリの位置を環境変数 MYSQL_DATA_DIR で指定してください"
  31.   exit -1
  32. fi
  33. if [ -z "${MYSQL_VERSION:-}" ];then
  34.   echo "mysql のバージョンを環境変数 MYSQL_VERSION で指定してください"
  35.   exit -1
  36. fi
  37. if [ ! -d "${MYSQL_DATA_DIR}" ];then
  38.   mkdir -p "${MYSQL_DATA_DIR}"
  39. fi
  40. MYSQL_DATA_DIR_SUM="$(cd "$MYSQL_DATA_DIR" && pwd -|md5)"
  41. CONTAINER_NAME=mysql-$MYSQL_VERSION-${MYSQL_DATA_DIR_SUM:0:5}
  42.  
  43. # 起動済み
  44. eps=$(docker ps -f "name=/$CONTAINER_NAME\$" -q)
  45. # 停止中
  46. eps2=$(docker ps -f "name=/$CONTAINER_NAME\$" -q -a)
  47.  
  48. case "${1:-}" in
  49. "start")
  50.   echo "VERSION=$MYSQL_VERSION DATA_DIR=$MYSQL_DATA_DIR PORT=$MYSQL_PORT CONTAINER=$CONTAINER_NAME"
  51.   if [ -z "$eps" ];then
  52.     if [ -z "$eps2" ];then
  53.       !! docker run -d --name "$CONTAINER_NAME" \
  54.         -e MYSQL_ALLOW_EMPTY_PASSWORD=${MYSQL_ALLOW_EMPTY_PASSWORD:true} \
  55.         -e MYSQL_ROOT_HOST=${MYSQL_ROOT_HOST:\%} \
  56.         -p ${MYSQL_PORT}:3306 \
  57.         ${MYSQL_DATABASE:+ -e MYSQL_DATABASE=$MYSQL_DATABASE } \
  58.         ${MYSQL_USER:+ -e MYSQL_USER=$MYSQL_USER } \
  59.         ${MYSQL_PASSWORD:+ -e MYSQL_PASSWORD=$MYSQL_PASSWORD } \
  60.         -e MYSQL_HISTFILE=/var/lib/mysql/mysql_history \
  61.         -v "$MYSQL_DATA_DIR:/var/lib/mysql" \
  62.         "mysql/mysql-server:$MYSQL_VERSION"
  63.     else
  64.       !! docker start "$CONTAINER_NAME"
  65.     fi
  66.   else
  67.     echo "mysql is running"
  68.   fi
  69.   ;;
  70. "stop")
  71.   echo "VERSION=$MYSQL_VERSION DATA_DIR=$MYSQL_DATA_DIR PORT=$MYSQL_PORT CONTAINER=$CONTAINER_NAME"
  72.   if [ -n "$eps" ];then
  73.     !! docker stop "$CONTAINER_NAME"
  74.   else
  75.     echo "mysql is not running"
  76.   fi
  77.   ;;
  78. "status")
  79.   echo "VERSION=$MYSQL_VERSION DATA_DIR=$MYSQL_DATA_DIR PORT=$MYSQL_PORT CONTAINER=$CONTAINER_NAME"
  80.   if [ -z "$eps2" ];then
  81.     echo "docker mysql is not running"
  82.     exit -1
  83.   else
  84.     docker ps -f "name=/$CONTAINER_NAME\$" -a
  85.   fi
  86.   ;;
  87. "rm")
  88.   echo "VERSION=$MYSQL_VERSION DATA_DIR=$MYSQL_DATA_DIR PORT=$MYSQL_PORT CONTAINER=$CONTAINER_NAME"
  89.   if [ -n "$eps" ];then
  90.     !! docker stop "$CONTAINER_NAME"
  91.   fi
  92.   if [ -n "$eps2" ];then
  93.     !! docker rm "$CONTAINER_NAME"
  94.   else
  95.     echo "mysql container is not exists"
  96.   fi
  97.   ;;
  98. "client"|"dump"|"mysqldump")
  99.   if [ -z "$eps" ];then
  100.     echo "mysql is not running"
  101.     exit -1
  102.   else
  103.     case "${1}" in
  104.     "client")
  105.       cmd=mysql
  106.       ;;
  107.     "dump"|"mysqldump")
  108.       cmd=mysqldump
  109.       ;;
  110.     esac
  111.     shift
  112.     term_opt=""
  113.     if ! is_pipe; then
  114.       term_opt="-t"
  115.     fi
  116.     docker exec -i $term_opt "$CONTAINER_NAME" $cmd "$@"
  117.   fi
  118.   ;;
  119. *)
  120.   echo "mysql を docker で起動します。 MYSQL_VERSION と MYSQL_DATA_DIR の環境変数を指定してください"
  121.   echo "VERSION=$MYSQL_VERSION DATA_DIR=$MYSQL_DATA_DIR PORT=$MYSQL_PORT CONTAINER=$CONTAINER_NAME"
  122.   echo "{start|stop|rm|client|dump|status}"
  123.   exit -1
  124. esac
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top