编写脚本,使用mysqldump实现分库分表备份。

分库备份:

#!/bin/bash
#分库备份
bak_user=root
bak_password=513721ykp
bak_path=/backup/db_bak
bak_cmd="-u$bak_user -p$bak_password"
exc_db="Database|information_schema|mysql|performance_schema|sys"
dbname=`mysql $bak_cmd -e "show databases"| egrep -v $exc_db` 
for db in `echo $dbname`
do
   [ -d $bak_path ] || mkdir -p $bak_path 
   mysqldump $bak_cmd -B $db | gzip > $bak_path/${db}_`date +$Y%m%d`.sql.gz
done

实现分表备份:

#!/bin/bash
bak_user=root
bak_password=513721ykp
bak_path=/backup
bak_cmd="-u$bak_user -p$bak_password"
exc_db="Database|information_schema|mysql|performance_schema|sys"
db_name=`mysql $bak_cmd -e "show databases"| egrep -v $exc_db`
#echo $db_name
for db in `echo $db_name`
do        
  [ -d ${bak_path}/dtbak_$db ] || mkdir -p ${bak_path}/dtbak_$db
  dt_name=`mysql $bak_cmd -e "use $db;show tables" | egrep -v "Tables_in_$db"`
  #echo $db
  #echo $dt_name
  for dt in `echo $dt_name`
  do
  #echo $db $dt
  mysqldump $bak_cmd $db ${dt} | gzip > $bak_path/dtbak_${db}/${db}_${dt}.`date +%F%m%d`.sql.gz
  done 
done

你可能感兴趣的:(adb,android)