<?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>Wisdom and Wonder &#187; PHP</title>
	<atom:link href="http://www.wisdomandwonder.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wisdomandwonder.com</link>
	<description>Computer Science and Personal Philosophy</description>
	<lastBuildDate>Mon, 26 Jul 2010 01:23:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Simple PHP Class to Back Up MySQL</title>
		<link>http://www.wisdomandwonder.com/article/3273/a-simple-php-class-to-back-up-mysql</link>
		<comments>http://www.wisdomandwonder.com/article/3273/a-simple-php-class-to-back-up-mysql#comments</comments>
		<pubDate>Sun, 07 Jun 2009 01:57:52 +0000</pubDate>
		<dc:creator>Grant</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming Language]]></category>

		<guid isPermaLink="false">http://www.wisdomandwonder.com/?p=3273</guid>
		<description><![CDATA[My web host is cutting over from allowing shell scripts to requiring scripting languages instead. I had wanted to write a script in PHP, so I figured this is a good opportunity.
Here is a simple PHP class to back up a MySQL database and blanket-delete old backups from a directory.


class MysqlBackup &#123;
    [...]]]></description>
			<content:encoded><![CDATA[<p>My web host is cutting over from allowing shell scripts to requiring scripting languages instead. I had wanted to write a script in PHP, so I figured this is a good opportunity.</p>
<p>Here is a simple PHP class to back up a MySQL database and blanket-delete old backups from a directory.<br />
<span id="more-3273"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; ">class</span> MysqlBackup <span style="color: #000000; font-weight: bold; ">&#123;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$host</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'host'</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$db</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'db'</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$user</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'user'</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$passwd</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'passwd'</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$dest_dir</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'dest_dir'</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$dest_file_prefix</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'dest_file_prefix'</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; ">private</span> <span style="color: #000088;">$days_to_keep</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #0000ff;">'0'</span><span style="color: #000000; font-weight: bold; ">;</span>
&nbsp;
<span style="color: #000000; ">public</span> <span style="color: #000000; ">function</span> __construct<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$host</span><span style="color: #000000; font-weight: bold; ">,</span> <span style="color: #000088;">$db</span><span style="color: #000000; font-weight: bold; ">,</span> <span style="color: #000088;">$user</span><span style="color: #000000; font-weight: bold; ">,</span> <span style="color: #000088;">$passwd</span><span style="color: #000000; font-weight: bold; ">,</span> <span style="color: #000088;">$dest_dir</span><span style="color: #000000; font-weight: bold; ">,</span> <span style="color: #000088;">$dest_file_prefix</span><span style="color: #000000; font-weight: bold; ">,</span> <span style="color: #000088;">$days_to_keep</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>host <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$host</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>db  <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$db</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>user  <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$user</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>passwd  <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$passwd</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>dest_dir <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$dest_dir</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>dest_file_prefix <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$dest_file_prefix</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>days_to_keep <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$days_to_keep</span><span style="color: #000000; font-weight: bold; ">;</span>
    <span style="color: #000000; font-weight: bold; ">&#125;</span>
&nbsp;
<span style="color: #000000; ">public</span> <span style="color: #000000; ">function</span> dump<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
        <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;MysqlBackup::dump()<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;Dumping database: <span style="color: #006699; font-weight: bold;">$this-&gt;db</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$date</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">date</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #0000ff;">'m-d-Y_H-i-s'</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;Dump file date: <span style="color: #006699; font-weight: bold;">$date</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$dest_file</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>dest_file_prefix <span style="color: #000000; font-weight: bold; ">.</span> <span style="color: #0000ff;">'.'</span> <span style="color: #000000; font-weight: bold; ">.</span> <span style="color: #000088;">$date</span> <span style="color: #000000; font-weight: bold; ">.</span> <span style="color: #0000ff;">'.sql.bz2'</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$dest_file_path</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">&quot;<span style="color: #006699; font-weight: bold;">$this-&gt;dest_dir</span>/<span style="color: #006699; font-weight: bold;">$dest_file</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;Dump file destination: <span style="color: #006699; font-weight: bold;">$dest_file_path</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$cmd</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">&quot;mysqldump --add-drop-table --host=<span style="color: #006699; font-weight: bold;">$this-&gt;host</span> --user=<span style="color: #006699; font-weight: bold;">$this-&gt;user</span> --password=<span style="color: #006699; font-weight: bold;">$this-&gt;passwd</span> <span style="color: #006699; font-weight: bold;">$this-&gt;db</span> | bzip2 -c &gt; <span style="color: #006699; font-weight: bold;">$dest_file_path</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; font-style: italic; ">// call 'system' with '$cmd' as an argument here</span>
        <span style="color: #000000; font-style: italic; ">// for some reason, WordPress wants to blow up when I include the call</span>
    <span style="color: #000000; font-weight: bold; ">&#125;</span>
<span style="color: #000000; ">public</span> <span style="color: #000000; ">function</span> clean<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
        <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;MysqlBackup::clean()<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$handle</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">opendir</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>dest_dir<span style="color: #000000; font-weight: bold; ">&#41;</span> or <span style="color: #000000; ">die</span> <span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #0000ff;">'Could not open directory: '</span><span style="color: #000000; font-weight: bold; ">.</span> <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>dest_dir<span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; ">print</span> <span style="color: #0000ff;">'Opened directory: '</span> <span style="color: #000000; font-weight: bold; ">.</span> <span style="color: #000088;">$this</span><span style="color: #000000; font-weight: bold; ">-&gt;</span>dest_dir <span style="color: #000000; font-weight: bold; ">.</span> <span style="color: #000000; ">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;Examining files<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000088;">$keep_threshold_time</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">strtotime</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; ">&quot;-<span style="color: #006699; font-weight: bold;">$this-&gt;days_to_keep</span> days&quot;</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">;</span>
        <span style="color: #000000; ">while</span> <span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; ">false</span> <span style="color: #000000; font-weight: bold; ">!==</span> <span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">readdir</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
            <span style="color: #000088;">$dest_file_path</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">&quot;<span style="color: #006699; font-weight: bold;">$this-&gt;dest_dir</span>/<span style="color: #006699; font-weight: bold;">$file</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
            <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;'<span style="color: #006699; font-weight: bold;">$dest_file_path</span>': &quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
            <span style="color: #000000; ">if</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">!</span><span style="color: #000000; ">is_dir</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$dest_file_path</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
                <span style="color: #000088;">$file_time</span> <span style="color: #000000; font-weight: bold; ">=</span> <span style="color: #000000; ">filemtime</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$dest_file_path</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">;</span>
                <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;<span style="color: #006699; font-weight: bold;">$file_time</span> &lt; <span style="color: #006699; font-weight: bold;">$keep_threshold_time</span>? &quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
                <span style="color: #000000; ">if</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$file_time</span> <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #000088;">$keep_threshold_time</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
                    <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;Yes. Deleting.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
                    <span style="color: #000000; ">unlink</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000088;">$dest_file_path</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">;</span>
                <span style="color: #000000; font-weight: bold; ">&#125;</span>
                <span style="color: #000000; ">else</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
                    <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;No. Skipping.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
                <span style="color: #000000; font-weight: bold; ">&#125;</span>
            <span style="color: #000000; font-weight: bold; ">&#125;</span>
            <span style="color: #000000; ">else</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>
                <span style="color: #000000; ">print</span> <span style="color: #000000; ">&quot;A Directory, excluding<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold; ">;</span>
            <span style="color: #000000; font-weight: bold; ">&#125;</span>
        <span style="color: #000000; font-weight: bold; ">&#125;</span>
    <span style="color: #000000; font-weight: bold; ">&#125;</span>
<span style="color: #000000; font-weight: bold; ">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.wisdomandwonder.com/article/3273/a-simple-php-class-to-back-up-mysql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
