#!/bin/bash
#
DATE=`date "+%F"`
IP="localhost"
DB_NAME="db1 db2 db3"
DB_USER="root"
DB_PASS="centosmysql"
BIN_DIR="/usr/bin"
BAK_DIR="/root/backup-mysql-db"
BAK_PATH=$BAK_DIR/$DATE
if [ ! -d $BAK_PATH ] ; then
mkdir -p $BAK_PATH
fi
for db_name in $DB_NAME
do
variables="`$BIN_DIR/mysql -u$DB_USER -p$DB_PASS -h$IP -e "show create database $db_name;" |awk '{print $9}' |sed -n '2'p`"
if $BIN_DIR/mysqldump --opt --master-data --default-character-set=$variables -h$IP -u$DB_USER -p$DB_PASS --databases $db_name > $BAK_PATH/$db_name-$DATE.sql ; then
echo -e "\033[32m$db_name backup success...\033[0m" >>/root/backup-sucess.txt
else
echo -e "\033[31;5m$db_name backup false...\033[0m" >>/root/backup-fals.txt
fi
done
#删除15天以前备份的文件
find $BAK_DIR -name "*" -mtime +15 |xargs rm -rf
exit 0
阅读(591) | 评论(0) | 转发(0) |