备份脚本

  • 全量备份脚本

  • 增量备份脚本

  • 全量恢复

  • 增量恢复

全量备份脚本

  1. [root@xucl-test-03 mysql_script]# cat fullbak.sh
  2. #!/bin/sh
  3. # add xucl
  4. INNOBACKUPEX=innobackupex
  5. INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
  6. TODAY=`date +%Y%m%d%H%M`
  7. YESTERDAY=`date -d"yesterday" +%Y%m%d%H%M`
  8. USEROPTIONS="--user=root --password=123456"
  9. TMPFILE="/dbbak/mysql_logs/innobackup_$TODAY.$$.tmp"
  10. MYCNF=/etc/my.cnf
  11. MYSQL=/usr/local/mysql/bin/mysql
  12. MYSQLADMIN=/usr/local/mysql/bin/mysqladmin
  13. BACKUPDIR=/dbbak/mysql_data # 备份的主目录
  14. FULLBACKUPDIR=$BACKUPDIR/full # 全库备份的目录
  15. INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录
  16. KEEP=1 # 保留几个全库备份
  17. # 获取开始时间
  18. #############################################################################
  19. # 打印错误信息并退出
  20. #############################################################################
  21. error()
  22. {
  23. echo "$1" 1>&2
  24. exit 1
  25. }
  26. # 开始备份前检查相关的参数
  27. if [ ! -x $INNOBACKUPEXFULL ]; then
  28. error "$INNOBACKUPEXFULL does not exist."
  29. fi
  30. if [ ! -d $BACKUPDIR ]; then
  31. error "Backup destination folder: $BACKUPDIR does not exist."
  32. fi
  33. if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then
  34. error "HALTED: MySQL does not appear to be running."
  35. fi
  36. if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then
  37. error "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)."
  38. fi
  39. # 输出备份信息
  40. echo "----------------------------"
  41. echo
  42. echo "$0: MySQL backup script"
  43. echo "started: `date`"
  44. echo
  45. # 如果备份目录不存在则创建相应的全备增备目录
  46. for i in $FULLBACKUPDIR $INCRBACKUPDIR
  47. do
  48. if [ ! -d $i ]; then
  49. mkdir -pv $i
  50. fi
  51. done
  52. # 压缩上传前一天的备份
  53. echo "压缩前一天的备份,scp到远程主机"
  54. cd $BACKUPDIR
  55. tar -zcvf $YESTERDAY.tar.gz ./full/ ./incr/
  56. # scp -P 8022 $YESTERDAY.tar.gz root@192.168.10.46:/data/backup/mysql/
  57. #if [ $? = 0 ]; then
  58. rm -rf $BACKUPDIR/full $BACKUPDIR/incr
  59. echo "开始全量备份"
  60. innobackupex --defaults-file=$MYCNF $USEROPTIONS $FULLBACKUPDIR > $TMPFILE 2>&1
  61. #else
  62. # echo "远程备份失败"
  63. #fi
  64. if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
  65. echo "$INNOBACKUPEX failed:"; echo
  66. echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
  67. # cat $TMPFILE
  68. # rm -f $TMPFILE
  69. exit 1
  70. fi
  71. # 这里获取这次备份的目录
  72. THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
  73. echo "THISBACKUP=$THISBACKUP"
  74. #rm -f $TMPFILE
  75. echo "Databases backed up successfully to: $THISBACKUP"
  76. # Cleanup
  77. echo "delete tar files of 3 days ago"
  78. find $BACKUPDIR/ -mtime +3 -name "*.tar.gz" -exec rm -rf {} \;
  79. echo
  80. echo "completed: `date`"
  81. exit 0

