15.xtrabackup

全量备份

1)全量备份恢复

###解压全量备份文件到指定目录/rds/mysql

xbstream -x -C /rds/mysql < full_backup.xbstream

innobackupex --decompress --parallel=6 --compress-threads=6 /rds/mysql



#应用日志

innobackupex --defaults-file=/rds/mysql/backup-my.cnf --use-memory=256M --apply-log /rds/mysql

#修改数据文件目录

vim /etc/mysql/mysql.conf.d/mysqld.conf

#修改数据目录权限

chown -R -f mysql:mysql /rds/mysql



# 启动mysql服务

service mysqld restart



2)全量+增量恢复

# 1. 恢复全备

xbstream -x -C /rds/mysql < full_backup.xbstream

innobackupex --decompress --parallel=6 --compress-threads=6 /rds/mysql

innobackupex --defaults-file=/rds/mysql/backup-my.cnf --use-memory=256M --apply-log /rds/mysql --redo-only


# 2. 恢复增备,如果有多个增备,重复这一步,注意修改/tmp/increase_backup的增备临时目录

xbstream -x -C /tmp/increase_backup < increase_backup.xbstream

innobackupex --decompress --parallel=6 --compress-threads=6 /tmp/increase_backup

innobackupex --defaults-file=/rds/mysql/backup-my.cnf --use-memory=256M --apply-log /rds/mysql --incremental-dir=/tmp/increase_backup --redo-only

rm -rf /tmp/increase_backup


# 3. apply log

innobackupex --defaults-file=/rds/mysql/backup-my.cnf --use-memory=256M --apply-log /rds/mysql

chown -R -f mysql:mysql /rds/mysql



# 4. 启动mysql服务

service mysqld restart





3)全量+增量指定库表恢复

# 2. 恢复增备,如果有多个增备,重复这一步,注意修改/tmp/increase_backup的增备临时目录

xbstream -x -C /tmp/increase_backup < increase_backup.xbstream

innobackupex --decompress --parallel=6 --compress-threads=6 /tmp/increase_backup



innobackupex --defaults-file=/rds/mysql/backup-my.cnf --use-memory=256M --apply-log /rds/mysql --incremental-dir=/tmp/increase_backup --redo-only

rm -rf /tmp/increase_backup


# 3. apply log

innobackupex --defaults-file=/rds/mysql/backup-my.cnf --use-memory=256M --apply-log /rds/mysql



#解压并追加日志,以上完成后,删除原有库表目录相应的表frm idb文件,拷贝/rds/mysql中恢复后的库表文件

#并修改文件权限chown +R mysql:mysql /rds/mysql



#完成以上步骤即可恢复数据





4)如果全量+增量未成达到要求,需要在全量+增量备份文件恢复的同时从binlog追加后续的增量数据

数据库备份

#! /bin/bash

# author linsheng

# version 0.0.1

# email [email protected]

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

export PATH

backup_dir="/dbs/backup/mysql"

restore_dir="$2"

mysql_cnf="/etc/mysql/mysql.conf.d/mysqld.cnf"

mysql_user="user"

mysql_password="password"

mysql_port=3306

mysql_sock=""

backup_application="/usr/bin/innobackupex"

backup_day_month=$(date +%F)

thread_count=16

backup_day_minute=$(date +%H)-$(date +%M)

echo $backup_day_month

echo $backup_day_minute

return_value=0

if [ $# -lt "1" ] ; then

  echo "arg error!"

fi

#全量备份

full_backup(){

  #判断每天备份目录,如果不存在就创建目录并备份

  if [ ! -d ${backup_dir}/$backup_day_month/full ]; then

    mkdir ${backup_dir}/$backup_day_month/full -p

    ${backup_application}  --defaults-file=${mysql_cnf} --no-timestamp --user=${mysql_user} --password=${mysql_password} --port=${mysql_port} --parallel=$thread_count --compress --compress-threads=$thread_count --stream=xbstream --extra-lsndir=${backup_dir}/$backup_day_month/full/xtrabackup_info ${backup_dir}/$backup_day_month/full/base.xbstream

    if [ `echo $?` -ne ${return_value} ]; then

      echo "Backup Error" > ${backup_dir}/$backup_day_month/full/MySQL_Innodb_backupex.log

    fi

  fi

}

#增量备份

incremental_backup(){

  echo "begin incremental_backup..."

  if [ ! -d ${backup_dir}/$backup_day_month/full ]; then

        full_backup

  fi

  ####如果不存在增量则添加增量

  if [ ! -d ${backup_dir}/$backup_day_month/incremental/lsndir ]; then

    mkdir ${backup_dir}/$backup_day_month/incremental/lsndir -p

    ${backup_application}  --defaults-file=${mysql_cnf} --no-timestamp --user=${mysql_user} --password=${mysql_password} --port=${mysql_port} --parallel=$thread_count --compress --compress-threads=$thread_count --stream=xbstream --extra-lsndir=${backup_dir}/$backup_day_month/incremental/lsndir --incremental  --incremental-basedir=${backup_dir}/$backup_day_month/full/xtrabackup_info  base>${backup_dir}/$backup_day_month/incremental/$backup_day_minute/incre.xbstream

    if [ `echo $?` -ne ${return_value} ]; then

      echo "Backup Error" > ${backup_dir}/$backup_day_month/incremental/MySQL_Innodb_backupex.log

    fi

  else

    ###如果存在则在增量基础上添加增量

# mkdir ${backup_day_minute}_incremental

        mv ${backup_dir}/$backup_day_month/incremental/lsndir ${backup_dir}/$backup_day_month/incremental/${backup_day_minute}_incremental

mkdir ${backup_dir}/$backup_day_month/incremental/$backup_day_minute/

    ${backup_application}  --defaults-file=${mysql_cnf} --no-timestamp --user=${mysql_user} --password=${mysql_password} --port=${mysql_port} --parallel=$thread_count --compress --compress-threads=$thread_count --stream=xbstream --extra-lsndir=${backup_dir}/$backup_day_month/incremental/lsndir --incremental  --incremental-basedir=${backup_dir}/$backup_day_month/incremental/${backup_day_minute}_incremental  base>${backup_dir}/$backup_day_month/incremental/$backup_day_minute/incre.xbstream

    if [ `echo $?` -ne ${return_value} ]; then

      echo "Backup Error" > ${backup_dir}/$backup_day_month/incremental/MySQL_Innodb_backupex.log

    fi

  fi

}

case $1 in

  Backup)

  if [ ! -d ${backup_dir}/$backup_day_month/full ]; then

    full_backup

  else

    incremental_backup

  fi

  ;;

  *)

  echo "Usage:$0(Backup|recovery)"

  ;;

esac

你可能感兴趣的:(15.xtrabackup)