使用 innobackupex 做 mysql 的全备

安装备份工具

yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
yum -y install percona-xtrabackup-24.x86_64

编写脚本

mkdir /root/shell
vim mysqlbackup.sh
#!/bin/bash
#定义数据库信息
dbname='ma'
dbuser='root'
dbpassword='[email protected]#mysql2018'
#定义备份目录及日志
backup_dir='/home/mysql_data/data'
backup_log='/home/mysql_data/logs/mysql_backup_log.log'
#定义时间
ymtime=`date +%Y-%m`
dtime=`date +%d`
#定义输出内容
wlog(){
  echo -e "`date "+%F %T"` DBname: $1 State: $2\n" >> $backup_log
}
#判断备份目录是否存在,不存在则创建
[ ! -d $backup_dir/$ymtime ] && mkdir -p $backup_dir/$ymtime
#开始备份
cd $backup_dir/$ymtime && /usr/bin/innobackupex --user=$dbuser --password=$dbpassword --no-timestamp $dbname > /dev/null 2>&1
#判断是否备份成功,输出相应信息到日志
if [ $? -eq 0 ];then
  wlog $dbname 'Backup success.'
  dbakfile=$dbname.$dtime.tar.gz
  tar zcf $dbakfile $dbname --remove-files
  [ $? -eq 0 ] && wlog $dbakfile 'Packaging success.' || wlog $dbakfile 'Packaging failed.'
else
  wlog $dbname 'Backup failed.'
fi

授予权限并运行测试效果

chmod 755 mysqlbackup.sh
sh -x mysqlbackup.sh

配置定时任务

crontab -e
0 0 * * 7 /root/shell/mysqlbackup.sh
vim /etc/crontab
0 0 * * 7 root /root/shell/mysqlbackup.sh
mysqlinnobackupexmysql全备mysql备份

我来吐槽

*

*