<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brian Bell &#187; Sysadmin</title>
	<atom:link href="http://webguru.org/tag/sysadmin/feed/" rel="self" type="application/rss+xml" />
	<link>http://webguru.org</link>
	<description>Bits of web-dev, travel and personal musings...</description>
	<lastBuildDate>Mon, 09 Nov 2009 21:36:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL Automated Backup and Testing Bash Script</title>
		<link>http://webguru.org/2007/10/31/lamp/mysql-automated-backup-and-testing-bash-script/</link>
		<comments>http://webguru.org/2007/10/31/lamp/mysql-automated-backup-and-testing-bash-script/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 20:19:44 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://webguru.org/2007/10/31/lamp/mysql-automated-backup-and-testing-bash-script/</guid>
		<description><![CDATA[So &#8211; you can go to any one of 100,000 sites that will tell you how to do an automated MySQL database dump with some combination of mysqldump and crond, etc.  But, I was recently faced with the question, &#8220;what happens if the dump file is corrupt? can we validate it before we pack [...]]]></description>
			<content:encoded><![CDATA[<p>So &#8211; you can go to any one of 100,000 sites that will tell you how to do an automated MySQL database dump with some combination of mysqldump and crond, etc.  But, I was recently faced with the question, &#8220;what happens if the dump file is corrupt? can we validate it before we pack it away with our backup service?&#8221;  So I came up with this little shell script.</p>
<p>It does the following:</p>
<ol>
<li>Creates a backup of the selected db using mysqldump</li>
<li>Generates an MD5 checksum of the backup file (written to a separate file)</li>
<li>Attempts to restore the dumped file into a dummy test database</li>
<li>If errors are encountered, it grabs the error and sends an email to the designated address</li>
<li>If no errors are encountered, wraps the .sql and .sql.md5 in a timestamped, gzipped, tarball &#8211; then deletes the originals</li>
</ol>
<p><a href="http://www.webguru.org/content/mysql_backup.tar.gz">Download the file here</a></p>
<pre class="brush: css;">
#!/bin/sh
#######################################################
# LICENSE:
# (c) 2007 Brian Bell (GNU LGPL V2.1) You may
# view the full copyright text at:
# http://www.opensource.org/licenses/lgpl-license.html
#
# DESCRIPTION:
# A simple BASH script to do automate MySQL database
# backup; includes testing and MD5 hash creation.
# Emails designated address on failure.
#######################################################

## CONFIGURATION VARS
MYSQL_NAME=
MYSQL_HOST=
MYSQL_USER=
MYSQL_PASS=
MYSQL_TESTDB=
BACKUP_PATH=/path/to/backup/dir # No trailing slash
MAIL_SUBJECT=”TESTING MySQL Backup Error”
MAIL_TO=”monitor@yourdomain.com”

#######################################################
## We need to create a unique timestamp for use on the filename
TIMESTAMP=`date +%Y_%m_%d`

## Generate the base part of the filename to use in backing up
BACKUP_FILE_BASE=”${MYSQL_NAME}_${TIMESTAMP}”

echo “Backing up $MYSQL_NAME…”
/usr/bin/mysqldump –opt -c -e -Q -h $MYSQL_HOST -u $MYSQL_USER –password=$MYSQL_PASS \
–add-drop-table $MYSQL_NAME &gt; $BACKUP_PATH/$BACKUP_FILE_BASE.sql

## MD5 the backup file
/usr/bin/md5sum -t $BACKUP_PATH/$BACKUP_FILE_BASE.sql &gt; $BACKUP_PATH/$BACKUP_FILE_BASE.sql.md5

## Try to import the backup sql into a test db
MYSQL_RESULT=`/usr/bin/mysql -h ${MYSQL_HOST} -u ${MYSQL_USER} –password=${MYSQL_PASS} ${MYSQL_TESTDB} &lt; \
${BACKUP_PATH}/${BACKUP_FILE_BASE}.sql &gt;

${BACKUP_PATH}/mysql_test.log`

if [[ “$MYSQL_RESULT” =~ “ERROR” ]]
then
echo “The following error was encountered at `date` ” &gt; ${BACKUP_PATH}/error_email.log
echo “” &gt;&gt; ${BACKUP_PATH}/error_email.log
echo “#####################################################################” &gt;&gt; ${BACKUP_PATH}/error_email.log
echo $MYSQL_RESULT &gt;&gt; ${BACKUP_PATH}/error_email.log

echo “SENDING ERROR EMAIL TO: ${MAIL_TO}”
/bin/mail -s “$MAIL_SUBJECT” “$MAIL_TO” &lt; ${BACKUP_PATH}/error_email.log
else
tar czpf $BACKUP_PATH/$TIMESTAMP_$BACKUP_FILE_BASE.sql.tar.gz $BACKUP_PATH/$BACKUP_FILE_BASE.sql* –remove-files
fi
</pre>
]]></content:encoded>
			<wfw:commentRss>http://webguru.org/2007/10/31/lamp/mysql-automated-backup-and-testing-bash-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

