php 定时备份mysql数据库 并删除8天前的记录

1.安装php执行环境 网上百度php即可,或者下载

2.写php执行文件 保存为backup.php

<?php 

if ($argc != 2 || in_array($argv[1], array('--help', '-?'))) { 
?> 
backup  Ver 0.01, for Win95/Win98/WinNT/Win2000/WinXP on i32 
Copyright (C) 2000 ptker All rights reserved. 
This is free software,and you are welcome to modify and redistribute it 
under the GPL license 


PHP Shell script for the backup MySQL database. 


Usage: <?php echo $argv[0]; ?> <option> 


  <option> can be database name you would like to backup. 
  With the --help, or -? options, you can get this help and exit. 
<?php 
} else { 
    $dbname = $argv[1]; 
$dburi =$dbname.date("Y-m-d-H-i-s");
echo $dburi;
    $dump_tool = "C:\\xampp\\mysql\\bin\\mysqldump"; 
    $rar_tool = "c:\\php\\rar"; 
    @exec("$dump_tool --opt -u root -p -h127.0.0.1  $dbname > C:/php/aa/$dburi.sql"); 
del_dir("C:/php/aa");
    //@exec("$rar_tool a -ag_yyyy_mm_dd_hh_mm $dbname.rar $dbname.sql"); 
    //@unlink("$dbname.sql"); 
    echo "Backup complete!"; 

?>


<?php
function del_dir($dir){ //删除目录
if(!($mydir=@dir($dir))){
return;
}
while($file=$mydir->read()){
echo $file;
if(is_dir("$dir$file") && $file!='.' && $file!='..'){ 
//@chmod("$dir$file", 0777);
//del_dir("$dir$file"); 
}elseif(is_file("$dir/$file")){
$file_time=stat("$dir/$file"); //读取文件的最后更新时间
if(time()-$file_time['ctime']>3600*24*8){
if($file != '.'&& $file!='..'&& $file !="")
{
@chmod("$dir/$file", 0777);
@unlink("$dir/$file");
}
}
}
}
$mydir->close();
}

?>

3.设定定时执行任务 c:\php\php.exe   -q   c:\php\backup.php  busiman

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值