Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

linux shell 备份mysql 数据库

$
0
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

作者:wyfhist 发表于2013-11-27 1:39:02 原文链接
阅读:47 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>