一 真實環境使用冷備
在上一篇文章(MySQL備份與恢復之冷備)中,我們提到了冷備。但是有個問題,我們存儲的數據文件是保存在當前本地磁盤的,如果這個磁盤掛掉,那我們存儲的數據不就丟失了,這樣備份數據不就功虧一篑,勞而無功。所以真實環境中我們多准備幾塊磁盤,然後再在這些磁盤上搭建LVM,把MySQL的數據目錄掛載到LVM上,這樣數據就不是存儲在當前磁盤上,就可以保證數據的安全性。
二 示意圖
真實環境使用冷備示意圖
三 真實環境使用冷備模擬
第一步,需要提前規劃好磁盤,這裡做模擬,添加兩磁盤。
第二步,對磁盤進行分區。
fdisk /dev/sdb
fdisk /dev/sdc
ll /dev/sd[bc]1
第三步,yum安裝lvm2。
yum install lvm2 -y
第四步,創建物理卷。
pvcreate /dev/sdb1 /dev/sdc1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
第五步,創建卷組
vgcreate data /dev/sdb1 /dev/sdc1
Volume group "data" successfully created
第六步,創建邏輯卷
lvcreate -L 2G -n mydata data
Logical volume "mydata" created
第七步,格式化磁盤。
mkfs.ext4 /dev/data/mydata
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
第八步,冷備
ls /usr/local/mysql/data/
tar -cvPzf mysql01.tar.gz /usr/local/mysql/data/
第九步,刪除數據庫文件。
rm -rf /usr/local/mysql/data/*
第十步,掛載。
mount /dev/data/mydata /usr/local/mysql/data/
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 2.4G 6.8G 27% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 160M 3.7G 5% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/mapper/data-mydata
2.0G 67M 1.9G 4% /usr/local/mysql/data
[root@serv01 ~]#
第十一步,將掛載信息寫入配置文件。
echo "/dev/mapper/data-mydata /usr/local/mysql/data ext4 defaults 1 2" >> /etc/fstab
tail -n1 /etc/fstab
/dev/mapper/data-mydata /usr/local/mysql/data ext4 defaults 1 2
第十二步,停掉數據庫。
/etc/init.d/mysqld stop
ERROR! MySQL server PID file could not be found!
ps -ef | grep mysqld
pkill -9 mysql
ps -ef | grep mysqld
chown mysql.mysql /usr/local/mysql/data/ -R
ll /usr/local/mysql/data/
ll /usr/local/mysql/data/ -d
第十三步,恢復數據。
tar -xPvf mysql01.tar.gz
第十四步,啟動數據庫,登錄MySQL,然後查看數據是否丟失。
/etc/init.d/mysqld start
Starting MySQL SUCCESS!
mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.29-log Source distribution
查看數據。
mysql> use larrydb;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_larrydb |
+-------------------+
| class |
| stu |
+-------------------+
2 rows in set (0.00 sec)
mysql> select * from class;
+------+--------+
| cid | cname |
+------+--------+
| 1 | linux |
| 2 | oracle |
+------+--------+
2 rows in set (0.01 sec)
mysql> select * from stu;
+------+---------+------+
| sid | sname | cid |
+------+---------+------+
| 1 | larry01 | 1 |
| 2 | larry02 | 2 |
+------+---------+------+
2 rows in set (0.00 sec)
第十五步,使用LVS的快照功能創建快照,快照不需要格式化。
lvcreate -L 100M -s -n smydata /dev/data/mydata
Logical volume "smydata" created
第十六步,掛載。
mount /dev/data/smydata /mnt
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 2.4G 6.8G 27% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 161M 3.7G 5% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/mapper/data-mydata
2.0G 98M 1.8G 6% /usr/local/mysql/data
/dev/mapper/data-smydata
2.0G 98M 1.8G 6% /mnt
第十七步,模擬數據丟失和驗證快照的數據不會受本身數據的影響。
cd /mnt
ls
# 進入數據目錄,創建一個文件
cd /usr/local/mysql/data/
touch aa01.txt
# 進入快照掛載目錄,發現沒有這個文件
ls aa01.txt
ls: cannot access aa01.txt: No such file or directory
第十八步,備份數據。
cd /databackup/
ll
total 976
-rw-r--r--. 1 root root 995761 Sep 10 17:47 mysql01.tar.gz
/etc/init.d/mysqld status
SUCCESS! MySQL running (2198)
tar -cvzf mysql02.tar.gz /mnt
模擬數據丟失。
rm -rf /usr/local/mysql/data/*
etc/init.d/mysqld stop
ERROR! MySQL server PID file could not be found!
pkill -9 mysql
ps -ef | grep mysqld | grep grep -v
cd /usr/local/mysql/data/
ll
total 0
第十九步,恢復數據,啟動數據庫,登錄MySQL,然後查看數據是否丟失。
tar -xvf /databackup/mysql02.tar.gz
ls
mnt
cd mnt/
mv ./* ../
cd ..
ls
/etc/init.d/mysqld start
Starting MySQL SUCCESS!
mysql
查看數據。
mysql> use larrydb;
Database changed
mysql> select * from class;
+------+--------+
| cid | cname |
+------+--------+
| 1 | linux |
| 2 | oracle |
+------+--------+
2 rows in set (0.00 sec)
mysql> select * from stu;
+------+---------+------+
| sid | sname | cid |
+------+---------+------+
| 1 | larry01 | 1 |
| 2 | larry02 | 2 |
+------+---------+------+
2 rows in set (0.00 sec)
–EOF–