最新消息: 新版网站上线了!!!

linux备份数据库脚本

利用vi mysql.sh

#!/bin/bash
#BACK_PATH 备份路径
#MYSQL_BIN mysql安装目录bin
#DB_NAMES数据库名称
MYSQL_BIN=/usr/local/mysql/bin
TMP_PATH=/tmp
BACK_PATH=/home/mysql_bak/mysql_bak
SAVE_OLD_FILE_TIME=30
DDATE=`date "+%Y-%m-%d-%H"`
LOG_FILE=/root/log/mysql.log
######## mysql config ##############
DB_USER=root
DB_ADDR="127.0.0.1"
DB_PASSWORD='password'
DB_NAMES=(web)
####################################
if [ $(id -u) != "0" ]
then
    echo "Error:This script must be root to run."
    exit 1
fi
###detele old database back file###
function fcleantar(){
    if [ -d $BACK_PATH ]
    then
        cd $BACK_PATH
        find $BACK_PATH -mtime +$SAVE_OLD_FILE_TIME -type f -name "*.tar.gz"  -exec rm -f {} \;
    else
        mkdir $BACK_PATH
    fi
}
### detele file ###
function fdelete(){
    if [ -f $1 ]
    then
        rm -f $1
    fi
}
###  compress file ###
function fcompress(){
    tar -C $TMP_PATH -zcvf $BACK_PATH/$1.tar.gz $1 &&  rm -f $1
}
### log ###
function flog(){
    cat $1  >> $LOG_FILE
}
### start back proesss ###
fcleantar
cd $TMP_PATH
for DB_NAME in ${DB_NAMES[@]}
do
    FILENAME=${DB_NAME}_${DDATE}.sql
    fdelete $FILENAME
    $MYSQL_BIN/mysqldump -u$DB_USER -p$DB_PASSWORD -h$DB_ADDR --opt $DB_NAME > $TMP_PATH/$FILENAME
    fdelete $BACK_PATH/${FILENAME}.tar.gz
    fcompress $FILENAME
done


输入命令 root@loal# chmod +x mysql.sh 执行方法

./mysql.sh 或  sh mysql.sh

转载请注明:谷谷点程序 » linux备份数据库脚本