• 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 ''
  • 相关阅读:
    gin使用validator库参数校验若干实用技巧
    在gin框架中使用JWT
    使用zap接收gin框架默认的日志并配置日志归档
    gin框架路由拆分与注册
    Gin框架介绍及使用
    GO学习-(39) 优雅地关机或重启
    GO学习-(38) Go语言结构体转map[string]interface{}的若干方法
    WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
    [ 夜间模式 ] NightVersion
    HDU1518 Square(DFS)
  • 原文地址:https://www.cnblogs.com/hzm112567/p/3936774.html
Copyright © 2020-2023  润新知