龙岩易富通网络科技有限公司

龙岩小程序开发,龙岩分销系统

shell自动备份mysql数据库

2016.01.14 | 1744阅读 | 0条评论 | shell脚本


#!/bin/bash


#1.数据库信息定义

echo  "Enter mysql password:"

read  PWD

mysql_host="localhost"

mysql_user="root"

mysql_passwd=$PWD


#sql备份目录

back_dir="/home/bakdb"



if [ ! -d $back_dir ]; then

mkdir -p $back_dir

fi


#备份的数据库数组

db_arr=$(echo "show databases;" | mysql -u$mysql_user -p$mysql_passwd -h$mysql_host)



#当前日期

nowdate=`date +%Y-%m-%d`;



#2.进入到备份目录

cd $back_dir

mkdir $nowdate

cd $nowdate



#3.循环备份

for dbname in ${db_arr}

do

   if [ "$dbname" = "information_schema" ] || [ "$dbname" = "Database" ] || [ "$dbname" = "test" ] || [ "$dbname" = "performance_schema" ] || [ "$dbname" = "mysql" ]; then

       echo "no"  

    else

       sqlfile=$dbname".sql"

       a=`mysqldump -u$mysql_user -p$mysql_passwd -h$mysql_host $dbname >$sqlfile`

       echo $dbname

   fi

done


赞 (

发表评论