生產環境MysqlDatabase Backup指令碼

來源:互聯網
上載者:User

指令碼準系統:

自動壓縮備份mysql資料庫

自動刪除近10天前的備份檔案

刪除時顯示刪除進度


#!/bin/bash#Author absolutely.xu@gmail.comMAXIMUM_BACKUP_FILES=10BACKUP_FOLDERNAME="database_backup"DB_HOSTNAME="localhost"DB_USERNAME="root"DB_PASSWORD="123456"DATABASES=(            "openfire"            "csp")#=========echo "Bash Database Backup Tool"#CURRENT_DATE=$(date +%F)CURRENT_DATE=$(date +%F)BACKUP_FOLDER="${BACKUP_FOLDERNAME}_${CURRENT_DATE}"mkdir $BACKUP_FOLDER#Count the database.count=0while [ "x${DATABASES[count]}" != "x" ];do    count=$(( count + 1 ))doneecho "[+] ${count} databases will be backuped..."# Iterate over the database list and dump (in SQL) the content of echo one.for DATABASE in ${DATABASES[@]};do    echo "[+] Mysql-Dumping: ${DATABASE}"    echo -n "   Began:  ";echo $(date)    if $(mysqldump -h ${DB_HOSTNAME} -u${DB_USERNAME} -p${DB_PASSWORD} ${DATABASE} > "${BACKUP_FOLDER}/${DATABASE}.sql");then        echo "  Dumped successfully!"    else        echo "  Failed dumping this database!"    fi        echo -n "   Finished: ";echo $(date)doneechoecho "[+] Packaging and compressing the backup folder..."tar -cv ${BACKUP_FOLDER} | bzip2 > ${BACKUP_FOLDER}.tar.bz2 && rm -rf $BACKUP_FOLDERBACKUP_FILES_MADE=$(ls -l ${BACKUP_FOLDERNAME}*.tar.bz2 | wc -l)BACKUP_FILES_MADE=$(( $BACKUP_FILES_MADE - 0 )) ###Convert into integer number.echoecho "[+] There are ${BACKUP_FILES_MADE} backup files actually."if [ $BACKUP_FILES_MADE -gt $MAXIMUM_BACKUP_FILES ];then    REMOVE_FILES=$(( $BACKUP_FILES_MADE - $MAXIMUM_BACKUP_FILES ))echo "[+] Remove ${REMOVE_FILES} old backup files."    ALL_BACKUP_FILES=($(ls -t ${BACKUP_FOLDERNAME}*.tar.bz2))    SAFE_BACKUP_FILES=("${ALL_BACKUP_FILES[@]:0:${MAXIMUM_BACKUP_FILES}}")echo "[+] Safeting the newest backup files and removing old files..."    FOLDER_SAFETY="_safety"if [ ! -d $FOLDER_SAFETY ]then mkdir $FOLDER_SAFETY                                                                                                                   fifor FILE in ${SAFE_BACKUP_FILES[@]};do                                                                                                                         mv -i  ${FILE}  ${FOLDER_SAFETY}done    rm -rf ${BACKUP_FOLDERNAME}*.tar.bz2    mv  -i ${FOLDER_SAFETY}/* ./    rm -rf ${FOLDER_SAFETY}CHAR=''for ((i=0;$i<=100;i+=2))do  printf "Removing:[%-50s]%d%%\r" $CHAR $i        sleep 0.1CHAR=#$CHARdone    echofi

第1天備份資料庫功能測試如下

650) this.width=650;" title="bak1.jpg" alt="wKioL1LgihrDG2aFAAGMEm4f4aY541.jpg" src="http://www.bkjia.com/uploads/allimg/140207/2156326132-0.jpg" />


10天以後,會自動保留最近10天的備份,10天以上會自動刪除

類比測試指令碼功能,建立31天的備份檔案如下

650) this.width=650;" width="881" height="223" title="bak.jpg" style="width:711px;height:166px;" alt="wKiom1Lgi1KD0mgyAAOJ-HIh3ks724.jpg" src="http://www.bkjia.com/uploads/allimg/140207/2156322W9-1.jpg" />

每天計劃任務定時執行指令碼保留最近10天的備份

650) this.width=650;" width="857" height="443" title="bak.jpg" style="width:720px;height:407px;" alt="wKioL1Lgi4HxUyfDAALxxhhOinA434.jpg" src="http://www.bkjia.com/uploads/allimg/140207/215632J06-2.jpg" />

本文出自 “老徐的私房菜” 部落格,謝絕轉載!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.