Created
November 15, 2016 10:57
-
-
Save gitfrage/45ca5d414fdb4fce15ae6c26a7b3fbc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
DBNAME="mydatabase" | |
TABLES="table_x table_y" | |
FILENAME=/tmp/tmp.sql | |
SOURCE="[email protected]" | |
DEST="[email protected] [email protected]" | |
ssh $SOURCE "sudo mysqldump --single-transaction --hex-blob --order-by-primary --skip-triggers -u user --password=password $DBNAME $TABLES > $FILENAME" | |
scp $SOURCE:$FILENAME $FILENAME | |
# Function to check for active table locks | |
function checkLocks($host) | |
{ | |
RESULT=$(ssh $host "mysql -h127.0.0.1 -uuser -ppass -P3304 $DBNAME -e \"SELECT p.user, LEFT(p.HOST, LOCATE(':', p.HOST) - 1) host, p.id, TIMESTAMPDIFF(SECOND, t.TRX_STARTED, NOW()) duration, COUNT(DISTINCT ot.REQUESTING_TRX_ID) waiting, p.Command, p.Info FROM INFORMATION_SCHEMA.INNODB_TRX t JOIN INFORMATION_SCHEMA.PROCESSLIST p ON ( p.ID = t.TRX_MYSQL_THREAD_ID ) LEFT JOIN INFORMATION_SCHEMA.INNODB_LOCK_WAITS ot ON ( ot.BLOCKING_TRX_ID = t.TRX_id ) WHERE t.TRX_STARTED + INTERVAL 2 SECOND <= NOW() GROUP BY LEFT(p.HOST, LOCATE(':', p.HOST) - 1), p.id, duration HAVING duration >= 2 OR waiting > 0;\"") | |
} | |
for host in $DEST; do | |
checkLocks($host) | |
cat $FILENAME | ssh $host 'mysql -u user --password=password $DBNAME' || exit 1 | |
done | |
while [ "$RESULT" != "" ]; do | |
echo "######### TABLE LOCKS FOUND ########" | |
echo $RESULT | |
read -p "(C)heck Again/(A)bort?" choice | |
case $choice in | |
[Cc]* ) checkLocks;; | |
[Aa]* ) exit 1;; | |
* ) checkLocks;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment