Heim >Backend-Entwicklung >PHP-Tutorial >PHP数据库备份与恢复,PHP数据库备份恢复_PHP教程
先说下关于数据库备份与恢复的原理:
1、查找所有表->2、查找所有字段->3、查找所有数据->4、生成SQL
备份注意点:
2=>需要列出所有字段名,字段类型等相关信息
3=>读取数据出来注意特殊符号的转换addslashes()
4=>把数据格式化生成对应的SQL
mysql_list_tables() 表查询函数,类似mysql_query() 函数 mysql_fetch_field() 字段信息函数,返回句柄 恢复注意点: 1、 注意数据库的大小,过大或者过多的表分段处理
<?<span>php </span><span>$mysql_dbname</span> = "php100"<span>; </span><span>mysql_connect</span>("localhost", "root", ""<span>); </span><span>mysql_select_db</span>(<span>$mysql_dbname</span><span>); </span><span>$sql_list</span> = <span>mysql_list_tables</span>(<span>$mysql_dbname</span><span>); </span><span>while</span> (<span>$row_data</span> = <span>mysql_fetch_row</span>(<span>$sql_list</span><span>)) { </span><span>echo</span> <span>$row_data</span>[0] . "<br>"<span>; get_table_fd(</span><span>$row_data</span>[0], "sql"<span>); </span><span>//</span><span> $fp=fopen("sql/$tablename.sql","w+"); // fwrite($fp,$field); // fclose($fp);</span> <span>} </span><span>function</span> get_table_fd(<span>$tablename</span><span>) { </span><span>$field</span> = "CREATE TABLE `<span>$tablename</span>`(\n"<span>; </span><span>$result</span> = <span>mysql_query</span>("select * from <span>$tablename</span>"<span>); </span><span>while</span> (<span>$meta</span> = <span>mysql_fetch_field</span>(<span>$result</span><span>)) { </span><span>if</span> (<span>$meta</span>-><span>not_null) </span><span>$not_null</span> = "not_null"<span>; </span><span>$field</span> .= "`<span>$meta</span>->name` <span>$meta</span>->type(<span>$meta</span>->max_length) <span>$not_null</span>; \n"<span>; } </span><span>$field</span> .= ")\n"<span>; </span><span>return</span> <span>$field</span><span>; } </span>?>
在网上找到个不错的类库,可直接拿来用,记录一下
1、db.php 数据库类
2、backup.php 备份
3、restore.php 恢复
下载附件
db.php
<span> 1</span> <? <span> 2</span> <span>class</span><span> db{ </span><span> 3</span> <span> 4</span> <span>var</span> <span>$linkid</span><span>; </span><span> 5</span> <span>var</span> <span>$sqlid</span><span>; </span><span> 6</span> <span>var</span> <span>$record</span><span>; </span><span> 7</span> <span> 8</span> <span>function</span> db(<span>$host</span>="",<span>$username</span>="",<span>$password</span>="",<span>$database</span>=""<span>) </span><span> 9</span> <span> { </span><span>10</span> <span>if</span>(!<span>$this</span>->linkid) @<span>$this</span>->linkid = <span>mysql_connect</span>(<span>$host</span>, <span>$username</span>, <span>$password</span>) or <span>die</span>("连接服务器失败."<span>); </span><span>11</span> @<span>mysql_select_db</span>(<span>$database</span>,<span>$this</span>->linkid) or <span>die</span>("无法打开数据库"<span>); </span><span>12</span> <span>mysql_query</span>('set names gbk'<span>); </span><span>13</span> <span>return</span> <span>$this</span>-><span>linkid;} </span><span>14</span> <span>15</span> <span>function</span> query(<span>$sql</span><span>) </span><span>16</span> {<span>if</span>(<span>$this</span>->sqlid=<span>mysql_query</span>(<span>$sql</span>,<span>$this</span>->linkid)) <span>return</span> <span>$this</span>-><span>sqlid; </span><span>17</span> <span>else</span><span> { </span><span>18</span> <span>$this</span>->err_report(<span>$sql</span>,<span>mysql_error</span><span>); </span><span>19</span> <span>return</span> <span>false</span><span>;} </span><span>20</span> <span> } </span><span>21</span> <span>22</span> <span>function</span> nr(<span>$sql_id</span>=""<span>) </span><span>23</span> {<span>if</span>(!<span>$sql_id</span>) <span>$sql_id</span>=<span>$this</span>-><span>sqlid; </span><span>24</span> <span>return</span> <span>mysql_num_rows</span>(<span>$sql_id</span><span>);} </span><span>25</span> <span>26</span> <span>function</span> nf(<span>$sql_id</span>=""<span>) </span><span>27</span> {<span>if</span>(!<span>$sql_id</span>) <span>$sql_id</span>=<span>$this</span>-><span>sqlid; </span><span>28</span> <span>return</span> <span>mysql_num_fields</span>(<span>$sql_id</span><span>);} </span><span>29</span> <span>30</span> <span>function</span> nextrecord(<span>$sql_id</span>=""<span>) </span><span>31</span> {<span>if</span>(!<span>$sql_id</span>) <span>$sql_id</span>=<span>$this</span>-><span>sqlid; </span><span>32</span> <span>if</span>(<span>$this</span>->record=<span>mysql_fetch_array</span>(<span>$sql_id</span>)) <span>return</span> <span>$this</span>-><span>record; </span><span>33</span> <span>else</span> <span>return</span> <span>false</span><span>; </span><span>34</span> <span> } </span><span>35</span> <span>36</span> <span>function</span> f(<span>$name</span><span>) </span><span>37</span> <span> { </span><span>38</span> <span>if</span>(<span>$this</span>->record[<span>$name</span>]) <span>return</span> <span>$this</span>->record[<span>$name</span><span>]; </span><span>39</span> <span>else</span> <span>return</span> <span>false</span><span>; </span><span>40</span> <span> } </span><span>41</span> <span>42</span> <span>function</span> close() {<span>mysql_close</span>(<span>$this</span>-><span>linkid);} </span><span>43</span> <span>44</span> <span>function</span> lock(<span>$tblname</span>,<span>$op</span>="WRITE"<span>) </span><span>45</span> {<span>if</span>(<span>mysql_query</span>("lock tables ".<span>$tblname</span>." ".<span>$op</span>)) <span>return</span> <span>true</span>; <span>else</span> <span>return</span> <span>false</span><span>;} </span><span>46</span> <span>47</span> <span>function</span><span> unlock() </span><span>48</span> {<span>if</span>(<span>mysql_query</span>("unlock tables")) <span>return</span> <span>true</span>; <span>else</span> <span>return</span> <span>false</span><span>;} </span><span>49</span> <span>50</span> <span>function</span><span> ar() { </span><span>51</span> <span>return</span> @<span>mysql_affected_rows</span>(<span>$this</span>-><span>linkid); </span><span>52</span> <span> } </span><span>53</span> <span>54</span> <span>function</span><span> i_id() { </span><span>55</span> <span>return</span> <span>mysql_insert_id</span><span>(); </span><span>56</span> <span> } </span><span>57</span> <span>58</span> <span>function</span> err_report(<span>$sql</span>,<span>$err</span><span>) </span><span>59</span> <span> { </span><span>60</span> <span>echo</span> "Mysql查询错误<br>"<span>; </span><span>61</span> <span>echo</span> "查询语句:".<span>$sql</span>."<br>"<span>; </span><span>62</span> <span>echo</span> "错误信息:".<span>$err</span><span>; </span><span>63</span> <span> } </span><span>64</span> }?>
backup.php
<span> 1</span> <? <span> 2</span> <span>global</span> <span>$mysqlhost</span>, <span>$mysqluser</span>, <span>$mysqlpwd</span>, <span>$mysqldb</span><span>; </span><span> 3</span> <span>$mysqlhost</span>=""; <span>//</span><span>host name</span> <span> 4</span> <span>$mysqluser</span>=""; <span>//</span><span>login name</span> <span> 5</span> <span>$mysqlpwd</span>=""; <span>//</span><span>password</span> <span> 6</span> <span>$mysqldb</span>=""; <span>//</span><span>name of database</span> <span> 7</span> <span> 8</span> <span>include</span>("mydb.php"<span>); </span><span> 9</span> <span>$d</span>=<span>new</span> db(<span>$mysqlhost</span>,<span>$mysqluser</span>,<span>$mysqlpwd</span>,<span>$mysqldb</span><span>); </span><span> 10</span> <span>/*</span><span>--------------界面--------------</span><span>*/</span><span>if</span>(!<span>$_POST</span>['act']){<span>/*</span><span>----------------------</span><span>*/</span> <span> 11</span> <span>$msgs</span>[]="服务器备份目录为backup"<span>; </span><span> 12</span> <span>$msgs</span>[]="对于较大的数据表,强烈建议使用分卷备份"<span>; </span><span> 13</span> <span>$msgs</span>[]="只有选择备份到服务器,才能使用分卷备份功能"<span>; </span><span> 14</span> show_msg(<span>$msgs</span><span>); </span><span> 15</span> ?> <span> 16</span> <form name="form1" method="post" action="backup.php"> <span> 17</span> <table width="99%" border="1" cellpadding='0' cellspacing='1'> <span> 18</span> <tr align="center" <span>class</span>='header'><td colspan="2">数据备份</td></tr> <span> 19</span> <tr><td colspan="2">备份方式</td></tr> <span> 20</span> <tr><td><input type="radio" name="bfzl" value="quanbubiao"> 备份全部数据</td><td>备份全部数据表中的数据到一个备份文件</td></tr> <span> 21</span> <tr><td><input type="radio" name="bfzl" value="danbiao"><span>备份单张表数据 </span><span> 22</span> <select name="tablename"><option value="">请选择</option> <span> 23</span> <? <span> 24</span> <span>$d</span>->query("show table status from <span>$mysqldb</span>"<span>); </span><span> 25</span> <span>while</span>(<span>$d</span>-><span>nextrecord()){ </span><span> 26</span> <span>echo</span> "<option value='".<span>$d</span>->f('Name')."'>".<span>$d</span>->f('Name')."</option>"<span>;} </span><span> 27</span> ?> <span> 28</span> </select></td><td>备份选中数据表中的数据到单独的备份文件</td></tr> <span> 29</span> <tr><td colspan="2">使用分卷备份</td></tr> <span> 30</span> <tr><td colspan="2"><input type="checkbox" name="fenjuan" value="yes"> <span> 31</span> 分卷备份 <input name="filesize" type="text" size="10">K</td></tr> <span> 32</span> <tr><td colspan="2">选择目标位置</td></tr> <span> 33</span> <tr><td colspan="2"><input type="radio" name="weizhi" value="server" checked>备份到服务器</td></tr><tr <span>class</span>="cells"><td colspan='2'> <input type="radio" name="weizhi" value="localpc"> <span> 34</span> 备份到本地</td></tr> <span> 35</span> <tr><td colspan="2" align='center'><input type="submit" name="act" value="备份"></td></tr> <span> 36</span> </table></form> <span> 37</span> <?<span>/*</span><span>-------------界面结束-------------</span><span>*/</span>}<span>/*</span><span>---------------------------------</span><span>*/</span> <span> 38</span> <span>/*</span><span>----</span><span>*/</span><span>else</span>{<span>/*</span><span>--------------主程序-----------------------------------------</span><span>*/</span> <span> 39</span> <span>if</span>(<span>$_POST</span>['weizhi']=="localpc"&&<span>$_POST</span>['fenjuan']=='yes'<span>) </span><span> 40</span> {<span>$msgs</span>[]="只有选择备份到服务器,才能使用分卷备份功能"<span>; </span><span> 41</span> show_msg(<span>$msgs</span><span>); pageend();} </span><span> 42</span> <span>if</span>(<span>$_POST</span>['fenjuan']=="yes"&&!<span>$_POST</span>['filesize'<span>]) </span><span> 43</span> {<span>$msgs</span>[]="您选择了分卷备份功能,但未填写分卷文件大小"<span>; </span><span> 44</span> show_msg(<span>$msgs</span><span>); pageend();} </span><span> 45</span> <span>if</span>(<span>$_POST</span>['weizhi']=="server"&&!writeable("./backup"<span>)) </span><span> 46</span> {<span>$msgs</span>[]="备份文件存放目录'./backup'不可写,请修改目录属性"<span>; </span><span> 47</span> show_msg(<span>$msgs</span><span>); pageend();} </span><span> 48</span> <span> 49</span> <span>/*</span><span>----------备份全部表-------------</span><span>*/</span><span>if</span>(<span>$_POST</span>['bfzl']=="quanbubiao"){<span>/*</span><span>----</span><span>*/</span> <span> 50</span> <span>/*</span><span>----不分卷</span><span>*/</span><span>if</span>(!<span>$_POST</span>['fenjuan']){<span>/*</span><span>--------------------------------</span><span>*/</span> <span> 51</span> <span>if</span>(!<span>$tables</span>=<span>$d</span>->query("show table status from <span>$mysqldb</span>"<span>)) </span><span> 52</span> {<span>$msgs</span>[]="读数据库结构错误"; show_msg(<span>$msgs</span><span>); pageend();} </span><span> 53</span> <span>$sql</span>=""<span>; </span><span> 54</span> <span>while</span>(<span>$d</span>->nextrecord(<span>$tables</span><span>)) </span><span> 55</span> <span> { </span><span> 56</span> <span>$table</span>=<span>$d</span>->f("Name"<span>); </span><span> 57</span> <span>$sql</span>.=make_header(<span>$table</span><span>); </span><span> 58</span> <span>$d</span>->query("select * from <span>$table</span>"<span>); </span><span> 59</span> <span>$num_fields</span>=<span>$d</span>-><span>nf(); </span><span> 60</span> <span>while</span>(<span>$d</span>-><span>nextrecord()) </span><span> 61</span> {<span>$sql</span>.=make_record(<span>$table</span>,<span>$num_fields</span><span>);} </span><span> 62</span> <span> } </span><span> 63</span> <span>$filename</span>=<span>date</span>("Ymd",<span>time</span>())."_all.sql"<span>; </span><span> 64</span> <span>if</span>(<span>$_POST</span>['weizhi']=="localpc") down_file(<span>$sql</span>,<span>$filename</span><span>); </span><span> 65</span> <span>elseif</span>(<span>$_POST</span>['weizhi']=="server"<span>) </span><span> 66</span> {<span>if</span>(write_file(<span>$sql</span>,<span>$filename</span><span>)) </span><span> 67</span> <span>$msgs</span>[]="全部数据表数据备份完成,生成备份文件'./backup/<span>$filename</span>'"<span>; </span><span> 68</span> <span>else</span> <span>$msgs</span>[]="备份全部数据表失败"<span>; </span><span> 69</span> show_msg(<span>$msgs</span><span>); </span><span> 70</span> <span> pageend(); </span><span> 71</span> <span> } </span><span> 72</span> <span>/*</span><span>-----------------不要卷结束</span><span>*/</span>}<span>/*</span><span>-----------------------</span><span>*/</span> <span> 73</span> <span>/*</span><span>-----------------分卷</span><span>*/</span><span>else</span>{<span>/*</span><span>-------------------------</span><span>*/</span> <span> 74</span> <span>if</span>(!<span>$_POST</span>['filesize'<span>]) </span><span> 75</span> {<span>$msgs</span>[]="请填写备份文件分卷大小"; show_msg(<span>$msgs</span><span>);pageend();} </span><span> 76</span> <span>if</span>(!<span>$tables</span>=<span>$d</span>->query("show table status from <span>$mysqldb</span>"<span>)) </span><span> 77</span> {<span>$msgs</span>[]="读数据库结构错误"; show_msg(<span>$msgs</span><span>); pageend();} </span><span> 78</span> <span>$sql</span>=""; <span>$p</span>=1<span>; </span><span> 79</span> <span>$filename</span>=<span>date</span>("Ymd",<span>time</span>())."_all"<span>; </span><span> 80</span> <span>while</span>(<span>$d</span>->nextrecord(<span>$tables</span><span>)) </span><span> 81</span> <span>{ </span><span> 82</span> <span>$table</span>=<span>$d</span>->f("Name"<span>); </span><span> 83</span> <span>$sql</span>.=make_header(<span>$table</span><span>); </span><span> 84</span> <span>$d</span>->query("select * from <span>$table</span>"<span>); </span><span> 85</span> <span>$num_fields</span>=<span>$d</span>-><span>nf(); </span><span> 86</span> <span>while</span>(<span>$d</span>-><span>nextrecord()) </span><span> 87</span> {<span>$sql</span>.=make_record(<span>$table</span>,<span>$num_fields</span><span>); </span><span> 88</span> <span>if</span>(<span>strlen</span>(<span>$sql</span>)>=<span>$_POST</span>['filesize']*1000<span>){ </span><span> 89</span> <span>$filename</span>.=("_v".<span>$p</span>.".sql"<span>); </span><span> 90</span> <span>if</span>(write_file(<span>$sql</span>,<span>$filename</span><span>)) </span><span> 91</span> <span>$msgs</span>[]="全部数据表-卷-".<span>$p</span>."-数据备份完成,生成备份文件'./backup/<span>$filename</span>'"<span>; </span><span> 92</span> <span>else</span> <span>$msgs</span>[]="备份表-".<span>$_POST</span>['tablename']."-失败"<span>; </span><span> 93</span> <span>$p</span>++<span>; </span><span> 94</span> <span>$filename</span>=<span>date</span>("Ymd",<span>time</span>())."_all"<span>; </span><span> 95</span> <span>$sql</span>=""<span>;} </span><span> 96</span> <span> } </span><span> 97</span> <span>} </span><span> 98</span> <span>if</span>(<span>$sql</span>!=""){<span>$filename</span>.=("_v".<span>$p</span>.".sql"<span>); </span><span> 99</span> <span>if</span>(write_file(<span>$sql</span>,<span>$filename</span><span>)) </span><span>100</span> <span>$msgs</span>[]="全部数据表-卷-".<span>$p</span>."-数据备份完成,生成备份文件'./backup/<span>$filename</span>'"<span>;} </span><span>101</span> show_msg(<span>$msgs</span><span>); </span><span>102</span> <span>/*</span><span>---------------------分卷结束</span><span>*/</span>}<span>/*</span><span>--------------------------------------</span><span>*/</span> <span>103</span> <span>/*</span><span>--------备份全部表结束</span><span>*/</span>}<span>/*</span><span>---------------------------------------------</span><span>*/</span> <span>104</span> <span>105</span> <span>/*</span><span>--------备份单表------</span><span>*/</span><span>elseif</span>(<span>$_POST</span>['bfzl']=="danbiao"){<span>/*</span><span>------------</span><span>*/</span> <span>106</span> <span>if</span>(!<span>$_POST</span>['tablename'<span>]) </span><span>107</span> {<span>$msgs</span>[]="请选择要备份的数据表"; show_msg(<span>$msgs</span><span>); pageend();} </span><span>108</span> <span>/*</span><span>--------不分卷</span><span>*/</span><span>if</span>(!<span>$_POST</span>['fenjuan']){<span>/*</span><span>-------------------------------</span><span>*/</span> <span>109</span> <span>$sql</span>=make_header(<span>$_POST</span>['tablename'<span>]); </span><span>110</span> <span>$d</span>->query("select * from ".<span>$_POST</span>['tablename'<span>]); </span><span>111</span> <span>$num_fields</span>=<span>$d</span>-><span>nf(); </span><span>112</span> <span>while</span>(<span>$d</span>-><span>nextrecord()) </span><span>113</span> {<span>$sql</span>.=make_record(<span>$_POST</span>['tablename'],<span>$num_fields</span><span>);} </span><span>114</span> <span>$filename</span>=<span>date</span>("Ymd",<span>time</span>())."_".<span>$_POST</span>['tablename'].".sql"<span>; </span><span>115</span> <span>if</span>(<span>$_POST</span>['weizhi']=="localpc") down_file(<span>$sql</span>,<span>$filename</span><span>); </span><span>116</span> <span>elseif</span>(<span>$_POST</span>['weizhi']=="server"<span>) </span><span>117</span> {<span>if</span>(write_file(<span>$sql</span>,<span>$filename</span><span>)) </span><span>118</span> <span>$msgs</span>[]="表-".<span>$_POST</span>['tablename']."-数据备份完成,生成备份文件'./backup/<span>$filename</span>'"<span>; </span><span>119</span> <span>else</span> <span>$msgs</span>[]="备份表-".<span>$_POST</span>['tablename']."-失败"<span>; </span><span>120</span> show_msg(<span>$msgs</span><span>); </span><span>121</span> <span> pageend(); </span><span>122</span> <span> } </span><span>123</span> <span>/*</span><span>----------------不要卷结束</span><span>*/</span>}<span>/*</span><span>------------------------------------</span><span>*/</span> <span>124</span> <span>/*</span><span>----------------分卷</span><span>*/</span><span>else</span>{<span>/*</span><span>--------------------------------------</span><span>*/</span> <span>125</span> <span>if</span>(!<span>$_POST</span>['filesize'<span>]) </span><span>126</span> {<span>$msgs</span>[]="请填写备份文件分卷大小"; show_msg(<span>$msgs</span><span>);pageend();} </span><span>127</span> <span>$sql</span>=make_header(<span>$_POST</span>['tablename']); <span>$p</span>=1<span>; </span><span>128</span> <span>$filename</span>=<span>date</span>("Ymd",<span>time</span>())."_".<span>$_POST</span>['tablename'<span>]; </span><span>129</span> <span>$d</span>->query("select * from ".<span>$_POST</span>['tablename'<span>]); </span><span>130</span> <span>$num_fields</span>=<span>$d</span>-><span>nf(); </span><span>131</span> <span>while</span> (<span>$d</span>-><span>nextrecord()) </span><span>132</span> <span> { </span><span>133</span> <span>$sql</span>.=make_record(<span>$_POST</span>['tablename'],<span>$num_fields</span><span>); </span><span>134</span> <span>if</span>(<span>strlen</span>(<span>$sql</span>)>=<span>$_POST</span>['filesize']*1000<span>){ </span><span>135</span> <span>$filename</span>.=("_v".<span>$p</span>.".sql"<span>); </span><span>136</span> <span>if</span>(write_file(<span>$sql</span>,<span>$filename</span><span>)) </span><span>137</span> <span>$msgs</span>[]="表-".<span>$_POST</span>['tablename']."-卷-".<span>$p</span>."-数据备份完成,生成备份文件'./backup/<span>$filename</span>'"<span>; </span><span>138</span> <span>else</span> <span>$msgs</span>[]="备份表-".<span>$_POST</span>['tablename']."-失败"<span>; </span><span>139</span> <span>$p</span>++<span>; </span><span>140</span> <span>$filename</span>=<span>date</span>("Ymd",<span>time</span>())."_".<span>$_POST</span>['tablename'<span>]; </span><span>141</span> <span>$sql</span>=""<span>;} </span><span>142</span> <span> } </span><span>143</span> <span>if</span>(<span>$sql</span>!=""){<span>$filename</span>.=("_v".<span>$p</span>.".sql"<span>); </span><span>144</span> <span>if</span>(write_file(<span>$sql</span>,<span>$filename</span><span>)) </span><span>145</span> <span>$msgs</span>[]="表-".<span>$_POST</span>['tablename']."-卷-".<span>$p</span>."-数据备份完成,生成备份文件'./backup/<span>$filename</span>'"<span>;} </span><span>146</span> show_msg(<span>$msgs</span><span>); </span><span>147</span> <span>/*</span><span>----------分卷结束</span><span>*/</span>}<span>/*</span><span>--------------------------------------------------</span><span>*/</span> <span>148</span> <span>/*</span><span>----------备份单表结束</span><span>*/</span>}<span>/*</span><span>----------------------------------------------</span><span>*/</span> <span>149</span> <span>150</span> <span>/*</span><span>---</span><span>*/</span>}<span>/*</span><span>-------------主程序结束------------------------------------------</span><span>*/</span> <span>151</span> <span>152</span> <span>function</span> write_file(<span>$sql</span>,<span>$filename</span><span>) </span><span>153</span> <span>{ </span><span>154</span> <span>$re</span>=<span>true</span><span>; </span><span>155</span> <span>if</span>(!@<span>$fp</span>=<span>fopen</span>("./backup/".<span>$filename</span>,"w+")) {<span>$re</span>=<span>false</span>; <span>echo</span> "failed to open target file"<span>;} </span><span>156</span> <span>if</span>(!@<span>fwrite</span>(<span>$fp</span>,<span>$sql</span>)) {<span>$re</span>=<span>false</span>; <span>echo</span> "failed to write file"<span>;} </span><span>157</span> <span>if</span>(!@<span>fclose</span>(<span>$fp</span>)) {<span>$re</span>=<span>false</span>; <span>echo</span> "failed to close target file"<span>;} </span><span>158</span> <span>return</span> <span>$re</span><span>; </span><span>159</span> <span>} </span><span>160</span> <span>161</span> <span>function</span> down_file(<span>$sql</span>,<span>$filename</span><span>) </span><span>162</span> <span>{ </span><span>163</span> <span>ob_end_clean</span><span>(); </span><span>164</span> <span>header</span>("Content-Encoding: none"<span>); </span><span>165</span> <span>header</span>("Content-Type: ".(<span>strpos</span>(<span>$_SERVER</span>['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream'<span>)); </span><span>166</span> <span>167</span> <span>header</span>("Content-Disposition: ".(<span>strpos</span>(<span>$_SERVER</span>['HTTP_USER_AGENT'], 'MSIE') ? 'inline; ' : 'attachment; ')."filename=".<span>$filename</span><span>); </span><span>168</span> <span>169</span> <span>header</span>("Content-Length: ".<span>strlen</span>(<span>$sql</span><span>)); </span><span>170</span> <span>header</span>("Pragma: no-cache"<span>); </span><span>171</span> <span>172</span> <span>header</span>("Expires: 0"<span>); </span><span>173</span> <span>echo</span> <span>$sql</span><span>; </span><span>174</span> <span>$e</span>=<span>ob_get_contents</span><span>(); </span><span>175</span> <span>ob_end_clean</span><span>(); </span><span>176</span> <span>} </span><span>177</span> <span>178</span> <span>function</span> writeable(<span>$dir</span><span>) </span><span>179</span> <span>{ </span><span>180</span> <span>181</span> <span>if</span>(!<span>is_dir</span>(<span>$dir</span><span>)) { </span><span>182</span> @<span>mkdir</span>(<span>$dir</span>, 0777<span>); </span><span>183</span> <span> } </span><span>184</span> <span>185</span> <span>if</span>(<span>is_dir</span>(<span>$dir</span><span>)) </span><span>186</span> <span> { </span><span>187</span> <span>188</span> <span>if</span>(<span>$fp</span> = @<span>fopen</span>("<span>$dir</span>/test.test", 'w'<span>)) </span><span>189</span> <span> { </span><span>190</span> @<span>fclose</span>(<span>$fp</span><span>); </span><span>191</span> @<span>unlink</span>("<span>$dir</span>/test.test"<span>); </span><span>192</span> <span>$writeable</span> = 1<span>; </span><span>193</span> <span>} </span><span>194</span> <span>else</span><span> { </span><span>195</span> <span>$writeable</span> = 0<span>; </span><span>196</span> <span> } </span><span>197</span> <span>198</span> <span>} </span><span>199</span> <span>200</span> <span>return</span> <span>$writeable</span><span>; </span><span>201</span> <span>202</span> <span>} </span><span>203</span> <span>204</span> <span>function</span> make_header(<span>$table</span><span>) </span><span>205</span> {<span>global</span> <span>$d</span><span>; </span><span>206</span> <span>$sql</span>="DROP TABLE IF EXISTS ".<span>$table</span>."\n"<span>; </span><span>207</span> <span>$d</span>->query("show create table ".<span>$table</span><span>); </span><span>208</span> <span>$d</span>-><span>nextrecord(); </span><span>209</span> <span>$tmp</span>=<span>preg_replace</span>("/\n/","",<span>$d</span>->f("Create Table"<span>)); </span><span>210</span> <span>$sql</span>.=<span>$tmp</span>."\n"<span>; </span><span>211</span> <span>return</span> <span>$sql</span><span>; </span><span>212</span> <span>} </span><span>213</span> <span>214</span> <span>function</span> make_record(<span>$table</span>,<span>$num_fields</span><span>) </span><span>215</span> {<span>global</span> <span>$d</span><span>; </span><span>216</span> <span>$comma</span>=""<span>; </span><span>217</span> <span>$sql</span> .= "INSERT INTO ".<span>$table</span>." VALUES("<span>; </span><span>218</span> <span>for</span>(<span>$i</span> = 0; <span>$i</span> < <span>$num_fields</span>; <span>$i</span>++<span>) </span><span>219</span> {<span>$sql</span> .= (<span>$comma</span>."'".<span>mysql_escape_string</span>(<span>$d</span>->record[<span>$i</span>])."'"); <span>$comma</span> = ","<span>;} </span><span>220</span> <span>$sql</span> .= ")\n"<span>; </span><span>221</span> <span>return</span> <span>$sql</span><span>; </span><span>222</span> <span>} </span><span>223</span> <span>224</span> <span>function</span> show_msg(<span>$msgs</span><span>) </span><span>225</span> <span>{ </span><span>226</span> <span>$title</span>="提示:"<span>; </span><span>227</span> <span>echo</span> "<table width='100%' border='1' cellpadding='0' cellspacing='1'>"<span>; </span><span>228</span> <span>echo</span> "<tr><td>".<span>$title</span>."</td></tr>"<span>; </span><span>229</span> <span>echo</span> "<tr><td><br><ul>"<span>; </span><span>230</span> <span>while</span> (<span>list</span>(<span>$k</span>,<span>$v</span>)=<span>each</span>(<span>$msgs</span><span>)) </span><span>231</span> <span> { </span><span>232</span> <span>echo</span> "<li>".<span>$v</span>."</li>"<span>; </span><span>233</span> <span> } </span><span>234</span> <span>echo</span> "</ul></td></tr></table>"<span>; </span><span>235</span> <span>} </span><span>236</span> <span>237</span> <span>function</span><span> pageend() </span><span>238</span> <span>{ </span><span>239</span> <span>exit</span><span>(); </span><span>240</span> <span>} </span><span>241</span> ?>
restore.php
<? <span>session_start</span><span>(); </span><span>global</span> <span>$mysqlhost</span>, <span>$mysqluser</span>, <span>$mysqlpwd</span>, <span>$mysqldb</span><span>; </span><span>$mysqlhost</span>=""; <span>//</span><span>host name</span> <span>$mysqluser</span>=""; <span>//</span><span>login name</span> <span>$mysqlpwd</span>=""; <span>//</span><span>password</span> <span>$mysqldb</span>=""; <span>//</span><span>name of database</span> <span>include</span>("mydb.php"<span>); </span><span>$d</span>=<span>new</span> db(<span>$mysqlhost</span>,<span>$mysqluser</span>,<span>$mysqlpwd</span>,<span>$mysqldb</span><span>); </span><span>/*</span><span>*****界面</span><span>*/</span><span>if</span>(!<span>$_POST</span>['act']&&!<span>$_SESSION</span>['data_file']){<span>/*</span><span>********************</span><span>*/</span> <span>$msgs</span>[]="本功能在恢复备份数据的同时,将全部覆盖原有数据,请确定是否需要恢复,以免造成数据损失"<span>; </span><span>$msgs</span>[]="数据恢复功能只能恢复由dShop导出的数据文件,其他软件导出格式可能无法识别"<span>; </span><span>$msgs</span>[]="从本地恢复数据需要服务器支持文件上传并保证数据尺寸小于允许上传的上限,否则只能使用从服务器恢复"<span>; </span><span>$msgs</span>[]="如果您使用了分卷备份,只需手工导入文件卷1,其他数据文件会由系统自动导入"<span>; show_msg(</span><span>$msgs</span><span>); </span>?> <form action="" method="post" enctype="multipart/form-data" name="restore.php"> <table width="91%" border="0" cellpadding="0" cellspacing="1"> <tr align="center" <span>class</span>="header"><td colspan="2" align="center">数据恢复</td></tr> <tr><td width="33%"><input type="radio" name="restorefrom" value="server" checked><span> 从服务器文件恢复 </span></td><td width="67%"><select name="serverfile"> <option value="">-请选择-</option> <? <span>$handle</span>=<span>opendir</span>('./backup'<span>); </span><span>while</span> (<span>$file</span> = <span>readdir</span>(<span>$handle</span><span>)) { </span><span>if</span>(<span>eregi</span>("^[0-9]{8,8}([0-9a-z_]+)(\.sql)$",<span>$file</span>)) <span>echo</span> "<option value='<span>$file</span>'><span>$file</span></option>"<span>;} </span><span>closedir</span>(<span>$handle</span><span>); </span>?> </select> </td></tr> <tr><td><input type="radio" name="restorefrom" value="localpc"> 从本地文件恢复</td> <td><input type="hidden" name="MAX_FILE_SIZE" value="1500000"><input type="file" name="myfile"></td></tr> <tr><td colspan="2" align="center"> <input type="submit" name="act" value="恢复"></td> </tr></table></form> <?<span>/*</span><span>*************************界面结束</span><span>*/</span>}<span>/*</span><span>***********************************</span><span>*/</span> <span>/*</span><span>***************************主程序</span><span>*/</span><span>if</span>(<span>$_POST</span>['act']=="恢复"){<span>/*</span><span>************</span><span>*/</span> <span>/*</span><span>**************服务器恢复</span><span>*/</span><span>if</span>(<span>$_POST</span>['restorefrom']=="server"){<span>/*</span><span>************</span><span>*/</span> <span>if</span>(!<span>$_POST</span>['serverfile'<span>]) {</span><span>$msgs</span>[]="您选择从服务器文件恢复备份,但没有指定备份文件"<span>; show_msg(</span><span>$msgs</span><span>); pageend(); } </span><span>if</span>(!<span>eregi</span>("_v[0-9]+",<span>$_POST</span>['serverfile'<span>])) {</span><span>$filename</span>="./backup/".<span>$_POST</span>['serverfile'<span>]; </span><span>if</span>(import(<span>$filename</span>)) <span>$msgs</span>[]="备份文件".<span>$_POST</span>['serverfile']."成功导入数据库"<span>; </span><span>else</span> <span>$msgs</span>[]="备份文件".<span>$_POST</span>['serverfile']."导入失败"<span>; show_msg(</span><span>$msgs</span><span>); pageend(); } </span><span>else</span><span> { </span><span>$filename</span>="./backup/".<span>$_POST</span>['serverfile'<span>]; </span><span>if</span>(import(<span>$filename</span>)) <span>$msgs</span>[]="备份文件".<span>$_POST</span>['serverfile']."成功导入数据库"<span>; </span><span>else</span> {<span>$msgs</span>[]="备份文件".<span>$_POST</span>['serverfile']."导入失败";show_msg(<span>$msgs</span><span>);pageend();} </span><span>$voltmp</span>=<span>explode</span>("_v",<span>$_POST</span>['serverfile'<span>]); </span><span>$volname</span>=<span>$voltmp</span>[0<span>]; </span><span>$volnum</span>=<span>explode</span>(".sq",<span>$voltmp</span>[1<span>]); </span><span>$volnum</span>=<span>intval</span>(<span>$volnum</span>[0])+1<span>; </span><span>$tmpfile</span>=<span>$volname</span>."_v".<span>$volnum</span>.".sql"<span>; </span><span>if</span>(<span>file_exists</span>("./backup/".<span>$tmpfile</span><span>)) { </span><span>$msgs</span>[]="程序将在3秒钟后自动开始导入此分卷备份的下一部份:文件".<span>$tmpfile</span>.",请勿手动中止程序的运行,以免数据库结构受损"<span>; </span><span>$_SESSION</span>['data_file']=<span>$tmpfile</span><span>; show_msg(</span><span>$msgs</span><span>); </span><span>sleep</span>(3<span>); </span><span>echo</span> "<script language='javascript'>"<span>; </span><span>echo</span> "location='restore.php';"<span>; </span><span>echo</span> "</script>"<span>; } </span><span>else</span><span> { </span><span>$msgs</span>[]="此分卷备份全部导入成功"<span>; show_msg(</span><span>$msgs</span><span>); } } </span><span>/*</span><span>*************服务器恢复结束</span><span>*/</span>}<span>/*</span><span>******************************************</span><span>*/</span> <span>/*</span><span>****************本地恢复</span><span>*/</span><span>if</span>(<span>$_POST</span>['restorefrom']=="localpc"){<span>/*</span><span>************</span><span>*/</span> <span>switch</span> (<span>$_FILES</span>['myfile']['error'<span>]) { </span><span>case</span> 1: <span>case</span> 2: <span>$msgs</span>[]="您上传的文件大于服务器限定值,上传未成功"<span>; </span><span>break</span><span>; </span><span>case</span> 3: <span>$msgs</span>[]="未能从本地完整上传备份文件"<span>; </span><span>break</span><span>; </span><span>case</span> 4: <span>$msgs</span>[]="从本地上传备份文件失败"<span>; </span><span>break</span><span>; </span><span>case</span> 0: <span>break</span><span>; } </span><span>if</span>(<span>$msgs</span>){show_msg(<span>$msgs</span><span>);pageend();} </span><span>$fname</span>=<span>date</span>("Ymd",<span>time</span>())."_".sjs(5).".sql"<span>; </span><span>if</span> (<span>is_uploaded_file</span>(<span>$_FILES</span>['myfile']['tmp_name'<span>])) { </span><span>copy</span>(<span>$_FILES</span>['myfile']['tmp_name'], "./backup/".<span>$fname</span><span>);} </span><span>if</span> (<span>file_exists</span>("./backup/".<span>$fname</span><span>)) { </span><span>$msgs</span>[]="本地备份文件上传成功"<span>; </span><span>if</span>(import("./backup/".<span>$fname</span>)) {<span>$msgs</span>[]="本地备份文件成功导入数据库"; <span>unlink</span>("./backup/".<span>$fname</span><span>);} </span><span>else</span> <span>$msgs</span>[]="本地备份文件导入数据库失败"<span>; } </span><span>else</span> (<span>$msgs</span>[]="从本地上传备份文件失败"<span>); show_msg(</span><span>$msgs</span><span>); </span><span>/*</span><span>***本地恢复结束****</span><span>*/</span>}<span>/*</span><span>**************************************************</span><span>*/</span> <span>/*</span><span>***************************主程序结束</span><span>*/</span>}<span>/*</span><span>********************************</span><span>*/</span> <span>/*</span><span>************************剩余分卷备份恢复*********************************</span><span>*/</span> <span>if</span>(!<span>$_POST</span>['act']&&<span>$_SESSION</span>['data_file'<span>]) { </span><span>$filename</span>="./backup/".<span>$_SESSION</span>['data_file'<span>]; </span><span>if</span>(import(<span>$filename</span>)) <span>$msgs</span>[]="备份文件".<span>$_SESSION</span>['data_file']."成功导入数据库"<span>; </span><span>else</span> {<span>$msgs</span>[]="备份文件".<span>$_SESSION</span>['data_file']."导入失败";show_msg(<span>$msgs</span><span>);pageend();} </span><span>$voltmp</span>=<span>explode</span>("_v",<span>$_SESSION</span>['data_file'<span>]); </span><span>$volname</span>=<span>$voltmp</span>[0<span>]; </span><span>$volnum</span>=<span>explode</span>(".sq",<span>$voltmp</span>[1<span>]); </span><span>$volnum</span>=<span>intval</span>(<span>$volnum</span>[0])+1<span>; </span><span>$tmpfile</span>=<span>$volname</span>."_v".<span>$volnum</span>.".sql"<span>; </span><span>if</span>(<span>file_exists</span>("./backup/".<span>$tmpfile</span><span>)) { </span><span>$msgs</span>[]="程序将在3秒钟后自动开始导入此分卷备份的下一部份:文件".<span>$tmpfile</span>.",请勿手动中止程序的运行,以免数据库结构受损"<span>; </span><span>$_SESSION</span>['data_file']=<span>$tmpfile</span><span>; show_msg(</span><span>$msgs</span><span>); </span><span>sleep</span>(3<span>); </span><span>echo</span> "<script language='javascript'>"<span>; </span><span>echo</span> "location='restore.php';"<span>; </span><span>echo</span> "</script>"<span>; } </span><span>else</span><span> { </span><span>$msgs</span>[]="此分卷备份全部导入成功"<span>; </span><span>unset</span>(<span>$_SESSION</span>['data_file'<span>]); show_msg(</span><span>$msgs</span><span>); } } </span><span>/*</span><span>*********************剩余分卷备份恢复结束******************************</span><span>*/</span> <span>function</span> import(<span>$fname</span><span>) {</span><span>global</span> <span>$d</span><span>; </span><span>$sqls</span>=<span>file</span>(<span>$fname</span><span>); </span><span>foreach</span>(<span>$sqls</span> <span>as</span> <span>$sql</span><span>) { </span><span>str_replace</span>("\r","",<span>$sql</span><span>); </span><span>str_replace</span>("\n","",<span>$sql</span><span>); </span><span>if</span>(!<span>$d</span>->query(<span>trim</span>(<span>$sql</span>))) <span>return</span> <span>false</span><span>; } </span><span>return</span> <span>true</span><span>; } </span><span>function</span> show_msg(<span>$msgs</span><span>) { </span><span>$title</span>="提示:"<span>; </span><span>echo</span> "<table width='100%' border='1' cellpadding='0' cellspacing='1'>"<span>; </span><span>echo</span> "<tr><td>".<span>$title</span>."</td></tr>"<span>; </span><span>echo</span> "<tr><td><br><ul>"<span>; </span><span>while</span> (<span>list</span>(<span>$k</span>,<span>$v</span>)=<span>each</span>(<span>$msgs</span><span>)) { </span><span>echo</span> "<li>".<span>$v</span>."</li>"<span>; } </span><span>echo</span> "</ul></td></tr></table>"<span>; } </span><span>function</span><span> pageend() { </span><span>exit</span><span>(); } </span>?>
有很多软件可以使用,比如phpmyadmin,sqlyog等等
下载一个phpmyadmin并且配置好(网上有如何配置),其中就有备份还原数据库的图标,很简单
补充:----------------------
对啊,点导出,然后执行就可以了啊
能生成文件但是内容为空,说明:php执行没问题,mysqldump也运行,初步判断问题出在mysqldump没正常运行。建议你到服务器上运行 "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump -uroot -hlocalhost -p123 --opt -B rsgl > ../bak/xxx.sql"
看能否正常生成sql文件