指令碼準系統:
自動壓縮備份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" />
本文出自 “老徐的私房菜” 部落格,謝絕轉載!