复制检查 shell 脚本错误

复制检查 shell 脚本错误

我正在尝试创建一个数据库复制检查脚本来检查主-主复制,但是在执行时出现错误。

以下是脚本

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

#Server Name
Server="Test Server"

#My Sql Username and Password
User=username
Password="password"

#Maximum Slave Time Delay
Delay="60"

#File Path to store error and email the same
Log_File=/tmp/replicationcheck.txt

#Email Settings
Subject="$Server Replication Error"
Sender_Name=TestServer
Recipients="[email protected]"

#Mail Alert Function
mailalert(){
sendmail -F $Sender_Name -it <<END_MESSAGE
To: $Recipients
Subject: $Subject

$Message_Replication_Error

`/bin/cat $Log_File`

END_MESSAGE
}

#Show Slave Status (Line I have edited later.)
Show_Slave_Status=$(echo "show slave status \G;" | mysql -u $User -p$Password) 

#Getting list of queries in mysql
$Show_Slave_Status | grep "Last_" > $Log_File

#Check if slave running
$Show_Slave_Status | grep "Slave_IO_Running: No"
if [ "$?" -eq "0" ]; then
Message_Replication_Error="$Server Replication error please check. The Slave_IO_Running state is No."
mailalert
exit 1
else
    $Show_Slave_Status | grep "Slave_IO_Running: Connecting"
    if [ "$?" -eq "0" ]; then
    Message_Replication_Error="$Server Replication error please check. The Slave_IO_Running state is Connecting."
    mailalert
    exit 1
    fi
fi

#Check if replication delayed
Seconds_Behind_Master=$Show_Slave_Status | grep "Seconds_Behind_Master" | awk -F": " {' print $2 '}
if [ "$Seconds_Behind_Master" -ge "$Delay" ]; then
Message_Replication_Error="Replication Delayed by $Seconds_Behind_Master."
mailalert
else
    if [ "$Seconds_Behind_Master" = "NULL" ]; then
    Message_Replication_Error="$Server Replication error please check. The Seconds_Behind_Master state is NULL."
    mailalert
    fi
fi

这是我收到的错误消息。

tarun@devenv:~/Desktop$ sh databasereplicationcheck.sh 
databasereplicationcheck.sh: 40: databasereplicationcheck.sh: 60: not found
databasereplicationcheck.sh: 43: databasereplicationcheck.sh: 60: not found
databasereplicationcheck.sh: 49: databasereplicationcheck.sh: 60: not found
databasereplicationcheck.sh: 59: [: Illegal number: 

请帮忙。

答案1

第 37 行的代码echo "show slave status \G;" | mysql -u $User -p$Password 2>&返回错误,我们没有看到这个错误,所以$Show_Slave_Status如果失败的话你应该打印变量:

#Show Slave Status
Show_Slave_Status=`echo "show slave status \G;" | mysql -u $User -p$Password 2>&1`
status=$?
if [ $status -ne 0 ]; then
  echo $Show_Slave_Status
  exit $status
fi

然后再次运行脚本。它现在应该会打印mysql命令的错误消息并退出。

并在第 40、43 和 49 行echo之前添加$Show_Slave_Status variable

第 40 行:echo $Show_Slave_Status | grep "Last_" > $Log_File

第 43 行:echo $Show_Slave_Status | grep "Slave_IO_Running: No"

第 49 行:echo $Show_Slave_Status | grep "Slave_IO_Running: Connecting"

相关内容