zoukankan      html  css  js  c++  java
  • linux mysql备份shell

     

    #!/bin/bash
    # Shell script to backup MySql database
    # Author: Henry he
    # Last updated: 2014-08-25
    # crontab -e
    # 0 8 * * 1 /home/elkan/H_Docs/PortMasterListPortal/backup.sh >> /opt/mysql_backup/log
    
    echo $(date +"%Y-%m-%d %H:%M:%S")
    
    username="root"     # USERNAME
    password="123456"       # PASSWORD 
    Hostname="localhost"          # Hostname
    DBName="PortMasterList" #DB name
    #DBName="testMavenWeb"
    programProt="8080"
    
    #max save backup file number
    maxBackUpFileCount=5
    
    # Linux bin paths, change this if it can not be autodetected via which command
    MYSQL="$(which mysql)"
    MYSQLDUMP="$(which mysqldump)"
    CHOWN="$(which chown)"
    CHMOD="$(which chmod)"
    GZIP="$(which gzip)"
    
    # Main directory where backup will be stored
    backupDir="/opt/mysql_backup/"$DBName
    
    # if not exsis then mkdir
    [ ! -d $backupDir ] && mkdir -p $backupDir || :
    
    # Get data in dd-mm-yyyy format
    NOW="$(date +"%Y-%m-%d-%H-%M-%S")"
    
    FILE="$backupDir/$DBName.$NOW.bak"
    
    # Only root can access it!
    #$CHOWN 0.0 -R $backupDir
    #$CHMOD 0600 $backupDir
    
    # connect to mysql using mysqldump for select mysql database
    # and pipe it out to file in backup dir
    echo 'usenrame:'$username 'password:'$password 'DBName:'$DBName
    $MYSQLDUMP -u $username -h $Hostname -p$password $DBName > $FILE
    
    # check the backup is success
    success="true"
    if [ ! -s "$FILE" ]; then 
        success="false"
        rm -f $FILE
        echo 'backup fail'
    else
        # delete the redundant backup files
        i=0
        for file in $(ls -t $backupDir)
        do
            i=`expr $i + 1`
            #echo $file
            if [ "$i" -gt "$maxBackUpFileCount" ]
            then
                $(rm -f "$backupDir/$file")
                echo 'rm file':"$backupDir/$file"
            fi
        done
    
        echo 'backup success file path:'$FILE
    fi
    
    #curl calling java to send email
    curl 'http://'$Hostname':'$programProt'/PortMasterListPortal/ajax/sendBackup.html?success='$success'&secureKey=xxx'
    echo ''
  • 相关阅读:
    为什么股票一买就跌,一卖就涨?终于找到答案了!
    搜集的一些股票讲师的博客
    一位操盘手的临别赠言
    VMware网络连接 桥接、NAt、host-only模式
    我常用的网络测试工具
    linux下性能测试工具netperf使用
    vm10虚拟机安装Mac OS X10.10教程
    ACE_Svc_Handler 通信原理
    mypwd实现
    2019-2020-1 20175307 20175308 20175319 实验五 通讯协议设计
  • 原文地址:https://www.cnblogs.com/hzm112567/p/3936774.html
Copyright © 2011-2022 走看看