/opt/shells/bak_test_table.sh

#!/bin/bash
datastr=/nas/bakup/mysql_table
DBHost="mysql.host"
DB="test"
user="root"
password="123456"
port="3306"
backupstr=mysqlbak(date +%Y%m%d%H%M)
#不备份表名,按最后一个下划线截取,匹配所有分表,如log_0 log_1
db_table_not_bak="log imei"

cddatastr
mkdir backupstr
cdbackupstr


#遍历表名称
#mysqldump  -h DBHost  -uuser  -ppassword   -Pport  --default-character-set=utf8 --single-transaction --master-data=2 -R -E -dt db   >db.sql
totalTABLE=(mysql -N -hDBHost -uuser -ppassword -Pport <<EOF
useDB;
show tables;
EOF
)

###IFS
OLD1_IFS="IFS"
IFS=" "
arr1=(totalTABLE)
IFS="OLD1_IFS"
mkdir -pbackupstr"/"DB
for table in{arr1[@]}
do 

    #table名称去掉最后一个下划线以后在db_table_not_bak中
    if [[db_table_not_bak =~ {table%_*} ]];then
        echo "不备份"DB"."table
    else
        echoDB"."table" dumping..."
        mysqldump  -hDBHost  -uuser  -ppassword   -Pport  --default-character-set=utf8 --single-transactionDB table  >backupstr"/"DB"/"DB.table.sql
    fi
done

# 压缩为tgz
cddatastr
tar zcvf backupstr".tgz"backupstr
if [ "0" -eq ? ];then
    rm -rfbackupstr
fi


crontab

#echo "00 */3 * * * /opt/shells/bak_test_table.sh" >> /var/spool/cron/crontabs/root
chmod +x /opt/shells/bak_test_table.sh
echo "30 */3 * * * /opt/shells/bak_test_table.sh" >> /var/spool/cron/root

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code