linux shell 备份mysql 数据库

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#!/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

标签: linux Mysql

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C#中通过xpath查找xml的指定元素

下一篇:C#实现一个MP3播放类