<?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>Chris Cook .me &#187; backup</title>
	<atom:link href="http://chriscook.me/tag/backup/feed/" rel="self" type="application/rss+xml" />
	<link>http://chriscook.me</link>
	<description>Homepage</description>
	<lastBuildDate>Sat, 23 Oct 2010 22:53:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Use PHP to Backup your MySQL Database</title>
		<link>http://chriscook.me/web-development/backup-mysql-database-php/</link>
		<comments>http://chriscook.me/web-development/backup-mysql-database-php/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 20:00:13 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[automated backup]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[cron job]]></category>
		<category><![CDATA[databsae]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=104</guid>
		<description><![CDATA[If you don&#8217;t backup your databases regularly, shame on you. You should! The code below will allow you to generate a backup as often as you&#8217;d like. It only makes sense to run this via a CRON job. This is pretty easy to do and you can set the frequency as you wish. The backup [...]]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t backup your databases regularly, shame on you.  You should!</p>
<p>The code below will allow you to generate a backup as often as you&#8217;d like.  It only makes sense to run this via a CRON job.  This is pretty easy to do and you can set the frequency as you wish.</p>
<p>The backup files will be stored in the same directory as this script.  </p>
<p><em><strong>Please leave feedback and let me know if this works for you!</strong></em></p>
<p>Here&#8217;s the PHP code:<br />
{code type=php}<br />
backup_tables(&#8216;localhost&#8217;,'username&#8217;,'password&#8217;,'blog&#8217;);</p>
<p>// Backup the entire database or just a specific table.<br />
function backup_tables($host,$user,$pass,$name,$tables = &#8216;*&#8217;)<br />
{</p>
<p>	$link = mysql_connect($host,$user,$pass);<br />
	mysql_select_db($name,$link);</p>
<p>	//get all of the tables<br />
	if($tables == &#8216;*&#8217;)<br />
	{<br />
		$tables = array();<br />
		$result = mysql_query(&#8216;SHOW TABLES&#8217;);<br />
		while($row = mysql_fetch_row($result))<br />
		{<br />
			$tables[] = $row[0];<br />
		}<br />
	}<br />
	else<br />
	{<br />
		$tables = is_array($tables) ? $tables : explode(&#8216;,&#8217;,$tables);<br />
	}</p>
<p>	//This method is completed for each table<br />
	foreach($tables as $table)<br />
	{<br />
		$result = mysql_query(&#8216;SELECT * FROM &#8216;.$table);<br />
		$num_fields = mysql_num_fields($result);</p>
<p>		$return.= &#8216;DROP TABLE &#8216;.$table.&#8217;;';<br />
		$row2 = mysql_fetch_row(mysql_query(&#8216;SHOW CREATE TABLE &#8216;.$table));<br />
		$return.= &#8220;\n\n&#8221;.$row2[1].&#8221;;\n\n&#8221;;</p>
<p>		for ($i = 0; $i < $num_fields; $i++)<br />
		{<br />
			while($row = mysql_fetch_row($result))<br />
			{<br />
				$return.= &#8216;INSERT INTO &#8216;.$table.&#8217; VALUES(&#8216;;<br />
				for($j=0; $j<$num_fields; $j++)<br />
				{<br />
					$row[$j] = addslashes($row[$j]);<br />
					$row[$j] = ereg_replace(&#8220;\n&#8221;,&#8221;\\n&#8221;,$row[$j]);<br />
					if (isset($row[$j])) { $return.= &#8216;&#8221;&#8216;.$row[$j].&#8217;&#8221;&#8216; ; } else { $return.= &#8216;&#8221;"&#8216;; }<br />
					if ($j<($num_fields-1)) { $return.= &#8216;,&#8217;; }<br />
				}<br />
				$return.= &#8220;);\n&#8221;;<br />
			}<br />
		}<br />
		$return.=&#8221;\n\n\n&#8221;;<br />
	}</p>
<p>	//Now, we&#8217;ll save the file<br />
	$handle = fopen(&#8216;backup-&#8217;.time().&#8217;-&#8217;.(md5(implode(&#8216;,&#8217;,$tables))).&#8217;.sql&#8217;,'w+&#8217;);<br />
	fwrite($handle,$return);<br />
	fclose($handle);<br />
}<br />
{/code}</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/backup-mysql-database-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

