Mini Shell

Direktori : /usr/libexec/cloudlinux-backup/
Upload File :
Current File : //usr/libexec/cloudlinux-backup/pre-mysql-freeze-script

#!/usr/bin/bash

source /etc/cloudlinux-backup/mysql_freeze.config

echo "$(date -Ins) ---------------------------------------------------------------------" >> "$FREEZE_LOGFILE"
echo "$(date -Ins) - Pre-freeze script started." >> "$FREEZE_LOGFILE"

echo "$(date -Ins) - Deleting freeze lock file..." >> "$FREEZE_LOGFILE"
rm -f "$FREEZE_LOCKFILE"

if ! which mysql 1>/dev/null; then
  echo "$(date -Ins) - No MySQL found. Skip." >> "$FREEZE_LOGFILE"
  exit 0
fi

if [ -f /root/.my.cnf ]; then
  MYSQL_AUTH_OPTION='--defaults-extra-file=/root/.my.cnf'
elif [ -f /etc/psa/.psa.shadow ]; then
  MYSQL_AUTH_OPTION='--user=admin'
  export MYSQL_PWD=$(cat /etc/psa/.psa.shadow)
elif [ -f /usr/local/directadmin/conf/my.cnf ]; then
  MYSQL_AUTH_OPTION='--defaults-extra-file=/usr/local/directadmin/conf/my.cnf'
else
  echo "$(date -Ins) - Can't determinate MySQL user and password. Abort." >> "$FREEZE_LOGFILE"
  exit 3
fi

echo "$(date -Ins) - Starting MySQL freeze session..." >> "$FREEZE_LOGFILE"
mysql \
  $MYSQL_AUTH_OPTION \
  --execute="FLUSH TABLES WITH READ LOCK; SYSTEM touch \"$FREEZE_LOCKFILE\"; SYSTEM echo \"\$(date -Ins) - Freeze lock aquired.\" >> \"$FREEZE_LOGFILE\"; SYSTEM sleep $FREEZE_SNAPSHOT_TIMEOUT; SYSTEM echo \"\$(date -Ins) - Freeze session terminated.\" >> \"$FREEZE_LOGFILE\";" \
  1>/dev/null 2>>$FREEZE_LOGFILE &

FREEZE_SESSION_PID=$!
echo "$(date -Ins) - Started MySQL freeze session, PID is $FREEZE_SESSION_PID..." >> "$FREEZE_LOGFILE"

attempts=0
while [ ! -f "$FREEZE_LOCKFILE" ]; do

  if ! ps -p $FREEZE_SESSION_PID 1>/dev/null; then
    echo "$(date -Ins) - Seems like MySQL freeze statement failed. Aborted." >> "$FREEZE_LOGFILE"
    exit 1
  fi

  sleep 1s
  attempts=$((attempts+1))

  if [ $attempts -gt $FREEZE_MYSQL_TIMEOUT ]; then
    echo "$(date -Ins) - MySQL cannot freeze in suitable time. Aborting..." >> "$FREEZE_LOGFILE"
    kill $FREEZE_SESSION_PID
    exit 2
  fi

  echo "$(date -Ins) - Waiting for MySQL to freeze tables. Making try $attempts..." >> "$FREEZE_LOGFILE"

done

echo $FREEZE_SESSION_PID > "$FREEZE_LOCKFILE"
echo "$(date -Ins) - Freeze successful." >> "$FREEZE_LOGFILE"

Zerion Mini Shell 1.0