#!/bin/bash
#Author xieping1991@163.com
cd /sqlbak
/usr/local/mysql/bin/mysqladmin -uroot -p123456 flush-logs
MAXIMUM_BACKUP_FILES=10
BACKUP_FOLDERNAME="database_backup"
DB_HOSTNAME="localhost"
DB_USERNAME="root"
DB_PASSWORD="123456"
DATABASES=(
"数据库名称"
"数据库名称"
"数据库名称"
)
#=========
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=0
while [ "x${DATABASES[count]}" != "x" ];do
count=$(( count + 1 ))
done
echo "[+] ${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)
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 )) ###Convert into integer number.
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}
CHAR=‘‘
for ((i=0;$i<=100;i+=2))
do printf "Removing:[%-50s]%d%%\r" $CHAR $i
sleep 0.1
CHAR=#$CHAR
done
echo
fi
本文出自 “xieping_linux” 博客,请务必保留此出处http://xieping.blog.51cto.com/3715452/1437201
mysql完整备份,超过十天的自动删除,布布扣,bubuko.com
原文:http://xieping.blog.51cto.com/3715452/1437201