<?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>dom111.co.uk &#187; backup</title>
	<atom:link href="http://www.dom111.co.uk/blog/tag/backup/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dom111.co.uk/blog</link>
	<description>Move along. Nothing to see here.</description>
	<lastBuildDate>Wed, 26 Oct 2011 16:37:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MySQL backup script</title>
		<link>http://www.dom111.co.uk/blog/coding/mysql-backup-script/30</link>
		<comments>http://www.dom111.co.uk/blog/coding/mysql-backup-script/30#comments</comments>
		<pubDate>Thu, 16 Oct 2008 08:45:30 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=30</guid>
		<description><![CDATA[I&#8217;ve recently been learning more and more about bash scripting and the cool functions and features that are included, that i&#8217;ll probably never use&#8230; I recently had to set up a cron to backup all MySQL databases but I didn&#8217;t want them in one huge file. So I&#8217;ve written a little script, that is probably [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been learning more and more about bash scripting and the cool functions and features that are included, that i&#8217;ll probably never use&#8230;</p>
<p>I recently had to set up a cron to backup all MySQL databases but I didn&#8217;t want them in one huge file.</p>
<p>So I&#8217;ve written a little script, that is probably not entirely efficient, but serves it&#8217;s purpose for us:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># get todays date</span>
<span style="color: #007800;">DATE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">&quot;%F&quot;</span><span style="color: #000000; font-weight: bold;">`</span>;
&nbsp;
<span style="color: #666666; font-style: italic;"># go to the backup directory</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span>mysql
&nbsp;
<span style="color: #666666; font-style: italic;"># export the databases, each to it's own file</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># the first echo is sent to mysql, so we're basically echoing SHOW DATABASES; to mysql</span>
<span style="color: #666666; font-style: italic;"># then we're chopping off the first line (sed 1d) and passing the remaining input to</span>
<span style="color: #666666; font-style: italic;"># xargs which accepts \n or space delimited arguments as a list and echo's out the</span>
<span style="color: #666666; font-style: italic;"># string mysqldump -u &lt;username&gt; -p&lt;password&gt; % &gt; %.sql (replacing % with the name) to</span>
<span style="color: #666666; font-style: italic;"># /bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;SHOW DATABASES;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-u</span> <span style="color: #000000; font-weight: bold;">&lt;</span>username<span style="color: #000000; font-weight: bold;">&gt;</span> -p<span style="color: #000000; font-weight: bold;">&lt;</span>password<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> 1d <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">--replace</span>=<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;mysqldump -u &lt;username&gt; -p&lt;password&gt; % &gt; %.sql&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># compress the backups</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> czf mysql_backup_<span style="color: #007800;">$DATE</span>.tar.gz <span style="color: #000000; font-weight: bold;">*</span>.sql
&nbsp;
<span style="color: #666666; font-style: italic;"># remove the large .sql files</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">*</span>.sql</pre></div></div>

<p>This was tested on a SuSE 11 server, might need some tweaking on other dists.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/mysql-backup-script/30/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

