数据库备份及还原实战

因某个业务使用的磁盘较小而且容量即将用完,因此将数据库目录更换到一个新的比较大的磁盘上,新的磁盘是对虚拟机动态添加的一块200G的磁盘,需要操作系统能够在不重启的情况下动态识别新添加的磁盘并将MySQL的数据目录进行备份并还原,即做到基本不影响业务的运行,本次使用XtraBacku,具体实现如下:

1.1:添加一块较大的磁盘并分区:

1.1.2:操作系统动态识别新硬盘:

# ll /sys/class/scsi_host/host
host0/ host1/ host2/
# echo “- – -” > /sys/class/scsi_host/host0/scan #重新扫描总线上的磁盘
# echo “- – -” > /sys/class/scsi_host/host1/scan
# echo “- – -” > /sys/class/scsi_host/host2/scan
# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0006ca14

Device Boot Start End Blocks Id System
/dev/sda1 2048 2099199 1048576 82 Linux swap / Solaris
/dev/sda2 * 2099200 20971519 9436160 83 Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c88d6

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux

Disk /dev/sdc: 214.7 GB, 214748364800 bytes, 419430400 sectors #识别到新添加的磁盘
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

1.1.3:分区及格式化,也可以不分区直接使用的:

# fdisk /dev/sdc
#分区过程略。。。。
# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=256 agcount=4, agsize=13107136 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=52428544, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=25599, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

1.1.4:先临时挂在到某个路径:

# mount /dev/sdc1  /mnt/

1.2:安装XtraBackup并执行备份及还原:

# yum install -y  https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.5/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.5-1.el7.x86_64.rpm 
# /etc/init.d/zabbix_server  stop #停止服务,不然备份之后再写入的数据需要其他方式找回
# innobackupex --user=root --password=xxx /mnt/ #备份当前数据,全部20G数据用了3分钟最有备份完毕,比使用mysqldump快了不止一个级别
# innobackupex  --apply-log /mnt/2017-02-14_11-16-36/ #执行整理,也就一分钟
# mkdir /mnt/mysql #新的mysql目录
# chown  mysql.mysql /mnt/mysql/ #更改权限为mysql
# vim /etc/my.cnf #将数据目录更改为新的目录
    datadir=/mnt/mysql #innobackupex读取my.cfg文件数据复制到新目录
# innobackupex  --copy-back /mnt/2017-02-14_11-16-36 #执行copy还原,
# umount /mnt/
# umount /data/
# mount /dev/sdc1  /data/  #将新的mysql数据磁盘挂载到/data作为以后的mysql数据目录
# vim /etc/my.cnf #更改为/data,我喜欢将mysql数据目录配置在这个路径
    datadir=/data/mysql
# chown  mysql.mysql /data/ -R
# /etc/init.d/mysqld  start
# /etc/init.d/zabbix_server  start
# /etc/init.d/zabbix_agentd   start

1.3:挂载后的新磁盘使用情况:

1.4:配置磁盘开机自动挂载:

# blkid  /dev/sdc1      /dev/sdc1: UUID="7ec48cc7-7cb6-4f6b-83da-c8a831159c14" TYPE="xfs # vim /etc/fstab     UUID=7ec48cc7-7cb6-4f6b-83da-c8a831159c14 /data      xfs     defaults        0 0