mysql備份指令碼

來源:互聯網
上載者:User

#!/bin/bash
#Shell variables
SHELLNAME="mysqlbak"
SCRIPTDIR="/data/dbbackup/"
BACKUPDIR="/data/dbbackup/"
LOGFILE=$SCRIPTDIR"/"$SHELLNAME".log"

#Date/Time veriables
REMAINDAYS="3"
CDATE=$(date "+%Y-%m-%d")
CTIME=$(date "+%H-%M-%S")

#Rsync variables
RSYNCUSER="backup"
RSYNCPASS=$SCRIPTDIR"/rsyncpasswd"
RSYNCCMD="/usr/bin/rsync"
DHOST="192.168.1.12"
DDIR="expert"
SDIR=$CDATE

#mysqldump veriables
MYSQLUSER="root"
MYSQLPASS="123"
MYSQLDUMPCMD="/usr/bin/mysqldump"
DBNAME="blog"

#write log
writelog(){
    ERRINFO=$1
    echo "$CDATE $CTIME : ${SHELLNAME} : ${ERRINFO}" >> ${LOGFILE}
}

#alters the scheduling priority of shell
low_priority(){
    writelog "Begin renice priority of shell"
    renice +19 -p $$ >/dev/null 2>&1
    if [ $? -ne 0 ];then
        writelog "renice failed"
        return 1
    else
        writelog "renice success"
        return 0
    fi
}

#Delete old Backup Data with RemainDays
delete_dir(){
    writelog "begin to delete old dir"
    cd $BACKUPDIR
    find ./ -maxdepth 1 -name "2011*" -type d -mtime +$REMAINDAYS -exec rm -rf {} \;
    if [ $? -ne 0 ];then
        writelog "delete old dir failed"
        return 1
    else
        writelog "delete old dir success"
        return 0
    fi
}

#Copy the Backup Data to Backup Server
rsync_file(){
    writelog "begin to rsync file"
    cd $BACKUPDIR;
    $RSYNCCMD -vzrtopg $SDIR $RSYNCUSER@$DHOST:DDIR --password-file=$RSYNCPASS
    if [ $? -ne 0 ];then
        writelog "rsync $SFILE to $DHOST failed"
        return 1;
    else
        writelog "rsync $SFILE to $DHOST success"
        return 0;
    fi
}

mysql_dump(){
    writelog "begin to mysqldump"
    MYSQLDUMPOPTION="-u "$MYSQLUSER" -p"$MYSQLPASS" --hex-blob -R  --no-autocommit"
    FILENAME="$DBNAME"_"$CDATE"_"$CTIME"".sql"
   
    cd $BACKUPDIR
    $MYSQLDUMPCMD $MYSQLDUMPOPTION $DBNAME > $FILENAME
    if [ $? -ne 0 ];then
        writelog "mysqldump $DBNAME failed"
        return 1
    else
        writelog "mysqldump $DBNAME success"
    fi

    tar -zcvf  $FILENAME".tgz" $FILENAME
    if [ $? -ne 0 ];then
        writelog "tar -zcvf  "$FILENAME".tgz $FILENAME failed"
        return 1
    else
        writelog "tar -zcvf  "$FILENAME".tgz $FILENAME success"
    fi
    rm -f $FILENAME
    mkdir -p $CDATE
    md5sum $FILENAME".tgz" > $CDATE/$FILENAME".tgz.MD5"
    mv $FILENAME".tgz" $CDATE
}

############ main ##################
main(){
    low_priority;
    mysql_dump;
    if [ $? -ne 0 ];then
        return 1
    fi
    rsync_file ;
    if [ $? -ne 0 ];then
        return 1
    fi
    delete_dir;
    if [ $? -ne 0 ];then
        return 1
    fi
    return 0
}

main

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.