• mysql全量备份和增量备份脚本


    vim   MYSQL_FULLBAK.sh                        //全量备份 ,每周一次

    1. #!/bin/bash
    2. #set -x
    3. ############################################################
    4. if [ $# -ne 2 ];then
    5. echo "Usage:依次输入参数: ";
    6. exit 1
    7. fi
    8. if ! ps -ef | grep -v "grep"|grep -q "mysqld ";then
    9. echo "未检测到mysql进程"
    10. exit 1
    11. fi
    12. ############################################################
    13. # 入参
    14. PASSWD=$1
    15. BAKDIR=$2
    16. ############################################################
    17. # PORT && HOST && MYSQLHOME && BAKDIR
    18. PORT=$(netstat -lanp | grep LISTEN | grep "mysqld"|awk -F ":" 'NR==1{print $4}')
    19. MYSQL_HOME=$(ps -ef | grep -v "grep"|grep "mysqld "|tr -s ' ' '\n'|grep "\-\-basedir"|awk -F "=" '{print $NF}')
    20. PATH=$MYSQL_HOME/bin:$PATH
    21. localnetcard=$(route -n|grep UG|head -n 1|awk '{print $NF}')
    22. if [ ! "$localnetcard" == "" ];then
    23. HOST=$(ip -4 addr show $localnetcard|awk -F '[/ ]' '{for(i=1;i<=NF;i++){if($i~"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"){print $i;break}}}'|awk 'NR==1{print $0}')
    24. fi
    25. if [ ! -d $BAKDIR ];then
    26. mkdir -p $BAKDIR
    27. chown -Rf mysql:mysql $BAKDIR
    28. fi
    29. ############################################################
    30. # 主从
    31. LANG=C
    32. MYSQL_CONN="mysql -uroot -p$PASSWD -h$HOST -P$PORT -s -e"
    33. MHOST=$($MYSQL_CONN "show slave status\G" | grep -i "Master_Host:")
    34. if [ "X$MHOST" != "X" ];then
    35. echo "current is Slave, got Master Info..."
    36. MPORT=$($MYSQL_CONN "show slave status\G" | grep -i "Master_Port:")
    37. if [ "X$MPORT" != "X" ]; then
    38. HOST=$(echo $MHOST | awk '{ print $2 }')
    39. PORT=$(echo $MPORT | awk '{ print $2 }')
    40. MYSQL_CONN="mysql -uroot -p$PWD -h$HOST -P$PORT -s -e"
    41. fi
    42. fi
    43. ############################################################
    44. # 备份时间
    45. TIME=$(date +"%Y%m%d%H%M%S")
    46. # 备份文件名
    47. DUMPFILE=FULL_$TIME.sql
    48. GZDUMPFILE=FULL_$TIME.sql.tgz
    49. ############################################################
    50. # 开始备份
    51. cd $BAKDIR
    52. LAST=$(date +"%Y年%m月%d日 %H:%M:%S")
    53. mysqldump -uroot -p$PASSWD -h$HOST -P$PORT --quick --events --all-databases >$DUMPFILE 2>&1
    54. if [ $? -eq 0 ];then
    55. tar -czf $GZDUMPFILE $DUMPFILE 2>&1
    56. rm -f $DUMPFILE
    57. echo "***********$GZDUMPFILE******************"
    58. echo "***********${LAST}全量备份成功******************"
    59. else
    60. rm -f $DUMPFILE
    61. echo "***********${LAST}全量备份失败******************"
    62. exit 1
    63. fi
    64. ############################################################
    65. # 清理备份,保留1个月
    66. COUNT=$(ls -lrt *.tgz | wc -l)
    67. if [ "$COUNT" -gt 4 ];then
    68. FILE=$(ls -lrt *.tgz |awk 'NR==1{print $NF}')
    69. rm -f $FILE
    70. fi
    71. # 删除增量
    72. #if [ -d "/data/backup/increment" ];then
    73. #rm -f /data/backup/increment/*
    74. #fi

    vim  MYSQL_INCREMENT_BAK.sh                           //增量备份,每天一次 

     

    1. #!/bin/bash
    2. #set -x
    3. ############################################################
    4. if [ $# -ne 2 ];then
    5. echo "Usage:依次输入参数: ";
    6. exit 1
    7. fi
    8. if [ "$UID" != 0 ];then
    9. echo "must be root"
    10. exit 1
    11. fi
    12. if ! ps -ef | grep -v "grep"|grep -q "mysqld ";then
    13. echo "未检测到mysql进程"
    14. exit 1
    15. fi
    16. ############################################################
    17. # 入参
    18. PASSWD=$1
    19. BAKDIR=$2
    20. ############################################################
    21. # PORT && HOST && MYSQLHOME && BAKDIR
    22. PORT=$(netstat -lanp | grep LISTEN | grep "mysqld"|awk -F ":" 'NR==1{print $4}')
    23. MYSQL_HOME=$(ps -ef | grep -v "grep"|grep "mysqld "|tr -s ' ' '\n'|grep "\-\-basedir"|awk -F "=" '{print $NF}')
    24. PATH=$MYSQL_HOME/bin:$PATH
    25. localnetcard=$(route -n|grep UG|head -n 1|awk '{print $NF}')
    26. if [ ! "$localnetcard" == "" ];then
    27. HOST=$(ip -4 addr show $localnetcard|awk -F '[/ ]' '{for(i=1;i<=NF;i++){if($i~"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"){print $i;break}}}'|awk 'NR==1{print $0}')
    28. fi
    29. if [ ! -d $BAKDIR ];then
    30. mkdir -p $BAKDIR
    31. chown -Rf mysql:mysql $BAKDIR
    32. fi
    33. ############################################################
    34. # 主从
    35. LANG=C
    36. MYSQL_CONN="mysql -uroot -p$PASSWD -h$HOST -P$PORT -s -e"
    37. MHOST=$($MYSQL_CONN "show slave status\G" | grep -i "Master_Host:")
    38. if [ "X$MHOST" != "X" ];then
    39. echo "current is Slave, got Master Info..."
    40. MPORT=$($MYSQL_CONN "show slave status\G" | grep -i "Master_Port:")
    41. if [ "X$MPORT" != "X" ]; then
    42. HOST=$(echo $MHOST | awk '{ print $2 }')
    43. PORT=$(echo $MPORT | awk '{ print $2 }')
    44. MYSQL_CONN="mysql -uroot -p$PASSWD -h$HOST -P$PORT -s -e"
    45. fi
    46. fi
    47. ############################################################
    48. if $MYSQL_CONN "show variables like 'log_%';" |egrep -i -q "log_bin[[:space:]]ON";then
    49. # binlog生成目录
    50. BINDIR=$($MYSQL_CONN "show variables like 'log_%';" |egrep -i "log_bin_basename"|awk '{print $NF}'|awk -F "mysql-bin|" '{print $1}')
    51. # binlog索引
    52. BINFILE=$($MYSQL_CONN "show variables like 'log_%';" |egrep -i "log_bin_index"|awk '{print $NF}')
    53. TIME=$(date +"%Y年%m月%d日 %H:%M:%S")
    54. # 产生新的mysql-bin.00000*文件
    55. mysqladmin -uroot -p$PASSWD -h$HOST -P$PORT flush-logs
    56. if [ $? -ne 0 ];then
    57. echo "***********${TIME}增量备份失败******************"
    58. exit 1
    59. fi
    60. else
    61. echo "mysql 未开启binlog日志"
    62. exit 1
    63. fi
    64. ############################################################
    65. COUNT=$(wc -l $BINFILE |awk '{print $1}')
    66. i=0
    67. cat $BINFILE|while read line;do
    68. BINNAME=$(basename $line)
    69. i=$(expr $i + 1)
    70. if [ $i -ne $COUNT ];then
    71. dest=$BAKDIR/$BINNAME
    72. if [ ! -e $dest ];then
    73. cp $BINDIR/$BINNAME $BAKDIR
    74. if [ $? -eq 0 ];then
    75. echo "***********${TIME} $BINNAME增量备份成功******************"
    76. else
    77. echo "***********${TIME} $BINNAME增量备份失败******************"
    78. exit 1
    79. fi
    80. fi
    81. fi
    82. done

    //赋权

    chmod +x  /data/backup/*.sh

     

    定时任务

    crontab  -e
    #每个星期日凌晨23:00执行完全备份脚本
    0 23 * * 0   sh  /data/backup/MYSQL_FULLBAK.sh /back1 123456 >/dev/null 2>&1
    #周一到周六凌晨23:00做增量备份
    0 23 * * 1-6 sh /data/backup/MYSQL_INCREMENT_BAK.sh /back2 123456>/dev/null 2>&1
     

  • 相关阅读:
    Java之反射机制
    django计算机毕业设计基于安卓Android/微信小程序的移动电商平台系统APP-商品购物商城app
    Part2_扩展MATSIM_Subpart4_除个人车外的其他模式_第22章 汽车共享
    91. 存钱罐
    基于探针的分布式追踪工具
    华为认证云计算专家(HCIE-Cloud Computing)–多选题
    Redis缓冲区溢出及解决方案
    Python正则表达式一文详解+实例代码展示
    这一次,弄明白JS中的文件相关(二):HTTP请求头和响应头
    小程序AI智能名片商城系统直连:打造用户与企业无缝对接的新时代!
  • 原文地址:https://blog.csdn.net/weixin_42054864/article/details/133575940