备份mysql数据库

#! /bin/bash

BPATH=/home/backup/mysql_bak

USER="root"

PASSWD="123456"

HOST="192.168.1.5"

DATE=$(date +%Y-%d-%m)

if [ ! -d "$BPATH" ];then

    mkdir -p $BPATH

fi

DB=$(mysql -u $USER -h $HOST -p $PASSWD -Bse 'show databases')

for database in $DB

do

    if [ ! $database == "information_schema" ;

        mysqldump -u $USER -h $HOST -P $PASSWD $database |bzip2> "$BPATH/${DATE}_mysql.gz" 

    fi

done

导入数据库

#! /bin/bash

BPATH=/home/backup/mysql_bak

USER="root"

PASSWD="123456"

HOST="192.168.1.5"

cd $BPATH

tar zxf all_mysql.tar.gz

mysqldump -u $USER -h $HOST -P $PASSWD < mysql.sql