配置文件
<?php /** * 任务进程中的配置文件 * time 单位秒 * @var array */ return [ //任务 id "TASK"=>[ "clearmeesage"=>[ "time"=>60, "number"=>0, "name"=>"clearmeesage" ],//清除短信中不要的垃圾数据 "clearroom"=>[ "time"=>3600, "number"=>1, "name"=>"clearroom" ],//清除房间中不要的垃圾数据 "backup"=>[ "time"=>86400,//多久备份一次 "number"=>2, "name"=>"backup", "target_dir"=>"/home/bak/",//备份的路径 "dbArray"=>[//要备份的数据库 "tourism_game", ], "past_time"=>9//过期时间/天 ],//数据库定时备份 ], "EXECUTE"=>["clearroom","backup","clearmeesage"],//需要启动的任务 "backup", //数据库信息 "DB"=>array( 'DB_TYPE' => 'mysql', 'DB_HOST' => 'localhost', 'DB_NAME' => '',//数据库名称 'DB_USER' => '',//数据库账号 'DB_PWD' => '',//密码 'DB_PORT' => '3306', 'DB_CODE'=>'utf8' ), //邮件发送配置 "EMAIL"=>[ 'SMTP_HOST' => 'smtp.qq.com', //SMTP服务器 'SMTP_PORT' => '587', //SMTP服务器端口 'SMTP_USER' => '8044023@qq.com', //SMTP服务器用户名 'SMTP_PASS' => '', //SMTP服务器密码 'FROM_EMAIL' => '8044023@qq.com', //发件人EMAIL 'FROM_NAME' => '测试附件服务器端', //发件人名称 'REPLY_EMAIL' => '', //回复EMAIL(留空则为发件人EMAIL) 'REPLY_NAME' => '', //回复名称(留空则为发件人名称) "GET_EMAIL" => 'cqkxm@qq.com',//接收邮箱地址 ], ]; ?>
业务代码
<?php /** * 备份数据库 * @author 码农<8044023@qq.com> * */ namespace Crontab; import("PHPMailer.PHPMailerAutoload");//邮件发送插件 class init{ public static function _init(){ $date = date("Ymd"); $sql_arr=array(); foreach(C("TASK.backup")['dbArray'] as $db_name){ $sql_name=C("TASK.backup")['target_dir'].$db_name."_".$date.".sql"; $command ="mysqldump -u ".C("DB.DB_USER")." -p".replace_keyword(C("DB.DB_PWD"))." ".$db_name." >".$sql_name; shell_exec($command); $sql_arr[]=$sql_name; } //邮件发送C("EMAIL.GET_EMAIL") timePHP_send_mail(C("EMAIL.GET_EMAIL"),'测试备份-'.date("Y-m-d H:i:s"),'备份测试环境-'.date("Y-m-d H:i:s",time()),'<p>感谢你使用timePHP框架!</p>',$sql_arr); //删除 过期的数据库备份数据 $past_time=C("TASK.backup")['target_dir']; for($i=$past_time;$i>=1;$i--){ foreach(C("TASK.backup")['db_array'] as $db_name){ $command="rm -rf ".C("TASK.backup")['target_dir'].$db_name."_".date("Ymd",time()-(($i+$past_time)*86400)).".sql"; shell_exec($command);//是否删除 过期的数据库 } } } }