1、mysql數據庫備份的來龍去脈node
一、備份的概念:mysql
將數據收集並保存至另外的多個副本,其目的是將數據還原恢復至備份數據時那個狀態。
sql
二、備份數據的緣由
數據庫
1)作災難恢復使用,要將數據副本作到異地多份備份;
vim
2)數據庫數據改動時使用備份;
bash
3)須要對當前服務器作相關測試時使用備份,備份與測試都是最好在服務器訪問量最少時進行。
服務器
三、備份的事先注意事項
多線程
1)能夠容忍丟失多長時間的數據;
app
2)恢復數據能在多長時間內完成;
less
3)是否須要持續提供服務;
4)須要恢復哪些內容,整個服務器的數據庫,單個數據庫,一個或多個表。
2、數據的備份類型
一、根據是否須要數據庫離線可分爲
1)冷備:cold backup
備份須要關閉mysql服務或讀寫請求均不容許;
2)溫備:warm backup
備份的同時,mysql服務在線,只容許讀不容許寫,在線交易要終止:
3)熱備:hot backup
備份的同時,mysql服務在線,支持讀寫請求,業務不受影響,但服務器的性能會有所降低。
二、根據要備份的數據範圍可分爲
1)徹底備份:full backup
備份當前狀態的整個數據庫的數據;
2)增量備份:increment backup
備份基於上次的徹底備份或增量備份以來所改變的數據;
3)差別備份:different backup
備份基於上次的徹底備份改變了的數據。
三、根據備份數據是否爲文件可分爲
1)物理備份:直接備份數據庫文件
2)邏輯備份:備份表中的數據和庫代碼
3、備份的對象及備份工具
一、備份對象
1)數據
2)配置文件
3)代碼、存儲過程、存儲函數、觸發器等
4)OS相關的配置文件
5)複製相關的配置
6)二進制日誌
二、引擎所支持的備份
MyISAM引擎只支持溫備,而InnoDB還支持熱備。
三、備份工具
1)mysqldump,mysql客戶端的經典備份工具
爲邏輯備份工具,備份和恢復比較慢;
2)mylvmdumper,mysqldump升級版
多線程的邏輯備份工具,備份和恢復速度稍快於mysqldump;
3)lvm-snapshot,基於快照卷的備份工具
接近於熱備的物理備份工具,備份和恢復的速度較快;
4)select ,mysql內置的備份工具
邏輯備份工具,速度快於mysqldump;
select into outfile;
load data infile;
5)xtrabackup,由percana提供的免費開源備份工具
爲物理備份工具,速度快。
6)mysql hotcopy:幾乎冷備的工具,通常不採用,速度慢。
4、數據從備份到恢復的完整流程
1)中止mysql服務;
2)記錄服務和配置文件權限;
3)複製備份文件與數據目錄;
4)按需調整配置;
5)按需改變文件權限;
6)嘗試啓動服務;
7)裝載邏輯備份;
8)檢查和重放二進制日誌;
9)肯定數據還原正常完成;
10)以徹底權限重啓服務器。
5、使用mysqldump進行數據備份及恢復
1:爲測試的數據庫及二進制建立備份目錄;
[root@node1 ~]# mkdir -pv {/mydata/data,/backup,/var/binlog} mkdir: created directory `/mydata/data' mkdir: created directory `/backup' mkdir: created directory `/var/binlog'
2:啓動mysqld服務,建立測試數據庫,並建立測試數據
[root@node1 ~]# service mysqld restart Shutting down MySQL.... [ OK ] Starting MySQL.. [ OK ] [root@node1 ~]#mysql mysql> create database students; Query OK, 1 row affected (0.00 sec) mysql> use students Database changed mysql> create table TLtb (Id tinyint unsigned not null primary key auto_increment,Name char(20) not null unique key,Age tinyint unsigned,Gender char(1) default 'M',Courses char(30) not null); Query OK, 0 rows affected (0.28 sec) mysql> insert into TLtb (Name,Age,Gender,Courses) values ('Xu zu',20,'M','Xiao Wuxianggong'),('Qiao Feng',28,'M','Xianglong Shibazhang'),('Duan Fu',23,'M','Liumai Shenjian'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from TLtb; +----+-----------+------+--------+----------------------+ | Id | Name | Age | Gender | Courses | +----+-----------+------+--------+----------------------+ | 1 | Xu zu | 20 | M | Xiao Wuxianggong | | 2 | Qiao Feng | 28 | M | Xianglong Shibazhang | | 3 | Duan Fu | 23 | M | Liumai Shenjian | +----+-----------+------+--------+----------------------+ 3 rows in set (0.01 sec)
3:使用mysqldump對測試數據庫進行備份,而且滾動二進制日誌,記錄日誌位置;
[root@node1 ~]# mysqldump --lock-all-tables --flush-logs --master-data=2 --databases students > /backup/students_`date +%F`.sq [root@node1 ~]# cp /var/binlog/mysql-bin. mysql-bin.000001 mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 mysql-bin.000005 mysql-bin.000006 mysql-bin.index
4:備份二進制日誌文件;
[root@node1 ~]# cp /var/binlog/mysql-bin.00000* /backup/ [root@node1 ~]# ls /backup/ mysql-bin.000001 mysql-bin.000003 mysql-bin.000005 students_2013-09-30.sql mysql-bin.000002 mysql-bin.000004 mysql-bin.000006 students_.sql
5:新增數據庫數據進行增量備份,查看當前日誌位置;
mysql> create table CDtb (Id tinyint unsigned not null primary key auto_increment,Name char(20) not null unique key,Age tinyint unsigned,Gender char(1) default 'M',Courses char(30) not null); Query OK, 0 rows affected (0.14 sec) mysql> insert into CDtb (Name,Age,Gender,Courses) values ('Yideng Dashi',80,'M','Yiyangzhi'),('Hong Qigong',66,'M','Dagou Bangfa'),('Huang Yaoshi',60,'M','Tanzhi Shengong'); Query OK, 3 rows affected (0.05 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from CDtb; +----+--------------+------+--------+-----------------+ | Id | Name | Age | Gender | Courses | +----+--------------+------+--------+-----------------+ | 1 | Yideng Dashi | 80 | M | Yiyangzhi | | 2 | Hong Qigong | 66 | M | Dagou Bangfa | | 3 | Huang Yaoshi | 60 | M | Tanzhi Shengong | +----+--------------+------+--------+-----------------+ 3 rows in set (0.01 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000006 | 716 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
[root@node1 ~]# less /backup/students_2013-09-30.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=107; [root@node1 ~]# mysqlbinlog --start-position=107 /var/binlog/mysql-bin.000006 > /backup/students_incremental.sql
6:模擬數據庫數據損壞;
mysql> insert into CDtb (Name,Age,Gender,Courses) values ('Ou Yangfeng',75,'M','Hamagong'); Query OK, 1 row affected (0.05 sec) mysql> drop database students; Query OK, 2 rows affected, 2 warnings (0.13 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000006 | 1082 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
7:恢復備份數據:徹底備份+增量備份+二進制日誌文件;
bac
[root@node1 ~]# mysqlbinlog /var/binlog/mysql-bin.000006 ...... # at 993 #130930 22:51:40 server id 1 end_log_pos 1082 Query thread_id=2 exec_time=0 error_code=1146 SET TIMESTAMP=1380552700/*!*/; drop database students [root@node1 ~]# mysqlbinlog --start-position=716 --stop-position=993 /var/binlog/mysql-bin.000006 > /backup/students_993.sql
[root@node1 ~]# mysqlbinlog /var/binlog/mysql-bin.000006 ...... # at 993 #130930 22:51:40 server id 1 end_log_pos 1082 Query thread_id=2 exec_time=0 error_code=1146 SET TIMESTAMP=1380552700/*!*/; drop database students [root@node1 ~]# mysqlbinlog --start-position=716 --stop-position=993 /var/binlog/mysql-bin.000006 > /backup/students_993.sql mysql> set global sql_log_bin=0; Query OK, 0 rows affected (0.00 sec) mysql> source /backup/students_2013-09-30.sql mysql> source /backup/students_incremental.sql mysql> source /backup/students_993.sql
8:檢測備份的數據是否已經正常恢復。
mysql> set global sql_log_bin=1; Query OK, 0 rows affected (0.00 sec)
mysql> use students; Database changed mysql> show tables; +--------------------+ | Tables_in_students | +--------------------+ | CDtb | | TLtb | +--------------------+ 2 rows in set (0.00 sec) mysql> select * from TLtb; +----+-----------+------+--------+----------------------+ | Id | Name | Age | Gender | Courses | +----+-----------+------+--------+----------------------+ | 1 | Xu zu | 20 | M | Xiao Wuxianggong | | 2 | Qiao Feng | 28 | M | Xianglong Shibazhang | | 3 | Duan Fu | 23 | M | Liumai Shenjian | +----+-----------+------+--------+----------------------+ 3 rows in set (0.00 sec) mysql> select * from CDtb; +----+--------------+------+--------+-----------------+ | Id | Name | Age | Gender | Courses | +----+--------------+------+--------+-----------------+ | 1 | Yideng Dashi | 80 | M | Yiyangzhi | | 2 | Hong Qigong | 66 | M | Dagou Bangfa | | 3 | Huang Yaoshi | 60 | M | Tanzhi Shengong | | 4 | Ou Yangfeng | 75 | M | Hamagong | +----+--------------+------+--------+-----------------+ 4 rows in set (0.00 sec)
6、使用lvm-snapshot進行數據備份及恢復
一、建立LVM邏輯卷並開機自動掛載,並建立mysql數據與二進制日誌存放目錄;
y
[root@localhost ~]# pvcreate /dev/sdb{1,2} Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created [root@localhost ~]# vgcreate vg1 /dev/sdb{1,2} Volume group "vg1" successfully created [root@localhost ~]# lvcreate -L +10G -n lv1 vg1 Logical volume "lv1" created [root@localhost ~]# mke2fs -t ext4 /dev/vg1/lv1 [root@localhost ~]# vim /etc/fstab /dev/vg1/lv1 /Mydata ext4 defaults 0 0 [root@localhost ~]# mount -a [root@localhost ~]# mkdir /Mydata/{data,,binlog} /backup1 [root@localhost ~]# chown -R mysql:mysql /Mydata/*
二、新建mysql數據庫並新增數據;
mysql> create database schooldb; Query OK, 1 row affected (0.01 sec) mysql> use schooldb Database changed mysql> create table studentstb (Id tinyint unsigned not null primary key auto_increment,Name char(20) not null unique key,Age tinyint unsigned,Gender char(1) default 'M',Courses char(30) not null); Query OK, 0 rows affected (0.38 sec) mysql> insert into studentstb (Name,Age,Gender,Courses) values ('Zhang San',19,'M','Shujujiegou'),('Li Ling',18,'F','Daxueyingyu'),('Wang Wu',20,'M','Dianluyuanli'); Query OK, 3 rows affected (0.09 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from studentstb; +----+-----------+------+--------+--------------+ | Id | Name | Age | Gender | Courses | +----+-----------+------+--------+--------------+ | 1 | Zhang San | 19 | M | Shujujiegou | | 2 | Li Ling | 18 | F | Daxueyingyu | | 3 | Wang Wu | 20 | M | Dianluyuanli | +----+-----------+------+--------+--------------+ 3 rows in set (0.03 sec)
三、登陸mysql,對全部表加鎖,並滾動日誌,查看當前日誌所在位置;
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> flush logs; Query OK, 0 rows affected (0.04 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000015 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.01 sec)
四、另外啓動一個終端,爲邏輯卷建立快照卷,作完快照釋放施加的鎖請求;
[root@localhost ~]# lvcreate -L 1G -s -p r -n lv1-snap /dev/vg1/lv1 Logical volume "lv1-snap" created
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)
五、掛載快照卷,並備份數據,備份完成卸載快照卷並刪除;
[root@localhost ~]# cp -rp /mnt/* /backup1 [root@localhost ~]# umount /mnt/ [root@localhost ~]# lvremove /dev/vg1/lv1-snap Do you really want to remove active logical volume lv1-snap? [y/n]: y Logical volume "lv1-snap" successfully removed
六、中止mysql服務,模擬數據庫數據損壞,進行數據恢復;
[root@localhost ~]# service mysqld stop Shutting down MySQL... [ OK ] [root@localhost ~]# rm -rf /Mydata/* [root@localhost ~]# cp -rp /backup1/* /Mydata/ [root@localhost ~]# service mysqld start Starting MySQL.. [ OK ]
七、檢測數據恢復是否正常恢復完成。
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | schooldb | | test | | xiaozheng | +--------------------+ 7 rows in set (0.01 sec) mysql> use schooldb Database changed mysql> show tables; +--------------------+ | Tables_in_schooldb | +--------------------+ | studentstb | +--------------------+ 1 row in set (0.00 sec) mysql> select * from studentstb; +----+-----------+------+--------+--------------+ | Id | Name | Age | Gender | Courses | +----+-----------+------+--------+--------------+ | 1 | Zhang San | 19 | M | Shujujiegou | | 2 | Li Ling | 18 | F | Daxueyingyu | | 3 | Wang Wu | 20 | M | Dianluyuanli | +----+-----------+------+--------+--------------+ 3 rows in set (0.00 sec)
7、使用xtrabackup進行數據備份恢復
一、下載並安裝xtrabackup;
[root@localhost ~]# ls anaconda-ks.cfg install.log percona-xtrabackup-2.1.4-656.rhel6.x86_64.rpm hellodb.sql install.log.syslog [root@localhost ~]# yum -y install percona-xtrabackup-2.1.4-656.rhel6.x86_64.rpm
二、建立有備份權限的數據庫用戶;
mysql> create user 'xtrabackup'@'localhost' identified by 'mypass'; Query OK, 0 rows affected (0.09 sec) mysql> revoke all privileges,grant option from 'xtrabackup'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> grant reload,lock tables,replication client,event on *.* to 'xtrabackup'@'localhost'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
三、對數據庫進行徹底備份;
[root@localhost ~]# innobackupex --user=xtrabackup --password=mypass /backup2 InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona Ireland Ltd 2009-2012. All Rights Reserved. ........ innobackupex: Backup created in directory '/backup2/2013-09-08_21-06-19' innobackupex: MySQL binlog position: filename 'mysql-bin.000017', position 598 130908 21:06:24 innobackupex: Connection to database server closed 130908 21:06:24 innobackupex: completed OK!
四、關閉mysql服務並模擬數據損壞,並準備一個徹底備份(prepare);
[root@localhost ~]# service mysqld stop Shutting down MySQL... [ OK ] [root@localhost ~]# rm -rf /mydata/data/* [root@localhost backup2]# innobackupex --apply-log /backup2/2013-09-08_21-06-19/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona Ireland Ltd 2009-2012. All Rights Reserved. ........ xtrabackup: starting shutdown with innodb_fast_shutdown = 1 130908 21:13:44 InnoDB: Starting shutdown... 130908 21:13:48 InnoDB: Shutdown completed; log sequence number 1626636 130908 21:13:48 innobackupex: completed OK!
五、從一個徹底備份中恢復數據,並檢測數據庫數據是否正常恢復完成。
[root@localhost ~]# innobackupex --copy-back /backup2/2013-09-08_21-06-19/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona Ireland Ltd 2009-2012. All Rights Reserved. ............ innobackupex: Copying '/backup2/2013-09-08_21-06-19/ib_logfile1' to '/mydata/data' innobackupex: Finished copying back files. 130908 21:21:23 innobackupex: completed OK! [root@localhost ~]#service mysqld start
[root@localhost ~]# innobackupex --copy-back /backup2/2013-09-08_21-06-19/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona Ireland Ltd 2009-2012. All Rights Reserved. ............ innobackupex: Copying '/backup2/2013-09-08_21-06-19/ib_logfile1' to '/mydata/data' innobackupex: Finished copying back files. 130908 21:21:23 innobackupex: completed OK! [root@localhost ~]# service mysqld start Starting MySQL.. [ OK ] mysql> use schooldb Database changed mysql> show tables; +--------------------+ | Tables_in_schooldb | +--------------------+ | studentstb | +--------------------+ 1 row in set (0.00 sec) mysql> select * from studentstb; +----+-----------+------+--------+--------------+ | Id | Name | Age | Gender | Courses | +----+-----------+------+--------+--------------+ | 1 | Zhang San | 19 | M | Shujujiegou | | 2 | Li Ling | 18 | F | Daxueyingyu | | 3 | Wang Wu | 20 | M | Dianluyuanli | +----+-----------+------+--------+--------------+ 3 rows in set (0.00 sec)
--------------------------------------------------------------------
六、在測試數據庫中新增數據,實現數據的增量備份;
七、進行第一次增量備份;
八、繼續於測試數據庫中新增數據,進行第二次增量備份;
九、關閉mysql服務並模擬數據庫數據損壞,進行兩次增量備份的恢復;
十、檢測增量備份的數據恢復是否正常完成。