引言
大项目中的数据库备份是必不可少的,否则一旦出现大问题就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',
'db_pwd' => 'root',
'db_back_path' => '/home/db_back',
'back_file_suffix' => 'aaa',
'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',
'back_type' => 3
);
private $file_name;
private $back_shell;
public function __construct()
{
set_time_limit(0);
date_default_timezone_set('PRC');
$this->db_config = array_merge($this->db_config,require "DB_config.php");
$this->set_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'];
$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 = 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_exec($shell);
}catch (Exception $e)
{
echo $e->getMessage();
}
}
}
$obj = new DB_back();
$obj->start_back();
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
将这两个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父目录,否则不会顺利创建目录
代码写的并不好,如有Bug或者建议,感谢指正