1,查看分区情况看看还剩多少空间
[root@centos7 backup]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
└─sda4 8:6 0 1G 0 part /app
sr0 11:0 1 8.1G 0 rom
#我这个磁盘200G,用了54G,还剩146G
2,创建1个新的分区,当做逻辑卷
fdisk /dev/sda
p 查看分区列表
n 创建分区
根据需要填写
t 修改id为逻辑卷
8e
p 再次确认
3,同步分区
partprobe #centos7这条命令就可搞定
4,查看分区是否出来
[root@ansible-7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 512B 0 part
├─sda5 8:5 0 20G 0 part /app
└─sda6 8:6 0 10G 0 part
sr0 11:0 1 8.1G 0 rom
1,将新分区加入PV卷
#加入pv卷
[root@ansible-7 ~]#pvcreate /dev/sda6
#查看PV卷
[root@ansible-7 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda6 lvm2 --- 10.00g 10.00g
2,将PV卷加入卷组
#加入卷组
[root@ansible-7 ~]#vgcreate vg0 /dev/sda6
#查看卷组
[root@ansible-7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 0 0 wz--n- <10.00g <10.00g
3,创建存放数据以及二进制日志文件的逻辑卷
#1,创建存放数据的逻辑卷
lvcreate -n mysqldata -L 3G vg0
#2,创建存放二进制日志文件的逻辑卷
lvcreate -n binlog -L 2G vg0
#查看创建情况
[root@ansible-7 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
binlog vg0 -wi-a----- 2.00g
mysqldata vg0 -wi-a----- 3.00g
"注意:PV卷的空间一定不要用完,因为需要有存放快照的空间"
4,创建文件系统
mkfs.xfs /dev/vg0/mysqldata
mkfs.xfs /dev/vg0/binlog
1,创建两个文件分别当挂载点
mkdir -pv /mysql/data
mkdir -pv /mysql/binlog
2,修改/etc/fstab文件
blkid 查看两个分区的UUID
#将两分区按照这个顺序填写:UUID 挂载点 文件系统 defaults 0 0 即可
vim /etc/fstab
UUID=d30a59eb-9a20-4376-a6ed-d47cc4d66d05 /mysql/data xfs defaults 0 0
UUID=ef9c6374-a9e0-457f-895a-bf456be0ed96 /mysql/binlog xfs defaults 0 0
3,挂载
[root@ansible-7 ~]# mount -a
[root@ansible-7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 512B 0 part
├─sda5 8:5 0 20G 0 part /app
└─sda6 8:6 0 10G 0 part
├─vg0-mysqldata 253:0 0 3G 0 lvm /mysql/data
└─vg0-binlog 253:1 0 2G 0 lvm /mysql/binlog
sr0 11:0 1 8.1G 0 rom
1,修改两个目录权限
chown -R mysql.mysql /mysql/
2,修改mysql配置文件
vim /etc/my.cnf
在[mysqld]这个语句块下修改这两项
[mysqld]
datadir=/mysql/data
log_bin=/mysql/binlog/mysql-binlog
3,重启mariadb服务
systemctl restart mariadb
systemctl enable mariadb
4,查看端口是否打开
[root@ansible-7 ~]# ss -ntl |grep 3306
LISTEN 0 50 *:3306 *:*
1,锁表防止备份时数据变动
flush tables with read lock;
2,记录二进制日志文件及事件位置
#1,刷新二进制日志
flush logs;
#2,查看当前二进制日志位置
show master status;
MariaDB [(none)]> show master logs;
+---------------------+-----------+
| Log_name | File_size |
+---------------------+-----------+
| mysql-binlog.000001 | 32644 |
| mysql-binlog.000002 | 31231291 |
| mysql-binlog.000003 | 245 |
+---------------------+-----------+
3 rows in set (0.00 sec)
3,将二进制日志文件当前备份位置存入文件保存,用作以后恢复查看
#1,创建存放备份数据,以及日志的目录
[root@ansible-7 data]#mkdir /backup/binlog
[root@ansible-7 data]#mkdir /backup/data
#2,二进制日志文件当前备份位置存放至/backup/binlog目录里
[root@ansible-7 ~]#mysql -uroot -p -e ‘show master logs‘ >/mysql/binlog/pos-`date +%F`.log
[root@ansible-7 ~]# cat /backup/binlog/pos-2018-03-07.log
Log_name File_size
mysql-binlog.000001 32644
mysql-binlog.000002 31231291
mysql-binlog.000003 245
#1,创建快照
[root@ansible-7 data]#lvcreate -L 3G -s -p r -n mysqldata-snapshoot /dev/vg0/mysqldata
命令解析:
-L:指定逻辑卷大小
-s:指定该逻辑卷是快照
-p:指定权限,r表示只有读权限
-n:指定该逻辑卷名字
/dav/vg0/mysqldata:表示针对该逻辑卷创建快照
#2,查看快照信息
[root@ansible-7 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/mysqldata-snapshoot
LV Name mysqldata-snapshoot
VG Name vg0
LV UUID CcaFOb-xLPT-pxh7-K9nP-MQP7-WFDm-gvPthA
LV Write Access read only
LV Creation host, time ansible-7, 2018-03-07 14:30:04 +0800
LV snapshot status active destination for mysqldata
LV Status available
# open 0
LV Size 3.00 GiB
Current LE 768
COW-table size 3.00 GiB
COW-table LE 768
Allocated to snapshot 0.00%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
unlock tables;
#1,创建临时挂载目录
[root@ansible-7 data]#mkdir /snapshoot
#2,挂载
[root@ansible-7 data]#mount -o nouuid,norecovery /dev/vg0/mysqldata-snapshoot /snapshoot
"注意:xfs文件系统快照挂载需要指定nouuid,norecovery这两个选项,ext4不需要"
tar cvf /backup/data/all-`date +%F`.tar /snapshoot
#1,取消挂载
[root@ansible-7 data]#umount /snapshoot
#2,删除快照逻辑卷
[root@ansible-7 data]# lvremove /dev/vg0/mysqldata-snapshoot
Do you really want to remove active logical volume vg0/mysqldata-snapshoot? [y/n]: y
Logical volume "mysqldata-snapshoot" successfully removed
(一),停止服务删除数据存放目录
systemctl stop mariadb
rm -rf /mysql/data/*
(二),恢复全备份数据
#1,解压缩
tar vxf /backup/data/all-2018-03-07.tar
#2,复制snapshoot目录下的所有文件至/mysql/data/目录下
cp -a snapshoot/* /mysql/data/
(三),用二进制日志还原至最新状态
1,查看之前存放在文件里的备份位置
cat /backup/binlog/pos-2018-03-07.log
Log_name File_size
mysql-binlog.000001 32644
mysql-binlog.000002 31231291
mysql-binlog.000003 245
之前备份的位置是mysql-binlog.000002文件的 31231291位置
所以我们需要将mysql-binlog.000002日志文件拷贝出来
2,重新生成二进制日志文件,
MariaDB [(none)]>flush logs
3,查看当前二进制日志文件位置
MariaDB [(none)]> show master logs;
+---------------------+-----------+
| Log_name | File_size |
+---------------------+-----------+
| mysql-binlog.000001 | 32644 |
| mysql-binlog.000002 | 312312 |
| mysql-binlog.000003 | 1212312 |
| mysql-binlog.000004 | 245 |
+---------------------+-----------+
3 rows in set (0.00 sec)
4,拷贝备份之后的相关二进制日志文件
cp -a /mysql/binlog/mysql-binlog.000003 /backup/binlog/
cp -a /mysql/binlog/mysql-binlog.000004 /backup/binlog/
5,将之前备份之后的内容取出来
mysqlbinlog --start-position=245 mysql-binlog.000003 >bin.sql
mysqlbinlog mysql-binlog.000004 >> bin.sql
(四),启动服务
systemctl start mariadb
(五),暂停记录二进制日志功能
set sql_log_bin=0;
(六),恢复二进制日志里的数据
MariaDB [(none)]> source /backup/binlog/bin.sql
(七),开启记录二进制日志功能
set sql_log_bin=1;
(八),恢复完了就可把拷贝的二进制日志文件删掉了,以免影响下次恢复
原文地址:http://blog.51cto.com/13598893/2084438