mysql备份与还原(已测试)


测试过了,可以恢复

1创建库跟表
awk截取/etc/passwd
cat /etc/passwd | awk -F: '{print $1,$3,$4,$6}' >/tmp/mysql.txt
mysql
create database user;
create table a(username char(10),uid int,gid int,homedir char(30));
use user
load data infile '/tmp/mysql.txt' into table user fields terminated by ' ';导入数据


2修改mysql配置文件增加以下内容
vim /etc/my.cnf
[mysqld]
log-bin=binlog
log-bin-index=binlog.index
sync_binlog=0
重启服务mysql
service mysqld restart

3增加任务计划周一到周六的早上3点进行增量备份
crontab -e
0 3 * * 1-6 /usr/bin/mysql -e 'flush logs'
0 3 * * 0 /usr/bin/mysqldump user > /tmp/user.bak

4恢复上周三之前的状态,假设周一的binlog.000001,周二是2以此类推。
建立脚本
#!/bin/bash
c="/var/lib/mysql/"
for((i=1;i<=3;i++));do
       a=`mysqlbinlog ${c}binlog.00000$i | grep -P "at [0-9]+" |awk '{print $3}' | head -1`
       b=`mysqlbinlog ${c}binlog.00000$i | grep -P "at [0-9]+" |awk '{print $3}' | tail -1`
       mysqlbinlog --start-position $a --stop-position $b  ${c}binlog.00000$i |mysql
done


你可能感兴趣的:(mysql,mysql,linux,linux,mysql备份)