Linux下crontab+php实现Mysql数据库定时备份

引言
大项目中的数据库备份是必不可少的,否则一旦出现大问题就GG了,虽然本文是讲述利用PHP实现数据库定时备份,但是这并不是好的方案

想要定时备份数据库,最快捷安全的方法就是利用shell脚本了,功能强大操作方便,而且执行速度极快,不像PHP还需要被apache解析一把。。

当然,不管是用php定时备份,还是shell脚本定时备份,都离不开crontab这玩意,毕竟它才是真正的定时器,这里粗略的说一下crontab吧

crontab使用简介
crontab常用基础命令

crontab -e //编辑定时任务,默认以VI打开
crontab -l //列出当前的定时任务
crontab -r //删除定时任务
任务格式 :

 *    *    *    *    *       program                
 分   时    日   月   星期       命令

crontab这里就不多说了,有兴趣的可以搜查相关的资料。

当然 ,还有mysql数据库备份的命令

mysqldump -h host -u user_name -ppassword database_name > filename 备份全数据
mysqldump -h host -u user_name -ppassword database_name –no-data > filename 只备份表结构
mysqldump -h host -u user_name -ppassword database_name –no-create-info > filename 只备份数据
注:-ppassword中间是没有空格的

这里都是些简介,不够详细,但是足够使用了,接下来看代码:

相关代码
DB_config.php

<?php 
return array(
    'db_host'  => '127.0.0.1', //主机地址
    'db_name'  => 'root', //数据库名
    'db_user'  => 'root', //username
    'db_pwd'   => 'root', //password
    'db_back_path' => '/home/db_back', //备份目录,建议使用绝对路径
    'back_file_suffix' => 'aaa', //备份文件的后缀名
    //type = 1  back all 备份数据和表结构
    //type = 2  just back table structure 只备份表结构
    //type = 3  just back table data  只备份表数据
    'back_type' => 1
)

 ?>

DB_config.php

<?php 

class DB_back {

    //这里是默认配置
    private $db_config = array(
        'db_host'  => '127.0.0.1',
        'db_name'  => 'ftdtian',
        'db_user' => 'root',
        'db_pwd'  => '123',
        'db_back_path' => '/home/db_back',
        'back_file_suffix' => 'bak',
        //type = 1  back all
        //type = 2  just back table structure
        //type = 3  just back table data
        'back_type' => 3
    );

    //备份文件名
    private $file_name;
    //shell命令
    private $back_shell;

    public function __construct()
    {
        //无限制脚本时间
        set_time_limit(0);
        //设置时区
        date_default_timezone_set('PRC');
        //merge配置文件
        $this->db_config = array_merge($this->db_config,require "DB_config.php"); 
        $this->set_config();

    }

    //配置config
    private function set_config()
    {   
        //当前时间
        $date_format = date("Y-m-d-H:i:s",time());
        $common_shell = "mysqldump  -h %s -u %s -p%s %s ";

        //默认备份文件名(备份表结构和数据)
        $file_format = $date_format.'_all.'.$this->db_config['back_file_suffix']; 
        //默认备份全部的shell
        $this->back_shell = $common_shell.' > %s';
        switch ($this->db_config['back_type']) {
            case '1':
                break;
            case '2':
                //只备份表结构
                $file_format = $date_format.'_table_structure.'.$this->db_config['back_file_suffix']; 
                $this->back_shell = $common_shell.' --no-data > %s';
                break;
                //只备份表数据
            case '3':
                $file_format = $date_format.'_table_data.'.$this->db_config['back_file_suffix']; 
                $this->back_shell = $common_shell.' --no-create-info > %s';
            default:
                break;
        }
        $this->db_config['db_back_path'] = $this->db_config['db_back_path'].DIRECTORY_SEPARATOR.date("Y-m-d",time());
        //创建文件夹
        $this->make_dir($this->db_config['db_back_path']);
        //构建文件全路径
        $this->file_name = $this->db_config['db_back_path'].DIRECTORY_SEPARATOR.$file_format;
    }

    //创建文件夹
    private function make_dir($path , $mode = 0755, $recursive = true)
    {
        if(!is_dir($path))
        {   
                mkdir($path,$mode,$recursive);
                chmod($path,$mode);      
        }
        return true;
    }

    //开始备份
    public function start_back()
    {
        //字符串格式名生成shell命令
        $shell = sprintf($this->back_shell,$this->db_config['db_host'],$this->db_config['db_user'],$this->db_config['db_pwd'],$this->db_config['db_name'],$this->file_name);
        try
        {   
            //执行shell
            shell_exec($shell);
        }catch (Exception $e)
        {
            echo $e->getMessage();
        }
    }
}

$obj = new DB_back();
$obj->start_back();
 ?>

将这两个PHP文件放到同一目录中,我们假设放在/var/www/html/back/下

crontab -e
//每天凌晨两点半执行备份
30 2 * * * /usr/bin/php  /var/www/html/back/DB_back.php

整个流程就是这样,如果需要修改相对应的配置,请直接修改DB_config.php配置文件

备注:
在使用php的mkdir时,需要确定当前目录的父目录是否具有相应的写权限,如果没有,请先进入终端进行chmod父目录,否则不会顺利创建目录
 

发布了25 篇原创文章 · 获赞 0 · 访问量 590

猜你喜欢

转载自blog.csdn.net/weixin_39709920/article/details/104474763