<?xml version="1.0" encoding="iso-8859-1"?>
<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>CrankBerry Blog &#187; Cronjob</title>
	<atom:link href="http://www.crankberryblog.com/tag/cronjob/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crankberryblog.com</link>
	<description>Sharing ideas on web design, development and the internet economy</description>
	<lastBuildDate>Thu, 24 Nov 2011 19:37:08 +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>Automated MySQL database backup with PHP and cronjob</title>
		<link>http://www.crankberryblog.com/2010/automated-mysql-database-backup-with-php-and-cronjob</link>
		<comments>http://www.crankberryblog.com/2010/automated-mysql-database-backup-with-php-and-cronjob#comments</comments>
		<pubDate>Fri, 16 Apr 2010 17:04:36 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web Owners]]></category>
		<category><![CDATA[Cronjob]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP Script]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=373</guid>
		<description><![CDATA[At some point of working on your website you'll want to back up you MySQL database. This is they key for all of you building dynamic sites. If one day your server goes down or something you may have the files backed up but without the database you'll have to start from scratch again. If you operate a very slow-paced site then manual back up is fine but if you want to backup your database daily then it becomes tedious. I, myself is lazy so I came up with a PHP and Cronjob solution to automatically backup MySQL database.]]></description>
			<content:encoded><![CDATA[<p>At some point of working on your website you&#039;ll want to back up you MySQL database. This is they key for all of you building dynamic sites. If one day your server goes down or something you may have the files backed up but without the database you&#039;ll have to start from scratch again. If you operate a very slow-paced site then manual back up is fine but if you want to backup your database daily then it becomes tedious. I, myself is lazy so I came up with a PHP and Cronjob solution to automatically backup MySQL database.</p>
<h3>Automating It</h3>
<p><strong>What you need?</strong> The first thing you need to know how to do it set cronjobs. If you don&#039;t know how to do that you can <a href="http://www.crankberryblog.com/2009/setting-php-cron-job-with-crontab" title="Learn to set cronjobs">learn how to set cronjobs here</a>. The cronjob is what will automate your server to run the script and how often to run it.</p>
<h3>The Files</h3>
<p>All you&#039;ll need is two files to make this work. The first file, <strong>mysqldump.php</strong> can be grabbed from <a href="http://www.creativefactory.it/lab/">CreativeFactory.it</a>. The next thing you need is my backup.php file. Just create a file called <strong>backup.php</strong> and dump the following script inside.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span></p>
<p><span class="co1">//The File Name You Want To Use</span><br />
<span class="re0">$fileName</span> = <span class="st0">&#039;Filename&#039;</span>;</p>
<p><span class="co1">//Global</span><br />
<span class="re0">$sqlhost</span> = <span class="st0">&#039;localhost&#039;</span>;<br />
<span class="re0">$sqluser</span> = <span class="st0">&#039;db_user&#039;</span>;<br />
<span class="re0">$sqlpass</span> = <span class="st0">&#039;db_password&#039;</span>;<br />
<span class="re0">$sqldb</span> = <span class="st0">&#039;db_name&#039;</span>;</p>
<p><span class="co1">//**********************************</span><br />
<span class="co1">//You Don&#039;t Need to Edit This</span><br />
<span class="co1">//**********************************</span></p>
<p><span class="co1">//Today&#039;s Timestamp</span><br />
<span class="re0">$today_ts</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="st0">&quot;now&quot;</span><span class="br0">&#41;</span>;</p>
<p>
<span class="re0">$backupDate</span> = <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#039;y.m.d.h.i&#039;</span>, <span class="re0">$today_ts</span><span class="br0">&#41;</span>;<br />
<span class="re0">$backupFile</span> = &nbsp;<span class="re0">$backupDate</span>.<span class="st0">&#039;- &#039;</span>.<span class="re0">$fileName</span>.<span class="st0">&#039;.txt&#039;</span>;</p>
<p><span class="co1">//Connect to mysql server</span><br />
<span class="re0">$connessione</span> = @<a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="re0">$sqlhost</span>,<span class="re0">$sqluser</span>,<span class="re0">$sqlpass</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Include class</span><br />
<span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#039;mysqldump.php&#039;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Create new instance of MySQLDump</span><br />
<span class="re0">$dumper</span> = <span class="kw2">new</span> MySQLDump<span class="br0">&#40;</span><span class="re0">$sqldb</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//If you want to write the MySQL dump to file</span><br />
<span class="re0">$dumper</span>-&gt;<span class="me1">writeDump</span><span class="br0">&#40;</span><span class="re0">$backupFile</span><span class="br0">&#41;</span>;</p>
<p><span class="kw2">?&gt;</span></div>
<h3>The Setup</h3>
<p>Configure the first file variables of the script and then just leave the rest. Put the backup.php and mysqldump.php in the same folder where you want the back up files to be dumped. Set the cronjob to run backup.php you don&#039;t need to worry about mysqldump.php. That&#039;s it, it&#039;ll automatically backup.</p>
<h3>Possible Problems</h3>
<p>The problems that you may experience is the MySQL privileges. If your account doesn&#039;t have enough privilege then you may not be able to properly query the database. Another problem maybe the CHMOD of the folder you&#039;re backing up to. You need to have write access into that folder.</p>
<h3>Recovering from Backup</h3>
<p>To recover you database just log into your phpMyAdmin, open the database you want to recover. Select the import tab and just browse for the file. If I recall the file itself should overwrite tables that are already in place if not then you may have to drop all the tables first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/automated-mysql-database-backup-with-php-and-cronjob/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

