php,mysql 進行數據庫備份

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);


?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章