IT虾米网

linux shell 备份mysql 数据库

qq123 2018年06月24日 数据库 1068 0
#!/bin/bash 
host="目标ip" 
id="用户名" 
pwd="密码" 
dbs=$(echo "show databases;" | mysql -u$id -p$pwd -h$host) 
nodeldb="Database information_schema performance_schema test mysql" 
backuppath="/root/mysqlbackup" 
day=15 
localIp=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d addr:地址:) 
 
[ ! -d $backuppath ] &&mkdir -p $backuppath 
cd $backuppath 
 
backupname=mysql_$(date +%Y-%m-%d) 
for db in $dbs; 
do 
   isbool="0" 
   for nodb in $nodeldb; 
   do 
    if [ "$db" == "$nodb" ] 
    then 
        isbool="1"     
    fi 
    done 
   if [ "$isbool" == "1" ] 
   then 
      continue 
   fi 
 
   mysqldump -u$id -p$pwd -h$host $db >$backupname_$db.sql 
   echo "$db" 
   if [ "$?" == "0" ] 
   then 
       echo $(date +%Y-%m-%d)" $localIp $host $db  mysqldump sucess">>mysql.log 
   else 
      echo $(date +%Y-%m-%d)"  $localIp $host $db mysql dump failed">>mysql.log 
      echo $(date +%Y-%m-%d)" client $localIp backup $host $db is Failure" 
      exit 0 
   fi 
done 
tar -czf $backupname.tar.gz *.sql 
#上一句执行的返回值linux中0表示成功 
if [ "$?" == "0" ] 
then 
   echo $(date +%Y-%m-%d)" tar sucess">>mysql.log 
  
else 
   echo $(date +%Y-%m-%d)" tar failed">>mysql.log 
   echo $(date +%Y-%m-%d)" client $localIp backup and Compression service $host is Failure" 
   exit 0 
fi 
 
rm -f *.sql 
 
delname=mysql_$(date -d "$day day ago" +%Y-%m-%d).tar.gz 
rm -f $delname 
echo $(date +%Y-%m-%d)"  $localIp backup  $host is success" |mail 18790542086@163.com

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!