Xtrabackup实现Mysql的InnoDB引擎热备份
前面Zabbix使用的数据库是mysql,数据库备份不用多说,必须滴,由于使用的是innodb引擎,既然做,那就使用第三方强大的Xtrabackup工具来热备吧,Xtrabackup的说明,参见https://my.oschina.net/u/1171265/blog/200437
-
数据库备份:
简单粗暴,下面给出mysql备份脚本:
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
|
#!/bin/bash #mysql热备脚本,Version:1.0 #Author:jzd #备份策略:每周一进行完整备份,以后每一天在前一天的基础上进行增量备份 # #备份目录 back_dir = "/back" #备份数据库信息 host = "127.0.0.1" dbuser = "dbuser" dbpasswd = "dbpasswd" #日志文件 back_log = "${back_dir}/mysql_back.log" #week day week_day = `date + % w` #yesterday yesterday = `date + % F - d "1 days ago" ` #日志记录函数 function log(){ echo "`date` $1" | tee - a ${back_log} } #判断当前日期,是周一进行全备份,其他时间进行增量备份 if [ ${week_day} - eq 1 ]; then #完整备份 log "周${week_day}开始完全备份..." innobackupex - - host = ${host} - - user = ${dbuser} - - password = ${dbpasswd} ${back_dir} &>> ${back_log} if [ $? - eq 0 ]; then log "完全备份完毕." else log "完全备份出错,请检查." exit 1 fi else let dir_num = `find ${back_dir} - type d - name "${yesterday}*" | wc - l` if [ ${dir_num} - ne 1 ]; then log "昨天增量目录未找到或昨天备份目录大于等于2个,请确认后再次备份." exit 1 fi incremental_dir = `find ${back_dir} - type d - name "${yesterday}*" ` log "周${week_day}开始增量备份..." #增量备份 innobackupex - - host = ${host} - - user = ${dbuser} - - password = ${dbpasswd} - - incremental ${back_dir} - - incremental - basedir = ${incremental_dir} &>> ${back_log} if [ $? - eq 0 ]; then log "增量备份成功." else log "备份失败,请检查日志..." exit 1 fi fi exit $? |
需修改的地方,也可以写成配置文件source进去,完全可以用在生产环境。
#备份目录
back_dir="/back"
#备份数据库信息
host="127.0.0.1"
dbuser="dbuser"
dbpasswd="dbpasswd"
#日志文件
back_log="${back_dir}/mysql_back.log"
-
数据恢复:
话说备份容易,恢复不易啊,下面也给出恢复脚本,配合上面备份脚本使用,但是恢复完毕后,需手动恢复当日的二进制文件,恢复方法见开篇Xtrabackup介绍文章;
恢复脚本的星期和日期对应的转换,搞得头都大了,缠了两天,判断太多,导致太乱,于是多使用函数,发现自己对函数的使用加强了(哈哈,相对于以前,感觉自己有面向对象的思想了,但这是shell)
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
101
102
103
104
105
106
107
108
|
#!/bin/bash #mysql数据恢复脚本 #Author:jzd #Version:V1.0 #back dir back_dir = '/back' #full back day full_back_day = 1 #today today = `date + % F` #week day week_day = `date + % w` #log file recovery_log = "${back_dir}/mysql_recovery.log" #log function log(){ echo "`date` $1" | tee - a ${recovery_log} } #find back dir function finddir(){ if [ `find $back_dir - type d - name "$1*" | wc - l` - ne 1 ]; then log "发现备份目录$1为0个或多于一个,请检查..." exit 1 else log "发现备份目录`find $back_dir -type d -name " $ 1 * "`" find_dir = "`find $back_dir -type d -name " $ 1 * "`" fi } #all recovery function allredo(){ log "开始完整备份恢复准备..." innobackupex - - apply - log - - redo - only $ 1 &>> ${recovery_log} if [ $? - ne 0 ]; then log "完整恢复准备出错,请检查..." exit 1 fi log "完整备份恢复准备完成" } #incremental recovery function incredo(){ log "开始增量数据恢复准备..." innobackupex - - apply - log - - redo - only $ 1 - - incremental - dir = $ 2 &>> ${recovery_log} if [ $? - ne 0 ]; then log "增量数据恢复准备出错,请检查..." exit 1 fi log "增量数据$2恢复准备完成" } #real recovery function recovery(){ log "备份数据准备完成,开始恢复数据..." innobackupex - - copy - back $ 1 &>> ${recovery_log} if [ $? - ne 0 ]; then log "完整恢复准备出错,请检查..." exit 1 fi log "恢复数据完成,请检查." log "请手动恢复二进制文件数据." } #判断今日是否是周一 if [ $week_day - eq ${full_back_day} ]; then finddir ${today} all_dir = "${find_dir}" if [ ! - z "${all_dir}" ]; then allredo $all_dir recovery $all_dir else log "未发现今日备份,恢复上周数据." for i in "7 6 5 4 3 2 1" do back_date = `date + % F - d "${i} days ago" ` if [ $i - eq 7 ]; then finddir ${back_date} all_dir = "${find_dir}" allredo ${all_dir} else finddir ${back_date} dir_back = ${find_dir} incredo ${all_dir} ${dir_back} fi done recovery ${all_dir} fi else back_date = `date + % F - d "$((${week_day}-${full_back_day})) days ago" ` finddir ${back_date} all_dir = "${find_dir}" allredo ${all_dir} let flag = $((${week_day} - 1 - ${full_back_day})) while [ ${flag} - ge 0 ] do incr_date = `date + % F - d "${flag} days ago" ` finddir ${incr_date} incr_dir = "${find_dir}" incredo ${all_dir} ${incr_dir} let flag = `expr $flag - 1 ` done recovery ${all_dir} fi exit 0 |
每天一点成长,欢迎指正!