29
1 Database Backup # cat /usr/backup/backup-dumpdaily mysqldump -uroot -pPassWord DBName -- routines --flush-logs --single- transaction --master-data=2 > /backup/dumpmysqldaily/MyApp`date +'%d- %b-%Y-%H-%M'`.sql 2> /backup/dumpmysqldaily/MyApp_err.txt `(ls /backup/dumpmysqldaily/MyApp* -t | head -n 6; ls /backup/dumpmysqldaily/MyApp*)| sort | uniq -u | xargs rm`

Shell Script

Embed Size (px)

DESCRIPTION

Shell scripts useful for MySQL DBA

Citation preview

Page 1: Shell Script

1

Database Backup

# cat /usr/backup/backup-dumpdaily mysqldump -uroot -pPassWord DBName --

routines --flush-logs --single-transaction --master-data=2 > /backup/dumpmysqldaily/MyApp`date +'%d-%b-%Y-%H-%M'`.sql 2> /backup/dumpmysqldaily/MyApp_err.txt

`(ls /backup/dumpmysqldaily/MyApp* -t | head -n 6; ls /backup/dumpmysqldaily/MyApp*)| sort | uniq -u | xargs rm`

Page 2: Shell Script

2

Ftp to remote location once in a week on every Thursday

DISPLAY=:0 notify-send "Backup started and copying on desktop $standby" -t 60000

Myfile=”/backup/dumpmysqldaily/MyApp`date +'%d-%b-%Y-%H-%M'`.sql”

mydate=`date | awk '{print $1}'`

if [[ $mydate = 'Thu' && -s /backup/mysqldump/$myfile ]];then

myvar1="SQL data dump file created and today is Wednesday"

ftp -in << mytransfer

open 110.212.191.62

user UserName PassWord

bin

mput $myfile

quit

exit 0

mytransfer

fi

Page 3: Shell Script

3

Exit Code status

if [[ $? -eq 0 ]];then mysql -h110.212.191.62 -uroot -pPassWord -e"insert into

backup.report_log values(NULL, now(), '$mydb', '$myvar1', 'success')"

else mysql -h110.212.191.62 -uroot -pPassWord -e"insert into

backup.report_log values(NULL, now(), '$mydb', '$myvar1', 'fail')"

fi

DISPLAY=:0 notify-send "Backup process complete!"

Page 4: Shell Script

4

Disk space shell script

#!/bin/sh

# Shell script to monitor or watch the disk space

# set alert level 60% is default

ALERT=60

df -HP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;

do

usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )

partition=$(echo $output | awk '{ print $2 }' )

if [ $usep -ge $ALERT ]; then

# do something

fi

done

Page 5: Shell Script

5

5 ways to log the messages

# echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" >> /home/develop/disk.txt 2>> /home/develop/disk_err.txt

# logger "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"

# echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space $usep" $ADMIN

# curl --basic --user username:passWord --data status="Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" http://twitter.com/statuses/update.xml > /home/develop/loadsuccess.txt 2> /home/develop/loaderr.txt

# curl -Ld'username=soak&password=43417010&source=oksoft&dmobile=9702977470&message=disk+is+running+out+on+master+server' http://64.120.23.38/smsclient//api.php

Page 6: Shell Script

6

Watch key stats

#!/bin/sh

# watch key stats

newTicket=$(mysql -uroot -pPassWord DBName -Bse"select count(*) as mycount from tbl_Response where Status='success' AND TxnDate > date_sub(now(), interval 1 hour)")

newCount=$(mysql -uroot -pPassWord DBName -Bse"select count(*) as mycount from tbl_UserMaster where Created_dt > date_sub(now(), interval 1 hour)")

if [[ $newTicket == 0 ]] || [[ $newCount == 0 ]];then

echo "some problem with tickets or new registrations'

fi

Page 7: Shell Script

7

Find Orphan Pages

#!/bin/sh

> /home/develop/orphan1.txt

for myfile in `ls -R /var/www/html/Working_latest/ | tr "\t" "\n"`

do

