#!/bin/bash
#auto backup mysql every database;
#date 2014.07.07
#author firxiao
###mysql_config####
HOST="localhost" ##mysql主机IP
USER="xxxx" ##用户名
PASSWD="xxxx" ##密码
BACKUPDIR='/backup' ##备份目录
###################
DATE=`date +%Y%m%d` ##当前日期(不需要改)
M=`date +%Y%m` ##当前月(不需要改)
function createdir() ##判断备份目录是否存在 不存在创建
{
while [ ! -d $BACKUPDIR ]; do
echo "$BACKUPDIR is not exits, will create now"
mkdir -p $BACKUPDIR
done
cd $BACKUPDIR
while [ ! -d "$DATE" ];do
mkdir $DATE
done
while [ ! -d "$M" ];do
mkdir $M
done
}
function showdatabase() ##查询mysql中有哪些库
{
mysql -h $HOST -u$USER -p$PASSWD -e 'show databases;' |grep -v 'Database'|grep -v 'information_schema' > /tmp/mysqldblist
}
function backup () ##备份并将备份移到当前月份文件夹
{
while read LINE
do
mysqldump -h $HOST -u$USER -p$PASSWD $LINE > $DATE/$LINE$DATE.sql
done < /tmp/mysqldblist
mysqldump -h $HOST -u$USER -p$PASSWD --all-database > $DATE/all$DATE.sql
tar cvfj $DATE.tar.gz $DATE
rm -rf $DATE
mv $DATE.tar.gz $M/
}
createdir && showdatabase && backup ##开始备份
mysql 备份脚本
点赞
收藏