zoukankan      html  css  js  c++  java
  • Mariadb使用xtrabackup工具备份数据脚本

    #!/bin/bash
    #这个脚本用来备份SQL文件;

    sql_home="/home/mysql"
    sql_bak_log="$sql_home/xtrabackup.log"
    server_conf="/usr/local/sunlight/conf/server.conf"
    sql_backup_date=$(date "+%Y%m%d")

    if [ -f $server_conf ];then
    while read line
    do
      eval "$line"
    done < $server_conf
    else
      dbuser="root"
      sql_passwd=""
      dbhost="127.0.0.1"
      sql_port="3306"
    fi

    sql_user="$dbuser"
    sql_passwd="$dbpass"
    sql_host="$dbhost"
    sql_port="$dbport"


    if [ -z "$sql_user" ];then
      echo "Error! sql user is empty!"
      echo "Error! sql user is empty!" >> $sql_bak_log
      exit 1
    fi

    if [ -z "$sql_passwd" ];then
      echo "Error! sql password is empty!"
      echo "Error! sql password is empty!" >> $sql_bak_log
      # exit 1
    fi

    if [ ! -d $sql_home ];then
      mkdir -p $sql_home
      chown mysql:mysql $sql_home
      chmod 700 $sql_home
    fi

    #check mysql daemon
    check_sql_daemon=$(mysql -h"$sql_host" -u"$sql_user"  --password="$sql_passwd" -e "select version();")
    if [ $? -ne 0 ];then
      echo "[ Error ] `date "+%Y/%m/%d %H:%M:%S"` [ msg ] We didn't find mysql daemon!"
      echo "[ Error ] `date "+%Y/%m/%d %H:%M:%S"` [ msg ] We didn't find mysql daemon!" >> $sql_bak_log
      exit 1
    fi

    #check xtrabackup package
    check_xtrabackup_rpm=$(rpm -qa|grep xtrabackup | wc -l)
    if [ $check_xtrabackup_rpm -ne 1 ];then
      echo "[ Error ] `date "+%Y/%m/%d %H:%M:%S"` [ msg ] xtrabackup does not be installed!"
      echo "[ Error ] `date "+%Y/%m/%d %H:%M:%S"` [ msg ] xtrabackup does not be installed!" >> $sql_bak_log
      exit 1
    fi

    if [ -e "$sql_home/${sql_backup_date}.tar.gz" ];then
      rm -f "$sql_home/${sql_backup_date}.tar.gz"
    fi

    sudo -u mysql innobackupex --user=$sql_user --password="$sql_passwd" --socket=/var/lib/mysql/mysql.sock --no-timestamp --stream=tar "$sql_home/" | gzip > "$sql_home/${sql_backup_date}.tar.gz"

    if [ $? -eq 0 ];then
      echo "[ Success ] `date "+%Y/%m/%d %H:%M:%S"` [ msg ] The tar.gz package has been finished !" >> $sql_bak_log
    else
      echo "[ Error ] `date "+%Y/%m/%d %H:%M:%S"` [ msg ] The first backup step has been failed !" >> $sql_bak_log
      echo "-------------------------------------------------------------------------------------------------" >> $sql_bak_log
      exit 1
    fi

    chown mysql:mysql  -R /home/mysql

    chmod 755 -R /home/mysql


    echo "[ Success ] `date "+%Y/%m/%d %H:%M:%S"` Mysql Files Backup Success! " >> $sql_bak_log
    echo "-------------------------------------------------------------------------------------------------" >> $sql_bak_log
    exit 0

  • 相关阅读:
    涡轮增压中冷器
    TortoiseSVN文件及文件夹图标不显示解决方法
    无线路由器无法登录管理界面,怎么办?
    TP-LINK-TL-WVR450G路由器经常断网
    阿里云DNS地址
    农村社保和职工社保能同时交吗?
    电视机顶盒遥控器可以同时遥控电视和机顶盒
    各种汽车的高端品牌
    金蝶云星空系统打印提示:对象不支持“SetGraphicsMode”属性或方法
    怎么去掉视频的背景音乐?
  • 原文地址:https://www.cnblogs.com/ralphdc/p/8283076.html
Copyright © 2011-2022 走看看