- 备份过程
1 实现LVM
mkdir /data/{mysql,binlog}
vim /etc/fstab
mount /dev/vg0/lv_mysql /data/mysql
mount /dev/vg0/lv_binlog /data/binlog
chown -R mysql.mysql /data/
vim /etc/my.cnf
datadir=/data/mysql
log_bin=/data/binlog/mysql-bin
systemctl restart mariadb
2 mysql> flush tables with read lock 读锁
mysql>flush logs;
mysql>show master logs; 记录文件名和position。如mysql-bin.000004,245
3 快照
lvcreate -n lv_mysql_snap -L 1G -s -p r /dev/vg0/lv_mysql
4 mysql>unlock tables; 解锁
5 备份快照里的内容
mount -o nouuid,norecovery /dev/vg0/lv_mysql_snap /mnt
cp -a /mnt/ /backup/
umount /mnt
lvremove /dev/vg0/lv_mysql_snap
备份过程结束
二 还原过程
1 系统损坏
rm -rf /data/mysql/*
systemctl stop mariadb
2 还原备份的数据文件
cp -av /backup/* /data/mysql/
3 禁止用户连接
vim /etc/my.cnf、
skip_networking
systemctl start mariadb
或
iptables 禁止用户连接
4 恢复binlog中最新日志到数据库中
mysqlbinlog --start-position=245 mysql-bin.000004 > /backup/bin.sql
mysqlbinlog mysql-bin.000005 >> /backup/bin.sql
mysql < /backup/bin.sql
5 恢复用户访问
vim /etc/my.cnf、
skip_networking删除此行
systemctl start mariadb
或iptables 清除