安装xtrabackup----------------------------------------------------------------
利用yum源
yum install https://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
yum install percona-xtrabackup.x86_64
其他:二进制安装、源码安装
脚本详解----------------------------------------------------------------------------------------------------------
#!/bin/bash
#name:xtrabackup-lsn.sh
#finish time:2015-05-18
#author:[email protected]
BegainTime=$(date +'%Y-%m-%d_%H%M%S')
DataName=${BegainTime}.tar.gz
#判断是否是星期天,用作全备只用,如果星期天,就进行全备
IsSunday=$(date +'%A')
#参与备份的跟目录
BackUpAll=/usr/backups
#备份的数据目录
BackUpDir=/usr/backups/data
#备份工具
BackupTool=/usr/bin/innobackupex
BackupUser=root
BackupPass="xxxxxx"
DbHost=192.168.1.124
DbPort=3306
DbName="hotel_newdb local8684com paycenterdb takeout qu8684com taodb1 tongji saledb daidb admin_8684 bendi"
#用来记录lsn的位置
LsnFile=/usr/backups/lsnfile
#日志路径
BackUpLog=/var/log/xtrabackupdb.log
bak_result(){
IsTure=$1
if [ "${IsTure}" -eq 0 ];then
#不存在xtrabackup_checkpoints,就无法获取lsn,说明备份失败
[ ! -f "${BackUpAll}/xtrabackup_checkpoints" ] && echo "check xtrabackup_checkpoints exists in ${BackUpAll},backup failed,time is :$(date +'%Y-%m-%d %H:%M:%S')" 1>>${Ba
ckUpLog} 2>&1 && exit
#如果存在xtrabackup_checkpoints说明备份成功
grep to_lsn ${BackUpAll}/xtrabackup_checkpoints | cut -d " " -f 3 >${LsnFile}&&echo "backup lsn is $(cat ${LsnFile})" >>${BackUpLog}
echo "backup sucess,time is :$(date +'%Y-%m-%d %H:%M:%S')" 1>>$BackUpLog 2>&1
else
echo "backup failed,time is :$(date +'%Y-%m-%d %H:%M:%S')" 1>>$BackUpLog 2>&1
fi
}
if [ ! -d "${BackUpAll}" ];then
mkdir -p ${BackUpAll}
fi
if [ ! -d "${BackUpDir}" ];then
mkdir -p ${BackUpDir}
fi
#判断是否是星期天,是则全备,足以-extra-lsndir是特意产生xtrabackup_checkpoints的
if [ "${IsSunday}"x = "Friday"x ];then
echo "full backup is starting...time is :"${BegainTime} >>$BackUpLog
$BackupTool --user=$BackupUser --password=$BackupPass --host=$DbHost --port=$DbPort --no-timestamp --extra-lsndir=${BackUpAll} --databases="${DbName}" --stream=xbstrea
m $BackUpDir 2>>$BackUpLog | gzip 1>${BackUpDir}/full${DataName}
bak_result "$?"
else
#否则,进行增倍
echo "incremental backup is starting...time is :"${BegainTime} >>$BackUpLog
noteslsn=$(cat ${LsnFile})
[ -z "${noteslsn}" ] &&echo "incremental backup failed,please check lsn is write in the lsnfile or you not do full backup." 1>>${BackUpLog} 2>&1 &&exit
$BackupTool --user=$BackupUser --password=$BackupPass --host=$DbHost --port=$DbPort --no-timestamp --incremental --incremental-lsn=${noteslsn} --databases="${DbName}"
--stream=xbstream --extra-lsndir=${BackUpAll} $BackUpDir 2>>$BackUpLog | gzip 1>${BackUpDir}/inc${DataName}
bak_result "$?"
fi
find ${BackUpDir} -type f -mtime +15 -exec rm -rf {} \;
/usr/bin/rsync -vzrtopg --bwlimit=600 --progress ${BackUpDir}/ [email protected]::backup/backbinlog/ --password-file=/usr/local/etc/rsyncd.sec
备份测试---------------------------------------------------------------------------------------------------
?备份的结果:
[root@node1 data]# ls -l
total 309940
-rw-r--r-- 1 root root 161487054 May 20 16:23 full2015-05-20_162255.tar.gz
-rw-r--r-- 1 root root 155888321 May 20 16:34 inc2015-05-20_163326.tar.gz
[root@node1 data]# cat /var/script/xtrabackup-lsn.sh
?增备前的数据:
mysql> select * from yzxtest;-------------------myisam存储引擎
+----------------+-------+
| name | id |
+----------------+-------+
| yuanzaixin1111 | 10001 |
| yuanzaixindd | 10002 |
+----------------+-------+
2 rows in set (0.00 sec)
mysql> select * from test;-------------innodb存储引擎
+--------------+-------+
| name | id |
+--------------+-------+
| yuanzaixin1 | 10001 |
| yuanzaixin2 | 10002 |
| yuanzaixin3 | 10003 |
| yuanzaixin4 | 10004 |
| yuanzaixin5 | 10005 |
| yuanzaixin6 | 10006 |
| yuanzaixindd | 10006 |
| yuanzaixinff | 10005 |
| yuanzaixinff | 10005 |
| yuanzaixindd | 10006 |
+--------------+-------+
10 rows in set (0.02 sec)
?增备份后,修改表数据(恢复后比对用):
mysql> use guangzhou1
Database changed
mysql> insert into yzxtest value('yuanzaixincc','10003');
Query OK, 1 row affected (0.02 sec)
mysql> update yzxtest set name='yuanzaixindd02' where id='10001';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from yzxtest;
+----------------+-------+
| name | id |
+----------------+-------+
| yuanzaixindd02 | 10001 |
| yuanzaixindd | 10002 |
| yuanzaixincc | 10003 |
+----------------+-------+
3 rows in set (0.00 sec)
mysql> delete from test where id='10005';
Query OK, 3 rows affected (0.10 sec)
mysql> delete from test where id='10006';
Query OK, 3 rows affected (0.09 sec)
mysql> select * from test;
+-------------+-------+
| name | id |
+-------------+-------+
| yuanzaixin1 | 10001 |
| yuanzaixin2 | 10002 |
| yuanzaixin3 | 10003 |
| yuanzaixin4 | 10004 |
+-------------+-------+
4 rows in set (0.00 sec)
mysql>
④备份:
[root@node1 data]# /var/script/xtrabackup-lsn.sh
[root@node1 data]# ls -l
total 157704
-rw-r--r-- 1 root root 161487054 May 20 16:23 full2015-05-20_162255.tar.gz
[root@node1 data]# vim /var/script/xtrabackup-lsn.sh
[root@node1 data]# /var/script/xtrabackup-lsn.sh
[root@node1 data]# ls -l
total 309940
-rw-r--r-- 1 root root 161487054 May 20 16:23 full2015-05-20_162255.tar.gz---全备
-rw-r--r-- 1 root root 155888321 May 20 16:34 inc2015-05-20_163326.tar.gz----增备
[root@node1 data]# cat /var/script/xtrabackup-lsn.sh
恢复测试----------------------------------------------------------------------------------------------------------
①将全备放到/usr/backup/basedir下,将增备放到/usr/backup/incrementaldir(这两个目录可以自己定义,但是不要将全备和增备的解压文件放到同一文件中)
[root@node1 basedir]# mv /usr/backups/data/full2015-05-20_162255.tar.gz /usr/backup/basedir
[root@node1 basedir]# ls -l
total 157704
-rw-r--r-- 1 root root 161487054 May 20 16:23 full2015-05-20_162255.tar.gz
[root@node1 basedir]#
[root@node1 incrementaldir]# mv /usr/backups/data/inc2015-05-20_163326.tar.gz /usr/backup/incrementaldir
[root@node1 incrementaldir]# ls -l
total 152236
-rw-r--r-- 1 root root 155888321 May 20 16:34 inc2015-05-20_163326.tar.gz
[root@node1 incrementaldir]#
?在各自的目录中解压文件,并删除压缩文件:
[root@node1 incrementaldir]# gunzip
[root@node1 incrementaldir]# rm -rf inc2015-05-20_163326.tar.gz
[root@node1 incrementaldir]# ls -l
total 152284
-rw-r----- 1 root root 188 May 20 17:12 backup-my.cnf
drwx------ 2 root root 4096 May 20 17:12 guangzhou
drwx------ 2 root root 4096 May 20 17:12 guangzhou1
-rw-r----- 1 root root 16384 May 20 17:12 ibdata1.delta
-rw-r----- 1 root root 44 May 20 17:12 ibdata1.meta
-rw-r----- 1 root root 21 May 20 17:12 xtrabackup_binlog_info
-rw-r----- 1 root root 99 May 20 17:12 xtrabackup_checkpoints
-rw-r----- 1 root root 743 May 20 17:12 xtrabackup_info
-rw-r----- 1 root root 2560 May 20 17:12 xtrabackup_logfile
[root@node1 incrementaldir]#
[root@node1 basedir]# gunzip
[root@node1 basedir]# rm -rf full2015-05-20_162255.tar.gz
[root@node1 basedir]# ls -l
total 34844
-rw-r----- 1 root root 188 May 20 17:18 backup-my.cnf
drwx------ 2 root root 4096 May 20 17:18 guangzhou
drwx------ 2 root root 4096 May 20 17:18 guangzhou1
-rw-r----- 1 root root 35651584 May 20 17:18 ibdata1
-rw-r----- 1 root root 21 May 20 17:18 xtrabackup_binlog_info
-rw-r----- 1 root root 93 May 20 17:18 xtrabackup_checkpoints
-rw-r----- 1 root root 693 May 20 17:18 xtrabackup_info
-rw-r----- 1 root root 2560 May 20 17:18 xtrabackup_logfile
[root@node1 basedir]#
?全备应用事物日志
[root@node1 basedir]# innobackupex --apply-log --redo-only /usr/backup/basedir
........................
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 355090966
150520 17:23:34 innobackupex: completed OK!-----------------------看到如下字眼,表示ok
[root@node1 basedir]#
④增备应用事物日志,并对整体实行事物回滚
[root@node1 incrementaldir]# innobackupex --apply-log /usr/backup/basedir --incremental-dir=/usr/backup/incrementaldir
..............
innobackupex: Copying '/usr/backup/incrementaldir/guangzhou/mem.MYI' to '/usr/backup/basedir/guangzhou/mem.MYI'
150520 17:28:15 innobackupex: completed OK!-----------------------------看到如下字眼,表示ok
[root@node1 incrementaldir]#
⑤停止mysqld
[root@node1 incrementaldir]# mysqladmin -u root -ptianqu shutdown
[root@node1 incrementaldir]#
⑥将原有数据目录(datadir)移至/usr/backupyzx/back1(为什么现在不直接删除?因为有系统数据库mysql,以及其他未备份的库需要移到现有环境上)
[root@node1 back1]# mv /usr/local/mysql/data /usr/backupyzx/back1
[root@node1 back1]# ls -l
total 4
drwxr-xr-x 8 mysql mysql 4096 May 20 17:30 data
[root@node1 back1]#
⑦持久化到数据库:
[root@node1 back1]# innobackupex --copy-back /usr/backup/basedir
innobackupex: Starting to copy InnoDB log files
innobackupex: in '/usr/backup/basedir'
innobackupex: back to original InnoDB log directory '/usr/local/mysql/data'
innobackupex: Finished copying back files.
150520 17:38:27 innobackupex: completed OK!-------------看到如下字眼,表示ok
[root@node1 back1]#
⑧将mysql,以及未备份的库移动到/usr/local/mysql/data中(如果未备份的库,以及现有用户都不需要或者已经损坏,则不需要此步骤,直接 mysql_install_db --user=mysql 生成系统库mysql即可)
[root@node1 back1]# mv /usr/backupyzx/data/guangzhou2 /usr/backupyzx/data/mysql /usr/local/mysql/data/
[root@node1 back1]#
将/usr/local/mysql/data的所有者改成mysql用户与组
[root@node1 back1]# chown -R mysql:mysql /usr/local/mysql/data
[root@node1 back1]#
⑨启动mysql
[root@node1 back1]# mysqld_safe &
[1] 14371
[root@node1 back1]# 150520 17:46:58 mysqld_safe Logging to '/usr/local/mysql/data/node1.err'.
150520 17:46:58 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
[root@node1 back1]# ps -ef | grep mysql
root 10810 19757 0 16:34 pts/0 00:00:00 mysql -u root -px xxxx
root 14371 22196 0 17:46 pts/3 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe
mysql 14626 14371 0 17:46 pts/3 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/node1.err --pid-file=/usr/local/mysql/data/node1.pid --socket=/tmp/mysql.sock --port=3306
root 14651 22196 0 17:47 pts/3 00:00:00 grep mysql
[root@node1 back1]#
⑩验证数据:-------------------数据已全部还原
[root@node1 back1]# mysql -u root -ptianqu
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use guangzhou1
Database changed
mysql> select * from test;
+--------------+-------+
| name | id |
+--------------+-------+
| yuanzaixin1 | 10001 |
| yuanzaixin2 | 10002 |
| yuanzaixin3 | 10003 |
| yuanzaixin4 | 10004 |
| yuanzaixin5 | 10005 |
| yuanzaixin6 | 10006 |
| yuanzaixindd | 10006 |
| yuanzaixinff | 10005 |
| yuanzaixinff | 10005 |
| yuanzaixindd | 10006 |
+--------------+-------+
10 rows in set (0.00 sec)
mysql> select * from yzxtest;
+----------------+-------+
| name | id |
+----------------+-------+
| yuanzaixin1111 | 10001 |
| yuanzaixindd | 10002 |
+----------------+-------+
2 rows in set (0.00 sec)
mysql>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30221425/viewspace-1665914/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30221425/viewspace-1665914/