一,全备份 与 全恢复
1、vim /root/db_backup/mysqldump_mraz.php,内容如下:
<?php
date_default_timezone_set("PRC");
$d = date("Ymd-His");
exec("/usr/local/mysql/bin/mysqldump -uroot -p111111 mraz > mraz-".$d.".sql");
2、进入crontab -e,设置每分钟备份一次(更安全的做法是将备份文件通过scp传到更安全的地方)
*/1 * * * * /usr/local/php/bin/php /root/db_backup/mysqldump_mraz.php
3、全恢复,进入mysql,执行
source /root/db_backup/mraz-20150928-160404.sql
二,增量备份 与 增量备份恢复
1、配置my.cnf,启用二进制备份,vim /etc/my.cnf,配置log-bin目录
源码安装的默认是log-bin=mysql-bin,对应的是/usr/local/mysql/var/
(ps:rpm包安装的话,可以自定义设置为log-bin=/var/mysqlbinlog/)
2、启动mysql后,对应log-bin目录会生成(更安全的做法是将二进制日志文件通过scp传到更安全的地方)
mysql-bin.index 索引文件
mysql-bin.000001 二进制增量备份文件
3、查看二进制备份文件,根据时间和位置恢复(PS:先记录出现故障前的时间,再查看全备份文件的时间)
cd /usr/local/mysql/var/
/usr/local/mysql/bin/mysqlbinlog mysql-bin.000015
3.1、例如:
#150826 12:05:45 server id 1 end_log_pos 202
Query
thread_id=40
exec_time=0
error_code=0
3.2、150826 12:05:45为时间,202为位置
4、恢复(PS:先记录出现故障前的时间,再查看全备份文件的时间)
按时间恢复(一般start-datetime是全备份的时间,stop-datetime是出现故障前的时间)
/usr/local/mysql/bin/mysqlbinlog --stop-datetime='2015-08-26 12:05:44' /usr/local/mysql/var/mysql-bin.000015 | /usr/local/mysql/bin/mysql -uroot -p
或者
/usr/local/mysql/bin/mysqlbinlog --start-datetime='2015-08-26 12:05:44' /usr/local/mysql/var/mysql-bin.000015 | /usr/local/mysql/bin/mysql -uroot -p
或者
/usr/local/mysql/bin/mysqlbinlog --start-datetime='2015-09-28 15:48:08' --stop-datetime='2015-09-28 15:50:10' /usr/local/mysql/var/mysql-bin.000015 | /usr/local/mysql/bin/mysql -uroot -p
按位置恢复
/usr/local/mysql/bin/mysqlbinlog --stop-position='202' /usr/local/mysql/var/mysql-bin.000015 | /usr/local/mysql/bin/mysql -uroot -p
或者
/usr/local/mysql/bin/mysqlbinlog --start-position='201' /usr/local/mysql/var/mysql-bin.000015 | /usr/local/mysql/bin/mysql -uroot -p
或者
/usr/local/mysql/bin/mysqlbinlog --start-position='201' --stop-position='202' /usr/local/mysql/var/mysql-bin.000015 | /usr/local/mysql/bin/mysql -uroot -p