建立一script,設權限為755

#!/bin/bash

#要備份的DB清單

BACKUP_LIST="/home/back_list"

#要備份到什麼地方

BACKUP_PATH="/home/Backup_DB"

BACKUP_TIME=`date +%Y%m%d_%H`
BACKUP_FOLDER=$BACKUP_PATH/$BACKUP_TIME

if [! -d $BACKUP_FOLDER ]; then
    /bin/mkdir -p $BACKUP_FOLDER
fi

#== LOG ==
echo $BACKUP_TIME >> $BACKUP_PATH/log

TIME_START=`date +%s` #現在時間換算成秒數

cat $BACKUP_LIST | while read DB
do

                                        #-p 後面接密碼為123456 設定為utf8格式
    /usr/bin/mysqldump -u root -p123456 --default-character-set=UTF8 $DB > $BACKUP_FOLDER/$DB.sql
    if [ $? -ne "0" ]; then
        #== LOG ==
        echo "  [ERROR] "$DB  >> $BACKUP_PATH/errorlog
    fi
done

TIME_END=`date +%s`
TIME_TOTAL=$(($TIME_END-$TIME_START))

#== LOG ==
echo "  [TIME] "$TIME_TOTAL" /sec" >> $BACKUP_PATH/log

cd $BACKUP_PATH
tar zcvf $BACKUP_TIME.tar.gz $BACKUP_TIME &> /dev/null (如果有錯誤訊息送到虛無設備/dev/null 直接丟棄不會顯示在營幕上)
if [ $? -eq "0" ]; then
    if [ -d $BACKUP_TIME ]; then
        rm -rf $BACKUP_TIME
    fi
fi

======================================

接著就設定排程讓它跑吧

neo2124 發表在 痞客邦 PIXNET 留言(0) 人氣()