php备份mysql 数据库

1、新建php文

<?php
header
('Content-Type:text/html;charset=utf8'); ini_set("max_execution_time", "0");//代码运行时间不限制 防止备份失败 ini_set('memory_limit', '128M');//设置内存 根据需求可以修改 date_default_timezone_set("PRC"); $user = ''; //数据库账户 $pwd = ''; //数据库密码 $dbname = ''; //数据库名称 $path = '/home/database_backup/';//备份路径 $delDate = date('Ymd',strtotime('-1 week'));//一周前 //以下勿动 $sqladdress = 'mysqldump'; $time = time(); if(!file_exists($path)) { mkdir($path,0777,true); } $sqlFile = date("Ymd",$time)."_".$dbname.".sql"; $password = $pwd== ''?'':' -p'.$pwd; $order = $sqladdress.' --opt -u'.$user.$password.' '.$dbname.' >'.$path.'\'.$sqlFile; exec($order); $delFile = $path.$delDate.'_'.$dbname.'.sql';
if(
file_exists($delFile)){
unlink($delFile);
}

2、添加crontab 任务。凌晨3点半开始

30 3 * * * php /home/dbbackup.php
原文地址:https://www.cnblogs.com/dongqiliang/p/12341751.html