每天一次dump数据库并压缩的shell脚本

#! /bin/sh


DBSERVER=192.168.1.236
USER=xxx
PASS=xxx
OUTPUT_DIR=data
OLD_DIR=old_data
LOG_FILE=log_dump.txt


#默认的数据库
default_all_dbs=('open_account'  'xx'  'xxxx' 'xxxxx' 'xxxxxx' 'xxxxxxxxxx' 'xxxxxxx')


MYSQLDUMPBIN_PATH='/software/mysql-5.6/bin/mysqldump'


function log_message()
{
    echo $1 $2 $3 >> ${LOG_FILE}
    #echo $1 $2 $3 
}


function dump_db()
{
    DATABASE=$1
    FILE=${DATABASE}.`date +"%Y%m%d"`.sql
    log_message "begin to dump db ${DATABASE} to ${FILE}.gz ."
    #use this command for a database server on remote.
    ${MYSQLDUMPBIN_PATH} --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${FILE}
    gzip $FILE
    #log_message "${FILE}.gz was created."
    #ls -al ${FILE}.gz
}


function dump_table()
{
    DATABASE=$1
    TABLE=$2
    FILE=${DATABASE}.${TABLE}.`date +"%Y%m%d"`.sql
    log_message "begin to dump table ${TABLE} of db ${DATABASE} to ${FILE}.gz ."
    #use this command for a database server on remote.
    ${MYSQLDUMPBIN_PATH} --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} ${TABLE} > ${FILE}
    gzip $FILE
    #log_message "${FILE}.gz was created."
    #ls -al ${FILE}.gz
}




while true
do
    begin_time=`date +%s`
    log_message "Start dumpping:" "`date +'%Y-%m-%d %H:%M:%S'`" ".............................."


    if [ ! -d "$OLD_DIR" ]; then  
        mkdir ${OLD_DIR}
    fi  


    if [ ! -d "$OUTPUT_DIR" ]; then  
        mkdir ${OUTPUT_DIR}
    fi  


    rm -fr ${OLD_DIR}/*.sql.gz 
    mv ${OUTPUT_DIR}/*.sql.gz ${OLD_DIR} 


    #文件名本身是第0个参数,但不算在总参数个数里面
    if [ $# == 0 ] ; then
        log_message "Usage: sh dump.sh db default or sh dump.sh db db1 db2 or sh dump.sh table dbname table1 table2..." 
        exit 1 ;
    fi




    if [ $1 == "db" ]; then
        log_message "will dump dbs"
        if [ $2 == "default" ];then
            log_message "will dump all default dbs"
            for db in "${default_all_dbs[@]}"
            do
                dump_db $db
            done
        else
            log_message "will dump passed in dbs"
            i=1
            begin=2
            for db in "$@"
            do
                if [ "${i}" -ge "${begin}" ]; then
                        dump_db $db
                fi
                let i++
            done
        fi


    elif [ $1 == "table" ]; then
        db=$2
        log_message "will dump tables in db $db"
        i=1
        begin=3
        for table in "$@"
        do
            if [ "${i}" -ge "${begin}" ]; then
                    dump_table $db $table
            fi
            let i++
        done


    else
        log_message "Usage: sh dump.sh db default or sh dump.sh db db1 db2 or sh dump.sh table dbname table1 table2..." 
        exit 1 ;
    fi


    mv *.sql.gz ${OUTPUT_DIR}


    finish_time=`date +%s`
    log_message "Finish dumpping:" "`date +'%Y-%m-%d %H:%M:%S'`"


    seconds_cost=$[ finish_time - begin_time ]
    log_message "Total cost seconds:" "${seconds_cost}"
    log_message ""


    sleep 86400
done


你可能感兴趣的:(杂项)