MySQL mysqldump备份脚本

#!/bin/bash
USER=root
PASSWD=123456
SOCKET=/tmp/mysql.sock
BKDIR=/wo/mysqlbk/
#LS=`ls -d $BKDIR`
DATE=`date "+%Y-%m-%d %H:%M:%S"`
D=`date +%Y%m%d`
USER_N=`whoami`
HOSTNAME=`hostname`
DBLOGIN="mysql -u$USER -p$PASSWD -S$SOCKET"
DBDUMP="mysqlump -u$USER -p$PASSWD -S$SOCKET"
DATABASE="$($DBLOGIN -e"show databases;"|grep -v 'Database'|grep -v 'information_schema'|grep -v  'performance_schema')"
if [ ! -d "$BKDIR" ]; then
/bin/mkdir "$BKDIR" -p
  echo -e "\033[40;33m "$USER_N $DATE Backup create directory successfully!"\033[0m" >>"$BKDIR"/bak_${D}.log
else
LS=`ls -d $BKDIR`
  echo -e "\033[40;33m $USER_N $DATE $HOSTNAME "$LS" directory already exists! \033[0m" >>"$BKDIR"/bak_${D}.log

fi

cd $BKDIR
echo -e "\033[40;33m "$USER_N $DATE $HOSTNAME  $LS MYSQL BACKUP START!"\033[0m" >>"$BKDIR"/bak_${D}.log


for i in $DATABASE
 do
   mysqldump --single-transaction --master-data $DBLOGIN  $i  --log-error="$BKDIR"/bak_${D}.log >"$BKDIR"/bak_${i}_${D}.sql
   echo -e "\033[40;32m "$USER_N $DATE $HOSTNAME $LS $i BACKUP SUCESSED!"\033[0m" >>"$BKDIR"/bak_${D}.log
done

cd $BKDIR
tar_name=`ls *.sql`
for tar in $tar_name
  do
  tar -czvf  ${tar}.tar.gz  ${tar}
  echo -e "\033[40;32m $USER_N $DATE $HOSTNAME $LS "$tar" BACKUP SUCESSED!\033[0m"
done

sleep 30
rm -rf $tar_name
find $BKDIR  -depth  -type f  -name '*.sql.tar.gz'  -mtime +9 -exec rm -rf {} \;
find $BKDIR  -depth  -type f  -name '*log'  -mtime +9 -exec rm -rf {} \;
echo -e "\033[40;32m "$USER_N $DATE $HOSTNAME $LS TAR  SUCESSED!"\033[0m" >>"$BKDIR"/bak_${D}.log



修正版本

#!/bin/bash
USER=root
#PASSWD=123456
SOCKET=/var/lib/mysql/mysql.sock
BKDIR=/backup/data/
#LS=`ls -d $BKDIR`
DATE=`date "+%Y-%m-%d %H:%M:%S"`
D=`date +%Y%m%d`
USER_N=`whoami`
HOSTNAME=`hostname`
DBLOGIN="mysql -u$USER  -S$SOCKET"
DBDUMP="mysqlump -u$USER  -S$SOCKET"
LOGIN=" -S$SOCKET -u$USER"
DATABASE="$($DBLOGIN -e"show databases;"|grep -v 'Database'|grep -v 'information_schema'|grep -v  'performance_schema'|grep -v '#mysql'|grep -v 'mysql'|grep -v 'test')"
if [ ! -d "$BKDIR" ]; then
/bin/mkdir "$BKDIR" -p
  echo -e "\033[40;33m "$USER_N $DATE Backup create directory successfully!"\033[0m" >>"$BKDIR"/bak_${D}.log
else
LS=`ls -d $BKDIR`
  echo -e "\033[40;33m $USER_N $DATE $HOSTNAME "$LS" directory already exists! \033[0m" >>"$BKDIR"/bak_${D}.log

fi