增量备份脚本

  1. [root@xucl-test-03 mysql_script]# cat incrbak.sh
  2. #!/bin/sh
  3. # add xucl
  4. INNOBACKUPEX=innobackupex
  5. INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
  6. TODAY=`date +%Y%m%d%H%M`
  7. USEROPTIONS="--user=root --password=123456"
  8. TMPFILE="/dbbak/mysql_logs/incr_$TODAY.$$.tmp"
  9. MYCNF=/etc/my.cnf
  10. MYSQL=/usr/local/mysql/bin/mysql
  11. MYSQLADMIN=/usr/local/mysql/bin/mysqladmin
  12. BACKUPDIR=/dbbak/mysql_data # 备份的主目录
  13. FULLBACKUPDIR=$BACKUPDIR/full # 全库备份的目录
  14. INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录
  15. #############################################################################
  16. # 打印错误信息并退出
  17. #############################################################################
  18. error()
  19. {
  20. echo "$1" 1>&2
  21. exit 1
  22. }
  23. # 开始备份前检查相关的参数
  24. if [ ! -x $INNOBACKUPEXFULL ]; then
  25. error "$INNOBACKUPEXFULL does not exist."
  26. fi
  27. if [ ! -d $BACKUPDIR ]; then
  28. error "Backup destination folder: $BACKUPDIR does not exist."
  29. fi
  30. if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then
  31. error "HALTED: MySQL does not appear to be running."
  32. fi
  33. if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then
  34. error "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)."
  35. fi
  36. # 输出备份信息
  37. echo "----------------------------"
  38. echo
  39. echo "$0: MySQL backup script"
  40. echo "started: `date`"
  41. echo
  42. # 如果备份目录不存在则创建相应的全备增备目录
  43. for i in $FULLBACKUPDIR $INCRBACKUPDIR
  44. do
  45. if [ ! -d $i ]; then
  46. mkdir -pv $i
  47. fi
  48. done
  49. # 查找最近的全备目录
  50. LATEST_FULL=`find $FULLBACKUPDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n"`
  51. echo "LATEST_FULL=$LATEST_FULL"
  52. # 如果最近的全备仍然可用执行增量备份
  53. # 创建增量备份的目录
  54. TMPINCRDIR=$INCRBACKUPDIR/$LATEST_FULL
  55. mkdir -p $TMPINCRDIR
  56. BACKTYPE="incr"
  57. # 获取最近的增量备份目录
  58. LATEST_INCR=`find $TMPINCRDIR -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`
  59. echo "LATEST_INCR=$LATEST_INCR"
  60. # 如果是首次增量备份,那么basedir则选择全备目录,否则选择最近一次的增量备份目录
  61. if [ ! $LATEST_INCR ] ; then
  62. INCRBASEDIR=$FULLBACKUPDIR/$LATEST_FULL
  63. else
  64. INCRBASEDIR=$LATEST_INCR
  65. fi
  66. echo "Running new incremental backup using $INCRBASEDIR as base."
  67. innobackupex --defaults-file=$MYCNF $USEROPTIONS --incremental $TMPINCRDIR --incremental-basedir $INCRBASEDIR > $TMPFILE 2>&1
  68. if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
  69. echo "$INNOBACKUPEX failed:"; echo
  70. echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
  71. exit 1
  72. fi
  73. # 这里获取这次备份的目录
  74. THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
  75. echo "THISBACKUP=$THISBACKUP"
  76. echo
  77. echo "Databases backed up successfully to: $THISBACKUP"
  78. echo
  79. echo "incremental completed: `date`"
  80. exit 0

目录结构

  1. [root@xucl-test-03 dbbak]# ls
  2. mysql_data mysql_logs mysql_script

其中mysql_data存放的是数据备份,mysql_logs存放的是备份的日志,mysql_script存放的是全量和增量备份的脚本。

crontab参考如下:

  1. [root@Mariadb-03 mysql_script]# crontab -l
  2. 0 4 * * 3,6 sh /dbbackup/mysql_script/full.sh > /dev/null 2>&1
  3. 0 4 * * 1,2,4,5,7 sh /dbbackup/mysql_script/incr.sh > /dev/null 2>&1

每周三、周六执行全备,其他时间增量备份,备份时间点在凌晨4点

恢复

  • 全量恢复

1.#停mysql服务

  1. sudo /etc/init.d/mysqld stop

2.# 删除data目录下的内容

  1. sudo rm rf /storage/mysql/data/*

3.# 编辑/etc/my.cnf 加入basedir和datadir 否则innobackupex: Error: Original data directory ‘.’ is not empty! at /usr/bin/innobackupex line 2163

  1. basedir = /usr/local/mysql
  2. datadir = /storage/mysql/data

4.# innobackupex恢复

  1. innobackupex --defaults-file=/etc/my.cnf --apply-log /storage/dump/ --use-memory=3G
  2. innobackupex --defaults-file=/etc/my.cnf --copy-back /storage/dump/ --use-memory=3G

5.# 修改权限

  1. chown -R mysql:mysql /storage/mysql

6.#启动mysqld

  1. mysqld_safe --defaults-file=/etc/my.cnf 2>&1 > /dev/null &

7.#跳过认证修改密码

  1. sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
  2. mysql uroot p #登录修改密码

8.#关闭mysqld_safe

  1. sudo /usr/local/mysql/bin/mysqladmin shutdown -uroot p
  2. /usr/local/mysql5719/bin/mysqld_safe --defaults-file=/storage/data/my.cnf &
  • 增量恢复
    大体步骤同全量恢复,多了合并的步骤
  1. 全备目录:/dbbak/full/2018-02-02_10-04-08
  2. 增量备份目录:/dbbak/incr/2018-02-02_10-04-08/2018-02-02_12-44-10

全备应用redo

  1. innobackupex --defaults-file=/etc/my.cnf --user=root --password=xxxxxx --apply-log --redo-only /dbbak/full/2018-02-02_10-04-08

合并增量备份

  1. innobackupex --defaults-file=/etc/my.cnf --user=root --password=xxxxxx --apply-log --redo-only /dbbak/full/2018-02-02_10-04-08 --incremental-dir=/dbbak/incr/2018-02-02_10-04-08/2018-02-02_12-44-10 --use-memory=8G

目录拷贝

  1. innobackupex --defaults-file=/etc/my.cnf --user=root --password=xxxxxx --copy-back /dbbak/full/2018-02-02_10-04-08 --use-memory=8G

后续步骤同全量恢复