github.com/verrazzano/verrazzano@v1.7.0/platform-operator/scripts/hooks/mysql-hook.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2022, Oracle and/or its affiliates.
     4  # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     5  #
     6  
     7  BACKUP_DIR="/oracle/mysql/data-backup"
     8  
     9  # takes backup of MySQL
    10  function backup() {
    11    FILE_PATH=${BACKUP_DIR}/$1
    12    mysqldump --all-databases --single-transaction --quick --lock-tables=false > ${FILE_PATH} -u root -p${MYSQL_ROOT_PASSWORD}
    13    if [ $? -eq 0 ]; then
    14           echo "MySQL dump successful"
    15           exit 0
    16      else
    17          echo "MySQL dump failed"
    18          exit 1
    19    fi
    20  }
    21  
    22  # Checks if MySQL is healthy
    23  # then restores MySQL from an existing dump file
    24  function restore() {
    25    FILE_PATH=${BACKUP_DIR}/$1
    26    if test -f "${FILE_PATH}"; then
    27        echo "'${FILE_PATH}' exists."
    28    else
    29       echo "'${FILE_PATH}' does not exist"
    30       exit 1
    31    fi
    32  
    33    # wait for MySQL to be up
    34    while ! mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD} --silent; do
    35            # polling delay to check if MySQL is up
    36            sleep 5
    37    done
    38  
    39    # verify MySQL status
    40    mysqladmin -u root -p${MYSQL_ROOT_PASSWORD} status
    41    if [ $? != 0 ] ; then
    42       echo "MySQL status is not healthy even though its reachable."
    43       exit 1
    44    fi
    45    echo "MySQL is up and ready to receive connections"
    46  
    47    # perform MySQL restore
    48    mysql -u root -p${MYSQL_ROOT_PASSWORD} < ${FILE_PATH}
    49    if [ $? -eq 0 ]; then
    50         echo "MySQL restore successful"
    51         exit 0
    52    else
    53        echo "MySQL restore failed"
    54        exit  1
    55    fi
    56  }
    57  
    58  mkdir -p ${BACKUP_DIR}
    59  
    60  function usage {
    61      echo
    62      echo "usage: $0 [-o operation ] [-f filename]"
    63      echo "  -o operation  The operation to be performed on MySQL (backup/restore)"
    64      echo "  -f filename   The filename of the MySQL dump file"
    65      echo "  -h            Help"
    66      echo
    67      exit 1
    68  }
    69  
    70  while getopts o:f:h flag
    71  do
    72      case "${flag}" in
    73          o) OPERATION=${OPTARG};;
    74          f) MYSQL_DUMP_FILE_NAME=${OPTARG};;
    75          h) usage;;
    76          *) usage;;
    77      esac
    78  done
    79  
    80  if [ -z "${OPERATION:-}" ]; then
    81      echo " Operation cannot be empty !"
    82      usage
    83      exit 1
    84  else
    85    if [ $OPERATION != "backup" ] && [ $OPERATION != "restore" ]; then
    86      echo "Invalid Operation - $OPERATION. Allowed operation values are backup or restore"
    87      exit 1
    88    fi
    89  fi
    90  
    91  if [ -z "${MYSQL_DUMP_FILE_NAME:-}"  ]; then
    92      echo "Dump file name cannot be empty !"
    93      usage
    94      exit 1
    95  fi
    96  
    97  ${OPERATION} ${MYSQL_DUMP_FILE_NAME}