mysql 备份脚本

日常常用mysql建库备份shell

1.不登陆数据库,批量创建数据库

#!/bin/bash
PATH="/usr/local/mysql/bin:$PATH"
MYUSER=root
MYPASS=123456
SOCKET=/tmp/mysql.sock
MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET"
for dbname in shunmai langwenke guirentong
  do 
    $MYCMD -e "create dabases dbname"
  done

2.不登录数据库备份

#!/bin/bash
PATH="/usr/local/mysql/bin:$PATH"
DAPATH=/data/backup
MYUSER=root
MYPASS=123456
SOCKET=/tmp/mysql.sock
MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYUSER -pMYPASS -S $SOCKET"
[ ! -d "$DAPATH" ] && mkdir $DAPATH
for dbname in `$MYCMD -e "show databases;"|sed '1,2d'`|egrep -v "mysql|test"
  do
    $MYDUMP $dbname |gzip >$DAPATH/${dbname}_$(date +%F).sql.gz
  done

3.不登录数据库批量建表,添加数据

#!/bin/bash
PATH="/usr/local/mysql/bin:$PATH"
DBPATH=/data/backup
MYUSER=root
MYPASS=123456
SOCKET=/tmp/mysql.sock
MYCMD="mysql -u$MYUSER -pMYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYUSER -pMYPASS -S $SOCKET"
for dbname in shunmai langwenke haibei guirentong
  do
    $MYCMD -e "use $dbname;create  table test (id int,name varchar(16)); insert into test values(1,'testdata');"
  done 

4.不登陆数据库查表

#!/bin/bash
PATH="/usr/local/mysql/bin:$PATH"
MYUSER=root
MYPASS=123456
SOCKET=/tmp/mysql.sock
MYCMD="mysql -u$MYUSER -pMYPASS -S $SOCKET"
for dbname in shunmai langwenke haibei guirentong
  do
    echo =================${dbname}.test================
      $MYCMD -e "use $dbname;select * from ${dbname}.test;"
  done

5.不登陆数据库,批量备份

#!/bin/bash
PATH="/usr/local/mysql/bin:$PATH"
DBPATH=/data/backup
MYUSER=root
MYPASS=123456
SOCKET=/tmp/mysql.sock
MYCMD="mysql -u$MYUSER -pMYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYUSER -pMYPASS -S $SOCKET"
  [ ! -d "$DAPATH" ] && mkdir $DBPATH
  for dbname in `$MYCMD -e "show databases;"|sed '1,2d'|egrep -v "mysql|schema"`
    do
      mkdir $DBPATH/${dbname}_$(date +%F) -p
      for table in `$MYCMD -e "show tables from $dbname;"|sed '1d'`
        do
          $MYCMD $dbname $table|gzip >$DBPATH/${dbname}_$(date +%F)/${dbname}_$(table).sql.gz
        done
    done

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值