mysql--用脚本实现分库分表备份

#!/bin/bash

USER=root

PASSWD=123456

SOCK=/data/3306/mysql.sock

LOGIN="mysql -u$USER -p$PASSWD -S $SOCK"

DATABASE=`$LOGIN -e "show databases;" | sed '1d'|egrep -v ".*schema|mysql"`

DUMP="mysqldump -u$USER -p$PASSWD -S $SOCK"

#cd /backup/mysql/

for database in $DATABASE;do

        [ ! -d $database ] && mkdir -p /backup/mysql/$database

        $DUMP -B $database | gzip > /backup/mysql/$database/${database}_$(date +%F).sql.gz

        TABLE=`$LOGIN -e "show tables from $database;"|sed '1d'`

        for table in $TABLE;do

                $DUMP $database $table | gzip > /backup/mysql/$database/${database}_${table}_$(date +%F).sql.gz

        done

done


再结合定时任务,即可实现全自动化备份

你可能感兴趣的:(msyql)