php,mysql 进行数据库备份

<?php
header("content-type:text/html;charset=utf-8");
// 设置SQL文件保存文件名 
  $cfg_dbname = 'db_name';//数据库名
  $filename = date("Ymd",time())."_".$cfg_dbname.".sql"; //导出要备份的sql文件名


  // 获取当前页面文件路径,SQL文件就导出到指定文件夹内
  // $savePath = './Public/upload/DB/';
  $savePath = 'D:/phpstudy/www/project/DB/';


  if(!file_exists($savePath)){
    mkdir($savePath,0777,true);
  }
  $tmpFile = $savePath.$filename;
  fopen($tmpFile, "r");
  chmod($tmpFile,0777);
  $cfg_dbuser = 'username';//用户名
    $cfg_dbpwd = 'password';//密码
    exec("D:/phpstudy/MySQL/bin/mysqldump.exe -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > $tmpFile ");
  $nowDate = date('Ymd',strtotime('-1 week'));//一周前
  $delFile = $savePath.$nowDate.'_'.$cfg_dbname.'.sql';
  unlink($delFile);


?>

猜你喜欢

转载自blog.csdn.net/lfbin5566/article/details/79708291