cd $BKDIR
echo -e "\033[40;33m "$USER_N $DATE $HOSTNAME  $LS MYSQL BACKUP START!"\033[0m" >>"$BKDIR"/bak_${D}.log

for i in $DATABASE
 do
   mysqldump --single-transaction $LOGIN  $i >"$BKDIR"/bak_${i}_${D}.sql
   echo -e "\033[40;32m "$USER_N $DATE $HOSTNAME $LS $i BACKUP SUCESSED!"\033[0m" >>"$BKDIR"/bak_${D}.log
done

cd $BKDIR
tar_name=`ls *.sql`
for tar in $tar_name
  do
  tar -czvf  ${tar}.tar.gz  ${tar}
  echo -e "\033[40;32m $USER_N $DATE $HOSTNAME $LS "$tar" BACKUP SUCESSED!\033[0m"
done

sleep 30
rm -rf $tar_name
find $BKDIR  -depth  -type f  -name '*.sql.tar.gz'  -mtime +9 -exec rm -rf {} \;
find $BKDIR  -depth  -type f  -name '*log'  -mtime +9 -exec rm -rf {} \;
echo -e "\033[40;32m "$USER_N $DATE $HOSTNAME $LS TAR  SUCESSED!"\033[0m" >>"$BKDIR"/bak_${D}.log

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

另外一个版本备脚本

MySQL mysqldump备份脚本_第1张图片

#!/bin/bash
#set LANG
export LANG=zh_CN.UTF-8
MAXIMUM_BACKUP_FILES=10              #最大备份文件数
BACKUP_FOLDERNAME="mysql_backup"  #数据库备份文件的主目录
DB_HOSTNAME="localhost"              #mysql所在主机的主机名
DB_USERNAME="root"                   #mysql登录用户名
DB_PASSWORD="123456"                 #mysql登录密码
DATABASES=(
            "test"
            "mysql"                    #备份的数据库名
)
#=========
echo "Bash Database Backup Tool"
#CURRENT_DATE=$(date +%F)
CURRENT_DATE=$(date +%F)              #定义当前日期为变量
BACKUP_FOLDER="${BACKUP_FOLDERNAME}_${CURRENT_DATE}" #存放数据库备份文件的目录
mkdir $BACKUP_FOLDER #创建数据库备份文件目录
#统计需要被备份的数据库
count=0
while [ "x${DATABASES[count]}" != "x" ];do
    count=$(( count + 1 ))
done
echo "[+] ${count} databases will be backuped..."
#循环这个数据库名称列表然后逐个备份这些数据库
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} --events ${DATABASE} > "${BACKUP_FOLDER}/${DATABASE}.sql");then
        b='' 
       for ((i=0;$i<=100;i+=2)) 
       do 
       printf "progress: [%-50s] %d%%\r" $b $i 
       sleep 0.1 
       b+='#' 
       done 
       echo
         echo "  Dumped successfully!"
    else
        echo "  Failed dumping this database!"
    fi
        echo -n "   Finished: ";echo $(date)
done
echo
echo "[+] Packaging and compressing the backup folder..."
tar -cv ${BACKUP_FOLDER} | bzip2 > ${BACKUP_FOLDER}.tar.bz2 && rm -rf $BACKUP_FOLDER
BACKUP_FILES_MADE=$(ls -l ${BACKUP_FOLDERNAME}*.tar.bz2 | wc -l)
BACKUP_FILES_MADE=$(( $BACKUP_FILES_MADE - 0 ))
#把已经完成的备份文件数的结果转换成整数数字
 
echo
echo "[+] 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
                                                                                                                    
fi
for 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}
#以下显示备份的数据文件删除进度,一般脚本都是放在crontab里,所以我这里只是为了显示效果,可以不选择这个效果。
     
CHAR=''
for ((i=0;$i<=100;i+=2))
do  printf "Removing:[%-50s]%d%%\r" $CHAR $i
        sleep 0.1
CHAR=#$CHAR
done
    echo
fi


你可能感兴趣的:(Mysql)