Tag Archives: mysql

Calling mysqldump in Python

Python is a fantastic tool to know, and despite being a beginner I find myself using it more and more for everyday tasks. Bash is great for knocking together quick scripts, but when you want to something a little more complex such as interfacing with APIs or other systems over a network, you really need a more fully-featured programming language.

The topic of this post, however, is the kind of task that bash is perfect for. Thanks to mysqldump, a database backup script can be written in a few lines and dump/restores are easily automated. So why on earth would we do this in Python?
Continue reading

I Sincerely Hope I Never Write a Script like this Again

This sort of stuff destroys your soul:

#!/bin/bash
logfile=~/slave-watcher.log

while true; do
	status=$(mysql --execute="show slave status\G"|grep "Seconds_Behind_Master:"|awk '{print $2}')

	if [ $status == "NULL" ]; then
		mysql --execute="show slave status\G" | grep "Last_SQL_Error:" | tee -a $logfile
		mysql --execute="set global sql_slave_skip_counter=1; start slave;"
	fi

	sleep 1
done

What it’s doing is looking at MySQL’s slave status and skipping over any statement that causes an error. There are so many reasons why you should never do this, and I won’t go into detail on what necessitated it here, but you’ll be glad to know the slave I set this loose on was not in production!