首页 > 代码库 > MySQL备份

MySQL备份

1. MySQL排除表备份

#!/bin/bash
#created by 90root
#date: 20160809
 
date_y=$(date +%Y)
date_m=$(date +%m)
time=`date ‘+%F-%R‘`
mysql_user="mysql_user"
mysql_passwd="mysql_passwd"
mysql_host="127.0.0.1"
tar_path="/data/backup/backup_mysql_tables"
mysql_dump="mysqldump -h$mysql_host -u$mysql_user -p$mysql_passwd"
mysql_cmd="mysql -h$mysql_host -u$mysql_user -p$mysql_passwd"
 
#备份数据库列表
backup_db_arr=("test1" "test2" "test3" "test4")
 
#测数据库存放路径
if [ ! -d $tar_path/$date_y/$date_m ];then
    mkdir -p $tar_path/$date_y/$date_m
fi
 
 
#排除备份数据库列表
exclude_db_tables="tables_name1|tables_name2|tables_name3|tables_name4"
 
#循环数组
for j in "${backup_db_arr[@]}";do
    if [ "$j" == "test2" ];then
        for i in `$mysql_cmd -e "use $j;show tables"|sed ‘1d‘|grep -Ev "$exclude_db_tables"`;do
            $mysql_dump $j $i|gzip >> $tar_path/$date_y/$date_m/$j-$time.sql.gz
        done
    else
        $mysql_dump $j |gzip >> $tar_path/$date_y/$date_m/$j-$time.sql.gz
    fi
done

2. MySQL多库备份

#!/bin/bash
#about: 90root
 
date=$(date +%Y%m%d-%H:%M)
date_y=$(date +%Y)
date_m=$(date +%m)
Mysql_Backup_Path=/data/backup/backup_mysql
Mysql_host="127.0.0.1"
Mysql_user="mysql_user"
Mysql_pass="mysql_passwd"
Mysql_cmd="mysql -h$Mysql_host -u$Mysql_user -p$Mysql_pass"
Mysql_dump="mysqldump -h$Mysql_host -u$Mysql_user -p$Mysql_pass"
Mysql_database="test1 test2 test3"
 
#检测数据库存放路径
if [ ! -d $Mysql_Backup_Path/$date_d ];then
    mkdir -p $Mysql_Backup_Path/$date_y/$date_m
fi
 
#清空mysql备份日志文件
echo > $Mysql_Backup_Path/mysqldump.log
 
#判断日期目录
if [ ! -d $Mysql_Backup_Path/$date_y/$date_m ];then
    mkdir $Mysql_Backup_Path/$date_y/$date_m
fi
 
#多库备份
for dbname in $Mysql_database;do
    $Mysql_dump $dbname | gzip > $Mysql_Backup_Path/$date_y/$date_m/${dbname}_${date}.sql.gz
    if [ $? -eq 0 ];then
        echo "****** mysqldump ${dbname} `date +%Y%m%d-%H:%M:%S` ******" >> $Mysql_Backup_Path/mysqldump.log
    fi
done


本文出自 “断肠人在天涯” 博客,请务必保留此出处http://8649605.blog.51cto.com/8639605/1855601

MySQL备份