windows PHP写的自动备份网站和MYSQL数据

  1. <?php  
  2. /*********************************************** 
  3. web_backup.php 
  4. ************************************************ 
  5. 说明 
  6. @author 杨军平 
  7. @date    2007年10月 
  8. 针对目前我中心需要承担网站维护工作, 
  9. 但是手工备份及其繁琐效率不高,因此 
  10. 在前人的基础上进行了扩展,实现自动备份 
  11. ************************************************** 
  12. 用法 
  13. 我的php及mysql安装目录为D:\phpstudy\php5 
  14. D:\phpstudy\php5\mysql 
  15. 在D:\phpstudy\php5 
  16. 新建目录winrar,拷贝C:\program files\winrar 
  17. 中winrar.exe 和Rarreg.key到新建的winrar目录中 
  18.  
  19. 在D:\phpstudy\php5 中新建webback.bat文件内容如下 
  20. ************************************************** 
  21. @echo off 
  22. php.exe web_backup.php 
  23. if exist webback_temp.bat call webback_temp.bat 
  24. ftp -i -s:ftp.src 
  25. *************************************************** 
  26. 在windows中新建计划任务定时调用webback.bat 
  27. ***************************************************/  
  28. //ftp配置文件  
  29. $host='111.222.111.222';  
  30. $username="jszx";  
  31. $password="jszxpass";  
  32. $dest="112";  
  33. //数据库配置  
  34. $db_username = "root";   
  35. $db_password = "weoffice";   
  36. //保存目录,路径要用反斜杠.您需要手动建立它.   
  37. $store_folder = 'E:\mysql_back_day_by_day';   
  38. //网站目录  
  39. //$olddir='d:\cache';  
  40.   
  41. $time=time();   
  42. $mysqldir"$store_folder\\"."mysql".date("Ymd",$time)."";  
  43. //$webdir = "$store_folder\\"."web".date("Ymd",$time)."";  
  44. //if(file_exists("$webdir.rar")) die("File exists.\r\n");  
  45. if(file_exists("$mysqldir")) die("File exists.\r\n"); 
  46. //创建mysql备份目录 
  47. @mkdir($mysqldir); 
  48. //生成fpt.src文件 配置ftp 自动上传 
  49. $ftpcommand=''; 
  50. $ftpcommand.="open $host \r\n"; 
  51. $ftpcommand.="$username \r\n"; 
  52. $ftpcommand.="$password \r\n"; 
  53. $ftpcommand.="cd $dest \r\n"; 
  54. //$ftpcommand.="mput $webdir.rar \r\n"; 
  55. $ftpcommand.="mput $mysqldir.rar \r\n"; 
  56. $ftpcommand.="quit \r\n";  
  57. $ftpcommand.="del ftp.src"; 
  58. $fp = fopen('ftp.src','w');  
  59. fwrite($fp,$ftpcommand);  
  60. fclose($fp); 
  61. //创建备份mysql命令 
  62. mysql_connect("localhost","$db_username","$db_password");  
  63. $query=mysql_list_dbs();  
  64. $command = '';  
  65. while($result=mysql_fetch_array($query)){  
  66.   $command .= dirname(__FILE__).'\..\mysql\bin\mysqldump --opt '."$result[Database] -u{$db_username".($db_password?"-p{$db_password}":"")." > $mysqldir\\$result[Database].sql \r\n";  
  67.   $command .= "echo dumping database `$result[Database]`... \r\n";  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.   $command .= "echo Winrar loading...\r\n";  
  74.  
  75.  //压缩mysql备份 
  76. $command .= dirname(__FILE__)."\\WinRAR\\WinRAR.exe a -ep1 -r -o+ -m5 -df \"$mysqldir.rar\" \"$mysqldir\" \r\n";   
  77. //压缩web目录  
  78. //$command .= dirname(__FILE__)."\\WinRAR\\WinRAR a -r  \"$webdir\" \"$olddir\" \r\n";   
  79.   
  80. $command .= "echo OK!\r\n";   
  81.   
  82. $command .= "del webback_temp.bat\r\n";   
  83.   
  84. $fp = fopen('webback_temp.bat','w');   
  85. fwrite($fp,$command);   
  86. fclose($fp);   
  87.   
  88.   
  89. //删除 5 天前的文件,当然也可以考虑保留更多天的备份   
  90. @unlink("$store_folder\\"."mysql".date("Ymd",$time-86400*5).".rar");  
  91. @unlink("$store_folder\\"."web".date("Ymd",$time-86400*5).".rar");   
  92.   
  93. ?> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北方的刀郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值