PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/DoYouHaoBaby/LibPHP/App/Package/Util/Backup.class.php

http://dyhb-frame.googlecode.com/
PHP | 259 lines | 241 code | 16 blank | 2 comment | 31 complexity | c873d54e63c0e54de33e1d797c6d113c MD5 | raw file
  1. <?php
  2. /* [DoYouHaoBaby!] (C)Dianniu From 2010.
  3. ??????($) */
  4. class Backup{
  5. protected $_nMaxSize=2097152; // 2M
  6. protected $_bIsShort=false;
  7. protected $_nOffset=300;
  8. protected $_sDumpSql='';
  9. protected $_nSqlNum=0;
  10. protected $_sErrorMessage='';
  11. protected $_oDbConnect;
  12. protected $_sDbChar='utf8';
  13. public function __construct(&$oDbConnect,$nMaxSize=0,$sDbChar='utf8'){
  14. $this->_oDbConnect=$oDbConnect;
  15. if($nMaxSize>0){
  16. $this->_nMaxSize=$nMaxSize;
  17. }
  18. if($sDbChar){
  19. $this->_sDbChar=$sDbChar;
  20. }
  21. }
  22. public function setIsShort($bIsShort=true){
  23. $this->_bIsShort=$bIsShort;
  24. }
  25. public function setMaxSize($nMaxSize){
  26. $this->_nMaxSize=$nMaxSize;
  27. }
  28. public function getDumpSql(){
  29. return $this->_sDumpSql;
  30. }
  31. public function getTableDf($sTable,$bAddDrop=false){
  32. if ($bAddDrop){
  33. $sTableDf="DROP TABLE IF EXISTS `$sTable`;\r\n";
  34. }
  35. else{
  36. $sTableDf='';
  37. }
  38. $arrTemp=$this->_oDbConnect->getRow("SHOW CREATE TABLE `{$sTable}`",null,false);
  39. $sTmpSql=$arrTemp[ 'Create Table' ];
  40. $sTmpSql=substr($sTmpSql,0,strrpos($sTmpSql,")") + 1); //???????
  41. if($this->_oDbConnect->getVersion()>='4.1'){
  42. $sTableDf.=$sTmpSql." ENGINE=MyISAM DEFAULT CHARSET=".str_replace('-','',$this->_sDbChar).";\r\n";
  43. }
  44. else{
  45. $sTableDf.=$sTmpSql. " TYPE=MyISAM;\r\n";
  46. }
  47. return $sTableDf;
  48. }
  49. public function getTableData($sTable,$nPos){
  50. $nPostPos=$nPos;
  51. $this->_oDbConnect->query("SELECT COUNT(*) FROM {$sTable}");// ?????????
  52. $nTotal=$this->_oDbConnect->getNumRows();
  53. if($nTotal==0 || $nPos>=$nTotal){// ????
  54. return -1;
  55. }
  56. $nCycleTime=ceil(($nTotal-$nPos)/$this->_nOffset); //??????&???offset?????????
  57. for($nI=0;$nI<$nCycleTime;$nI++){// ??????
  58. $arrData=$this->_oDbConnect->getAllRows("SELECT * FROM {$sTable} LIMIT " . ($this->_nOffset * $nI + $nPos) . ','.$this->_nOffset);// ???????
  59. $nDataCount=count($arrData);
  60. if(!isset($arrData[0])) continue;// ???????????
  61. $arrFields=array_keys($arrData[0]);
  62. $sStartSql="INSERT INTO `{$sTable}` (`".implode("`,`",$arrFields)."`) VALUES ";
  63. for($nJ=0;$nJ<$nDataCount;$nJ++){// ???????
  64. $sRecord=$this->_oDbConnect->qualifyStr($arrData[$nJ]);//??????
  65. $sRecord=$this->_oDbConnect->dumpNullString($sRecord); //??null?
  66. if($this->_bIsShort){// ????????????
  67. if($nPostPos==$nTotal-1){
  68. $sTmpDumpSql=" (".implode(",",$sRecord).")".($nDataCount==1?';':',')."\r\n";
  69. }
  70. else{
  71. if($nJ==$nDataCount-1){
  72. $sTmpDumpSql="(".implode(",",$sRecord).");\r\n";
  73. }
  74. else{
  75. $sTmpDumpSql="(".implode(",",$sRecord)."),\r\n";
  76. }
  77. }
  78. if($nPostPos==$nPos){// ???????
  79. $sTmpDumpSql=$sStartSql."\r\n".$sTmpDumpSql;
  80. }
  81. else{
  82. if($nJ==0){
  83. $sTmpDumpSql=$sStartSql."\r\n".$sTmpDumpSql;
  84. }
  85. }
  86. }
  87. else{
  88. $sTmpDumpSql=$sStartSql."(".implode(",",$sRecord).");\r\n";
  89. }
  90. if(strlen($this->_sDumpSql)+strlen($sTmpDumpSql)>$this->_nMaxSize-32){
  91. if($this->_nSqlNum==0){
  92. $this->_sDumpSql.=$sTmpDumpSql;//????????????
  93. $this->_nSqlNum++;
  94. $nPostPos++;
  95. if($nPostPos==$nTotal){// ????????
  96. return -1;
  97. }
  98. }
  99. return $nPostPos;
  100. }
  101. else{
  102. $this->_sDumpSql.=$sTmpDumpSql;
  103. $this->_nSqlNum++;//??sql??
  104. $nPostPos++;
  105. }
  106. }
  107. }
  108. return -1;// ????????
  109. }
  110. public function dumpTable($sPath,$nVol){
  111. $arrTables=$this->getTablesList($sPath);
  112. if($arrTables===false){
  113. return false;
  114. }
  115. if(empty($arrTables)){
  116. return $arrTables;
  117. }
  118. $this->_sDumpSql=$this->makeHead($nVol);
  119. foreach($arrTables as $sTable=> $nPos){
  120. if($nPos==-1){
  121. $sTableDf=$this->getTableDf($sTable,true);// ?????????????????
  122. if(strlen($this->_sDumpSql)+strlen($sTableDf)>$this->_nMaxSize-32){
  123. if($this->_nSqlNum==0){// ??????????
  124. $this->_sDumpSql.=$sTableDf;
  125. $this->_nSqlNum +=2;
  126. $arrTables[ $sTable ]=0;
  127. }
  128. break;
  129. }
  130. else{// ??????
  131. $this->_sDumpSql.=$sTableDf;
  132. $this->_nSqlNum +=2;
  133. $nPos=0;
  134. }
  135. }
  136. $nPostPos=$this->getTableData($sTable,$nPos);// ???????????
  137. if($nPostPos==-1){// ???????????
  138. unset($arrTables[ $sTable ]);
  139. }
  140. else{// ??????????????,????????
  141. $arrTables[ $sTable ]=$nPostPos;
  142. break;
  143. }
  144. }
  145. $this->_sDumpSql.='-- DoYouHaoBaby Database Backup Program';
  146. $this->putTablesList($sPath,$arrTables);
  147. return $arrTables;
  148. }
  149. public function makeHead($nVol){
  150. $arrSysInfo['os']=PHP_OS;// ????
  151. $arrSysInfo['web_server']=DYHB_PATH;
  152. $arrSysInfo['php_ver']=PHP_VERSION;
  153. $arrSysInfo['database_ver']=$this->_oDbConnect->getVersion();
  154. $arrSysInfo['date']=date('Y-m-d H:i:s');
  155. $sHead="-- DoYouHaoBaby Database Backup Program\r\n".
  156. "-- " .$arrSysInfo['web_server']."\r\n".
  157. "-- \r\n".
  158. "-- OS : ".$arrSysInfo["os"]."\r\n".
  159. "-- DATE : ".$arrSysInfo["date"]."\r\n".
  160. "-- DATABASE SERVER VERSION : ".$arrSysInfo['database_ver']."\r\n".
  161. "-- PHP VERSION : ".$arrSysInfo['php_ver']."\r\n".
  162. "-- Vol : ".$nVol."\r\n";
  163. return $sHead;
  164. }
  165. static public function getHead($sPath){
  166. $sSqlInfo=array('os'=>'','date'=>'','ver'=> '','php_ver'=>0,'vol'=>0,'database_ver'=>0);// ??Sql??????
  167. $hFp=fopen($sPath,'rb');
  168. $sStr=fread($hFp,250);
  169. fclose($hFp);
  170. $arrInfo=explode("\n",$sStr);
  171. foreach($arrInfo as $sVal){
  172. $nPos=strpos($sVal,':');
  173. if($nPos>0){
  174. $sType=trim(substr($sVal,0,$nPos),"-\n\r\t ");
  175. $sValue=trim(substr($sVal,$nPos+1),"/\n\r\t ");
  176. if($sType=='DATE'){
  177. $arrInfo['date']=$sValue;
  178. }
  179. elseif($sType=='DATABASE SERVER VERSION'){
  180. $arrInfo['database_ver']=$sValue;
  181. }
  182. elseif($sType=='PHP VERSION'){
  183. $arrInfo['php_ver']=$sValue;
  184. }
  185. elseif($sType=='Vol'){
  186. $arrInfo['vol']=$sValue;
  187. }
  188. elseif($sType=='OS'){
  189. $arrInfo['os']=$sValue;
  190. }
  191. }
  192. }
  193. return $arrInfo;
  194. }
  195. public function getTablesList($sPath){
  196. if(!file_exists($sPath)){
  197. $this->_sErrorMessage=sprintf('%s is not exists !',$sPath);
  198. return false;
  199. }
  200. $arrData=array();
  201. $sStr=@file_get_contents($sPath);
  202. if(!empty($sStr)){
  203. $arrTmp=explode("\n",$sStr);
  204. foreach($arrTmp as $sVal){
  205. $sVal=trim($sVal,"\r;");
  206. if(!empty($sVal)){
  207. list($sTable,$nCount)=explode(':',$sVal);
  208. $arrData[$sTable]=$nCount;
  209. }
  210. }
  211. }
  212. return $arrData;
  213. }
  214. public function putTablesList($sPath,$arrData){
  215. if(is_array($arrData)){
  216. $sStr='';
  217. foreach($arrData as $sKey=> $sVal){
  218. $sStr.=$sKey.':'.$sVal.";\r\n";
  219. }
  220. if(@file_put_contents($sPath,$sStr)){
  221. return true;
  222. }
  223. else{
  224. $this->_sErrorMessage=sprintf('Can not write %s',$sPath);
  225. return false;
  226. }
  227. }
  228. else{
  229. $this->_sErrorMessage='Variable $arrData can only be an array!';
  230. return false;
  231. }
  232. }
  233. static public function getRandomName(){
  234. $sStr=date('Ymd');
  235. for($nI=0;$nI<6;$nI++){
  236. $sStr.=chr(mt_rand(97,122));
  237. }
  238. return $sStr;
  239. }
  240. public function getErrorMessage(){
  241. return $this->_sErrorMessage;
  242. }
  243. }