grep -R -m1 $myfile /var/www/html/Working_latest/* > /dev/null 2>&1

if [[ $? -ne 0 ]];then

find /var/www/html/Working_latest/ -name $myfile >> /home/develop/orphan1.txt

fi

Done

# sort, remove duplicates and remove path

# sort -u /home/develop/orphan1.txt | sed 's/^\/var\/www\/html//g'

Page 8: Shell Script

8

Import files to MySQL

#!/bin/sh # I have abc.sql xyz.sql and pqr.sql files # to be imported in mysql for file in `ls /home/develop/report/*.sql | tr "\t" "\n"` do mysql -uroot -pPassWord dbName -f < $file 1>/dev/null

2>&1 done

Page 9: Shell Script

9

Copy file to servers Part I

#!/bin/sh

#copy file to 4 servers

while read IP

do

scp -p /home/newindex.php root@$IP:/var/www/html/ > /dev/null

done < myIPlist

111.222.333.444

555.666.777.888

999.111.222.333

444.555.666.777

myIPlist

Page 10: Shell Script

10

Copy file to servers Part II

#!/bin/sh

#copy file to 4 servers

myfile=" /home/newindex.php"

destination="/var/www/html/"

while read IP

do

scp -p $myfile root@$IP:$destination > /dev/null

done < /root/myIPlist.txt

cat /root/myIPlist.txt111.222.333.444555.666.777.888999.111.222.333444.555.666.777

Page 11: Shell Script

11

Execute SQL statement

while read IP do 0</dev/null ssh root@$IP "mysql -uroot -pPassWord -

e'select User, Host from mysql.user;'" done < /root/myIPlist.txt

Page 12: Shell Script

12

Copy table to remote servers

while read DBName IP do mysqldump -uroot -pPassWord test reports | ssh

root@$IP "mysql -uroot -pPassWd $DBName" done < myIPlist mumbai_report 111.222.333.444 delhi_report 555.666.777.888 chennai 999.111.222.333 bangalore 444.555.666.777 myIPlist

Page 13: Shell Script

13

Run sql query or dump

while read DBName IP do 0</dev/null ssh root@$IP mysql -uroot -pPassWord

$DBName < myquery.txt done < /var/www/html/IPlist.txt

Page 14: Shell Script

14

Search for a word in the database

#!/bin/bash

> tosave.txt

mysql -uroot -pPassWord -Be"show databases" | egrep -v "(backup|Database|information_schema|mysql|test)" | while read mydb

do

mysqldump -uroot -pPassWord $mydb -d --routines | grep 'machine_problem'

if [[ $? -ne 0 ]];then

echo "$mydb did not has the word" >> tosave.txt

fi

done

Page 15: Shell Script

15

Start server if not already active

service ntpd status | grep 'stopped' if [[ $? -eq 0 ]];then service ntpd start fi

Page 16: Shell Script

16

High Process Alert

# count the processes and alert the user if the processes are more than 30

myvar2=$(mysqladmin -uroot -pPassWord processlist | wc -l | awk '{print int($1)}');

if [[ $myvar2 -gt 30 ]];then echo "The number of processes is too high" fi

Page 17: Shell Script

17

Check if MySQL is alive

# ping mysql service islive=`mysqladmin -uroot -pPassWord ping |

grep alive | wc -l` if [[ $islive -ne 1 ]];then echo "Mysql is dead" fi

Page 18: Shell Script

18

status_success file output

mysqladmin -uroot -pPassWord status >> /home/develop/status_success.txt

Uptime: 2914371 Threads: 5 Questions: 181438411 Slow queries: 9351 Opens: 124111 Flush tables: 81 Open tables: 150 Queries per second avg: 62.256

Uptime: 2914431 Threads: 5 Questions: 181443669 Slow queries: 9355 Opens: 124111 Flush tables: 81 Open tables: 150 Queries per second avg: 62.257

Uptime: 2914491 Threads: 5 Questions: 181448454 Slow queries: 9358 Opens: 124115 Flush tables: 81 Open tables: 150 Queries per second avg: 62.257

Uptime: 2914551 Threads: 5 Questions: 181452947 Slow queries: 9359 Opens: 124115 Flush tables: 81 Open tables: 150 Queries per second avg: 62.258

Uptime: 2914611 Threads: 5 Questions: 181459383 Slow queries: 9359 Opens: 124124 Flush tables: 81 Open tables: 150 Queries per second avg: 62.259

Uptime: 2914671 Threads: 7 Questions: 181467155 Slow queries: 9359 Opens: 124129 Flush tables: 81 Open tables: 150 Queries per second avg: 62.260

Uptime: 2914731 Threads: 5 Questions: 181481907 Slow queries: 9360 Opens: 124159 Flush tables: 81 Open tables: 149 Queries per second avg: 62.264

Uptime: 2914791 Threads: 5 Questions: 181485640 Slow queries: 9360 Opens: 124160 Flush tables: 81 Open tables: 150 Queries per second avg: 62.264

Page 19: Shell Script

19

Negative uptime – MySQL restarted

#$uptime $threads $questions $slow $opens $flush $otables $aqueries

uptime=$(tail -2 /home/develop/status_success.txt | awk ' {

gsub(/[a-zA-Z: ]+/," ")

m=split($0,a," ");

for (i=1;i<=m;i++)

if (NR==1) b[i]=a[i]; else print a[i] - b[i]

} ' | head -1)

if [[ $uptime -lt 0 ]];then

echo "mysql restarted"

fi

Page 20: Shell Script

20

High processes alert

#$uptime $threads $questions $slow $opens $flush $otables $aqueries

questions=$(tail -2 /home/develop/status_success.txt | awk ' {

gsub(/[a-zA-Z: ]+/," ")

m=split($0,a," ");

for (i=1;i<=m;i++)

if (NR==1) b[i]=a[i]; else print a[i] - b[i]

} ' | head -3 | tail -1)

if [[ $questions -gt 20000 ]];then

echo "too many queries hitting MySQL every minute"

fi

Page 21: Shell Script

21

Slow queries are too high

#$uptime $threads $questions $slow $opens $flush $otables $aqueries

slow=$(tail -2 /home/develop/status_success.txt | awk ' {

gsub(/[a-zA-Z: ]+/," ")

m=split($0,a," ");

for (i=1;i<=m;i++)

if (NR==1) b[i]=a[i]; else print a[i] - b[i]

} ' | head -4 | tail -1)

if [[ $slow -gt 10 ]];then

echo "too many slow queries"

fi

Page 22: Shell Script

22

Email new offers to your customers

#!/bin/sh for customerMail in `mysql -uroot -pPassWord dbName -

BNe"select emailid from UserMaster"` do mail -s "New discounts offered" $customerMail < mbox.txt

-- -f [email protected] -F 'Customer Care'

done

Page 23: Shell Script

23

Slave Check Script

#!/bin/sh

# Slave Check status

mystatus=`mysql -uroot -pPassWord -e"show slave status\G" | grep 'Slave_SQL_Running' | awk '{print $2}'`

if [[ $mystatus = 'No' ]];then

echo "Slave is not working"

else

echo "Slave is working OK"

fi

Page 24: Shell Script

24

Change Master position script

#!/bin/sh

# change the master position after reading error log

binfile=`grep "Slave I/O thread exiting, read up to log" /var/log/mysqld.log | tail -1 | awk '{print $12}'`

position=`grep "Slave I/O thread exiting, read up to log" /var/log/mysqld.log | tail -1 | awk '{print $14}'`

echo "CHANGE MASTER TO MASTER_HOST='192.168.50.31', MASTER_USER='slave_user', MASTER_PASSWORD='slave_user', MASTER_LOG_FILE=$binfile MASTER_LOG_POS=$position;"

Page 25: Shell Script

25

Show database, tables and count

# mysqlshow trigger --countDatabase: trigger+--------------+----------+------------+| Tables | Columns | Total Rows |+--------------+----------+------------+| Response | 9 | 2 || mytime | 1 | 1 || todel | 11 | 2 |+--------------+----------+------------+3 rows in set.

[root@localhost ~]# mysqlshow+--------------------+| Databases |+--------------------+| information_schema || amaravati || dropme || lost+found || mysql || trigger |+--------------------+

# mysqlshow triggerDatabase: trigger+--------------+| Tables |+--------------+| Response || mytime || todel |+--------------+

Page 26: Shell Script

26

Show database, tables and count II[root@localhost ~]# mysqlshow+--------------------+| Databases |+--------------------+| information_schema || amaravati || dropme || lost+found || mysql || trigger |+--------------------+

# mysqlshow | awk '{print $2}'

Databases

information_schemaamaravatidropmelost+foundmysqltrigger

# mysqlshow | awk '{print "myslqshow --count", $2}'myslqshow --count myslqshow --count Databasesmyslqshow --count myslqshow --count information_schemamyslqshow --count amaravatimyslqshow --count dropmemyslqshow --count lost+foundmyslqshow --count mysqlmyslqshow --count triggermyslqshow --count

mysqlshow | awk '{print "myslqshow --count", $2}' | sh

Page 27: Shell Script

27

Kill runaway processes

#!/bin/sh# kill runaway processes

SEC=$1IFS="|"if [[ $SEC -lt 1 ]]; thenecho "Usage: $0 seconds"exit 1fi

mysqladmin proc -v | grep Query | grep -Evi "delete|update|insert|alter table" | while read dummy qid qusr qhost qdb qstat qsec qstat2 querydoif [ $qsec -gt $SEC ];thenecho "Killing query $qid..."mysqladmin kill $qidfi

done

Page 28: Shell Script

28

Table count in general log

for mytable in `mysql -uroot -pPassWord dbName -BNe"show tables"`

do mycount=`grep -c "$mytable" /root/mysql-gen.log` echo "$mytable" "$mycount" >> tableReport.txt done

Page 29: Shell Script

29

Delete files listed in a text file

#!/bin/sh # remove files listed in the unwanted.txt file cnt=0 while read fileName do rm -f $fileName cnt=$(($cnt+1)) done < unwanted.txt echo "total files dropped $cnt"