Location: PHPKode > scripts > IAM Backup > iam-backup/docs/__filesource/fsource_iam_backup__iam_restore.php.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<!-- template designed by Marco Von Ballmoos -->
			<title>File Source for iam_restore.php</title>
			<link rel="stylesheet" href="../media/stylesheet.css" />
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'/>
		</head>
		<body>
						<h1>Source for file iam_restore.php</h1>
<p>Documentation is available at <a href="../iam_backup/_iam_restore_php.html">iam_restore.php</a></p>
<div class="src-code">
<pre><a name="a1"></a><br />1     <span class="src-php">&lt;?php</span><a name="a2"></a><br />2     <span class="src-doc">/**</span><a name="a3"></a><br />3     <span class="src-doc"> *  IAM_restore A class for restoring a database from a backup file</span><a name="a4"></a><br />4     <span class="src-doc"> *</span><span class="src-doc">  </span><span class="src-doc-coretag">@package</span><span class="src-doc"> iam_backup</span><a name="a5"></a><br />5     <span class="src-doc"> *</span><span class="src-doc">  </span><span class="src-doc-tag">@desc</span><span class="src-doc"> IAM_restore A class for restoring a database from a backup file</span><a name="a6"></a><br />6     <span class="src-doc"> *</span><a name="a7"></a><br />7     <span class="src-doc"> *</span><span class="src-doc">  </span><span class="src-doc-coretag">@author</span><span class="src-doc">     Iván Ariel Melgrati &lt;hide@address.com&gt;</span><a name="a8"></a><br />8     <span class="src-doc"> *</span><span class="src-doc">  </span><span class="src-doc-coretag">@version</span><span class="src-doc"> 1.1.1</span><a name="a9"></a><br />9     <span class="src-doc"> *</span><a name="a10"></a><br />10    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  Requires PHP v 4.0+ and MySQL 3.23+</span><a name="a11"></a><br />11    <span class="src-doc"> *</span><a name="a12"></a><br />12    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  This library is free software; you can redistribute it and/or</span><a name="a13"></a><br />13    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  modify it under the terms of the GNU Lesser General Public</span><a name="a14"></a><br />14    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  License as published by the Free Software Foundation; either</span><a name="a15"></a><br />15    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  version 2 of the License, or (at your option) any later version.</span><a name="a16"></a><br />16    <span class="src-doc"> *</span><a name="a17"></a><br />17    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  This library is distributed in the hope that it will be useful,</span><a name="a18"></a><br />18    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  but WITHOUT ANY WARRANTY; without even the implied warranty of</span><a name="a19"></a><br />19    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span><a name="a20"></a><br />20    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc">  Lesser General Public License for more details.</span><a name="a21"></a><br />21    <span class="src-doc"> */</span><a name="a22"></a><br />22    <span class="src-doc">/**</span><a name="a23"></a><br />23    <span class="src-doc"> * IAM_restore A class for restoring a database from a backup file</span><a name="a24"></a><br />24    <span class="src-doc"> *</span><span class="src-doc"> </span><span class="src-doc-coretag">@package</span><span class="src-doc"> iam_backup</span><a name="a25"></a><br />25    <span class="src-doc"> */</span><a name="a26"></a><br />26    <span class="src-key">class</span> <a href="../iam_backup/iam_restore.html">iam_restore</a><a name="a27"></a><br />27    <span class="src-sym">{</span><a name="a28"></a><br />28    <span class="src-doc">/**</span><a name="a29"></a><br />29    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@var </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$host </span><span class="src-doc">Host that holds the DB</span><a name="a30"></a><br />30    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> private</span><a name="a31"></a><br />31    <span class="src-doc">*/</span><a name="a32"></a><br />32        <span class="src-key">var</span> <a href="../iam_backup/iam_restore.html#var$host">$host</a>=<span class="src-str">&quot;localhost&quot;</span>;<a name="a33"></a><br />33    <a name="a34"></a><br />34    <span class="src-doc">/**</span><a name="a35"></a><br />35    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@var </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$dbname </span><span class="src-doc"> Database to restore</span><a name="a36"></a><br />36    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> private</span><a name="a37"></a><br />37    <span class="src-doc">*/</span><a name="a38"></a><br />38        <span class="src-key">var</span> <a href="../iam_backup/iam_restore.html#var$dbname">$dbname</a>=<span class="src-str">&quot;mysql&quot;</span>;<a name="a39"></a><br />39    <a name="a40"></a><br />40    <span class="src-doc">/**</span><a name="a41"></a><br />41    <span class="src-doc">*</span><a name="a42"></a><br />42    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@var </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$dbuser </span><span class="src-doc">User to access the Database</span><a name="a43"></a><br />43    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> private</span><a name="a44"></a><br />44    <span class="src-doc">*/</span><a name="a45"></a><br />45        <span class="src-key">var</span> <a href="../iam_backup/iam_restore.html#var$dbuser">$dbuser</a>=<span class="src-str">&quot;root&quot;</span>;<a name="a46"></a><br />46    <a name="a47"></a><br />47    <span class="src-doc">/**</span><a name="a48"></a><br />48    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@var </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$dbpass </span><span class="src-doc">Password to access the Database</span><a name="a49"></a><br />49    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> private</span><a name="a50"></a><br />50    <span class="src-doc">*/</span><a name="a51"></a><br />51        <span class="src-key">var</span> <a href="../iam_backup/iam_restore.html#var$dbpass">$dbpass</a>=<span class="src-str">&quot;&quot;</span>;<a name="a52"></a><br />52    <a name="a53"></a><br />53    <span class="src-doc">/**</span><a name="a54"></a><br />54    <span class="src-doc">*</span><a name="a55"></a><br />55    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@var </span><span class="src-doc"></span><span class="src-doc-type">String </span><span class="src-doc"></span><span class="src-doc-var">$filename </span><span class="src-doc">Filename from which the class will restore the DB</span><a name="a56"></a><br />56    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> private</span><a name="a57"></a><br />57    <span class="src-doc">*/</span><a name="a58"></a><br />58        <span class="src-key">var</span> <a href="../iam_backup/iam_restore.html#var$filename">$filename</a>;<a name="a59"></a><br />59    <a name="a60"></a><br />60    <span class="src-doc">/**</span><a name="a61"></a><br />61    <span class="src-doc">* Initialize this class. Constructor</span><a name="a62"></a><br />62    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> public</span><a name="a63"></a><br />63    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@param </span><span class="src-doc"></span><span class="src-doc-type">String </span><span class="src-doc"></span><span class="src-doc-var">$filename </span><span class="src-doc">Filename from which the class will restore the DB</span><a name="a64"></a><br />64    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@param </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$host </span><span class="src-doc"> Host that holds the DB</span><a name="a65"></a><br />65    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@param </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$dbanme </span><span class="src-doc">Database to restore</span><a name="a66"></a><br />66    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@param </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$dbuser </span><span class="src-doc">User to access the Database</span><a name="a67"></a><br />67    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@param </span><span class="src-doc"></span><span class="src-doc-type">string </span><span class="src-doc"></span><span class="src-doc-var">$dbpass </span><span class="src-doc">Password to access the Database</span><a name="a68"></a><br />68    <span class="src-doc">*/</span><a name="a69"></a><br />69        <span class="src-key">function</span> <a href="../iam_backup/iam_restore.html#methodiam_restore">iam_restore</a><span class="src-sym">(</span><span class="src-var">$filename</span><span class="src-sym">,</span> <span class="src-var">$host</span><span class="src-sym">,</span> <span class="src-var">$dbname</span><span class="src-sym">,</span> <span class="src-var">$dbuser</span><span class="src-sym">,</span> <span class="src-var">$dbpass</span><span class="src-sym">)</span><a name="a70"></a><br />70        <span class="src-sym">{</span><a name="a71"></a><br />71            <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$host">host</a> = <span class="src-var">$host</span>;<a name="a72"></a><br />72            <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbname">dbname</a> =  <span class="src-var">$dbname</span>;<a name="a73"></a><br />73            <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbuser">dbuser</a> = <span class="src-var">$dbuser</span>;<a name="a74"></a><br />74            <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbpass">dbpass</a> = <span class="src-var">$dbpass</span>;<a name="a75"></a><br />75            <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$filename">filename</a> = <span class="src-var">$filename</span>;<a name="a76"></a><br />76        <span class="src-sym">}</span><a name="a77"></a><br />77    <a name="a78"></a><br />78    <span class="src-doc">/**</span><a name="a79"></a><br />79    <span class="src-doc">* Open the file containing the backup data</span><a name="a80"></a><br />80    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@return </span><span class="src-doc"></span><span class="src-doc-type">String </span><span class="src-doc"></span><span class="src-doc">a String containing the DB Dump</span><a name="a81"></a><br />81    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> private</span><a name="a82"></a><br />82    <span class="src-doc">*/</span><a name="a83"></a><br />83        <span class="src-key">function</span> <a href="../iam_backup/iam_restore.html#method_Open">_Open</a><span class="src-sym">(</span><span class="src-sym">)</span><a name="a84"></a><br />84            <span class="src-sym">{</span><a name="a85"></a><br />85                <span class="src-var">$fp</span> = <a href="http://www.php.net/gzopen">gzopen</a><span class="src-sym">(</span> <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$filename">filename</a><span class="src-sym">,</span> <span class="src-str">&quot;rb&quot;</span> <span class="src-sym">)</span> <span class="src-key">or</span> <span class="src-key">die</span><span class="src-sym">(</span>&quot;<span class="src-str"><span class="src-id">Error</span>. <span class="src-id">No</span> <span class="src-id">se</span> <span class="src-id">pudo</span> <span class="src-id">abrir</span> <span class="src-id">el</span> <span class="src-id">archivo</span> <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$filename">filename</a>"</span><span class="src-sym">)</span>;<a name="a86"></a><br />86                <span class="src-key">while</span> <span class="src-sym">(</span> <span class="src-sym">!</span> <a href="http://www.php.net/gzeof">gzeof</a><span class="src-sym">(</span> <span class="src-var">$fp</span> <span class="src-sym">)</span> <span class="src-sym">)</span><a name="a87"></a><br />87                    <span class="src-sym">{</span><a name="a88"></a><br />88                    <span class="src-var">$line</span> = <a href="http://www.php.net/gzgets">gzgets</a><span class="src-sym">(</span> <span class="src-var">$fp</span><span class="src-sym">,</span> <span class="src-num">1024</span> <span class="src-sym">)</span>;<a name="a89"></a><br />89                    <span class="src-var">$SQL</span> .= &quot;<span class="src-str"><span class="src-var">$line</span></span>&quot;;<a name="a90"></a><br />90                    <span class="src-sym">}</span><a name="a91"></a><br />91                <a href="http://www.php.net/gzclose">gzclose</a><span class="src-sym">(</span><span class="src-var">$fp</span><span class="src-sym">)</span>;<a name="a92"></a><br />92                <span class="src-key">return</span> <span class="src-var">$SQL</span>;<a name="a93"></a><br />93            <span class="src-sym">}</span><a name="a94"></a><br />94    <span class="src-doc">/**</span><a name="a95"></a><br />95    <span class="src-doc">* Restore the data from file</span><a name="a96"></a><br />96    <span class="src-doc">*</span><span class="src-doc"> </span><span class="src-doc-coretag">@access</span><span class="src-doc"> public</span><a name="a97"></a><br />97    <span class="src-doc">*/</span><a name="a98"></a><br />98        <span class="src-key">function</span> <a href="../iam_backup/iam_restore.html#methodperform_restore">perform_restore</a><span class="src-sym">(</span><span class="src-sym">)</span><a name="a99"></a><br />99            <span class="src-sym">{</span><a name="a100"></a><br />100               <span class="src-var">$SQL</span> = <a href="http://www.php.net/explode">explode</a><span class="src-sym">(</span><span class="src-str">&quot;;&quot;</span><span class="src-sym">,</span> <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#method_Open">_Open</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">)</span>;<a name="a101"></a><br />101               <span class="src-var">$link</span> = <a href="http://www.php.net/mysql_connect">mysql_connect</a><span class="src-sym">(</span><span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$host">host</a><span class="src-sym">,</span> <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbuser">dbuser</a><span class="src-sym">,</span> <span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbpass">dbpass</a><span class="src-sym">)</span> <span class="src-key">or</span> <span class="src-sym">(</span><span class="src-key">die</span> <span class="src-sym">(</span><a href="http://www.php.net/mysql_error">mysql_error</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">)</span><span class="src-sym">)</span>;<a name="a102"></a><br />102               <a href="http://www.php.net/mysql_select_db">mysql_select_db</a><span class="src-sym">(</span><span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbname">dbname</a><span class="src-sym">,</span> <span class="src-var">$link</span><span class="src-sym">)</span> <span class="src-key">or</span> <span class="src-sym">(</span><span class="src-key">die</span> <span class="src-sym">(</span><a href="http://www.php.net/mysql_error">mysql_error</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">)</span><span class="src-sym">)</span>;<a name="a103"></a><br />103   <a name="a104"></a><br />104               <span class="src-comm">//---- Drop all tables from DB</span><a name="a105"></a><br />105               <span class="src-var">$result</span> = <a href="http://www.php.net/mysql_list_tables">mysql_list_tables</a><span class="src-sym">(</span><span class="src-var">$this</span><span class="src-sym">-&gt;</span><a href="../iam_backup/iam_restore.html#var$dbname">dbname</a><span class="src-sym">)</span>;<a name="a106"></a><br />106               <span class="src-var">$not</span> = <a href="http://www.php.net/mysql_num_rows">mysql_num_rows</a><span class="src-sym">(</span><span class="src-var">$result</span><span class="src-sym">)</span>;<a name="a107"></a><br />107               <span class="src-key">for</span> <span class="src-sym">(</span><span class="src-var">$i</span>=<span class="src-num">0</span>; <span class="src-var">$i</span>&lt;<span class="src-var">$not</span>-<span class="src-num">1</span>; <span class="src-var">$i</span>++<span class="src-sym">)</span><a name="a108"></a><br />108                   <span class="src-sym">{</span><a name="a109"></a><br />109                       <span class="src-var">$row</span> = <a href="http://www.php.net/mysql_fetch_row">mysql_fetch_row</a><span class="src-sym">(</span><span class="src-var">$result</span><span class="src-sym">)</span>;<a name="a110"></a><br />110                       <span class="src-var">$tables</span> .= <span class="src-var">$row</span><span class="src-sym">[</span><span class="src-num">0</span><span class="src-sym">]</span>.<span class="src-str">&quot;,&quot;</span>;<a name="a111"></a><br />111                   <span class="src-sym">}</span><a name="a112"></a><br />112   <a name="a113"></a><br />113               <span class="src-var">$row</span> = <a href="http://www.php.net/mysql_fetch_row">mysql_fetch_row</a><span class="src-sym">(</span><span class="src-var">$result</span><span class="src-sym">)</span>;<a name="a114"></a><br />114               <span class="src-var">$tables</span> .= <span class="src-var">$row</span><span class="src-sym">[</span><span class="src-num">0</span><span class="src-sym">]</span>;<a name="a115"></a><br />115               <span class="src-key">if</span> <span class="src-sym">(</span><span class="src-var">$tables</span> != <span class="src-str">&quot;&quot;</span> || <span class="src-var">$tables</span> != <span class="src-id">NULL</span><span class="src-sym">)</span><a name="a116"></a><br />116                   <a href="http://www.php.net/mysql_query">mysql_query</a><span class="src-sym">(</span><span class="src-str">&quot;DROP TABLE &quot;</span>.<span class="src-var">$tables</span><span class="src-sym">)</span> <span class="src-key">or</span> <span class="src-sym">(</span><span class="src-key">die</span> <span class="src-sym">(</span><a href="http://www.php.net/mysql_error">mysql_error</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">)</span><span class="src-sym">)</span>;<a name="a117"></a><br />117   <a name="a118"></a><br />118               <span class="src-comm">//---- And now execute the SQL statements from backup file.</span><a name="a119"></a><br />119               <span class="src-key">for</span> <span class="src-sym">(</span><span class="src-var">$i</span>=<span class="src-num">0</span>;<span class="src-var">$i</span>&lt;<a href="http://www.php.net/count">count</a><span class="src-sym">(</span><span class="src-var">$SQL</span><span class="src-sym">)</span>-<span class="src-num">1</span>;<span class="src-var">$i</span>++<span class="src-sym">)</span><a name="a120"></a><br />120                   <span class="src-sym">{</span><a name="a121"></a><br />121                       <a href="http://www.php.net/mysql_unbuffered_query">mysql_unbuffered_query</a><span class="src-sym">(</span><span class="src-var">$SQL</span><span class="src-sym">[</span><span class="src-var">$i</span><span class="src-sym">]</span><span class="src-sym">)</span> <span class="src-key">or</span> <span class="src-sym">(</span><span class="src-key">die</span> <span class="src-sym">(</span><a href="http://www.php.net/mysql_error">mysql_error</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">)</span><span class="src-sym">)</span>;<a name="a122"></a><br />122                   <span class="src-sym">}</span><a name="a123"></a><br />123               <span class="src-key">return</span> <span class="src-num">1</span>;<a name="a124"></a><br />124               <span class="src-sym">}</span><a name="a125"></a><br />125   <span class="src-sym">}</span><a name="a126"></a><br />126   <span class="src-php">?&gt;</span></pre>
</div>
	<p class="notes" id="credit">
		Documentation generated on Thu, 11 Dec 2003 15:35:28 -0300 by <a href="http://www.phpdoc.org" target="_blank">phpDocumentor 1.2.3</a>
	</p>
	</body>
</html>
Return current item: IAM Backup