#!/bin/bash
IP=192.168.0.198
mysqlpath=/usr/local/mysql/bin
variables=`$mysqlpath/mysql -u root -pmima -h$IP -e" show variables like 'character\_set\_%';" |awk '{print$2}' |sed -n '4'p`
$mysqlpath/mysqldump -uroot -pmima -h$IP --default-character-set=$variables keyword_ad>/back/mysqldb/s198db/keyword_ad_$(date +%Y%m%d).sql;
$mysqlpath/mysqldump -uroot -pmima -h$IP --default-character-set=$variables mysql>/back/mysqldb/s198db/s198mysql_$(date +%Y%m%d).sql;
sleep 180s
cd /back/mysqldb/s198db/
/bin/tar zcf keyword_ad_$(date +%Y%m%d).tar.gz keyword_ad_$(date +%Y%m%d).sql
/bin/tar zcf s198mysql_$(date +%Y%m%d).tar.gz s198mysql_$(date +%Y%m%d).sql
sleep 60s
/usr/bin/find /back/mysqldb/s198db/ -name "*.tar.gz" -mtime +5 -exec rm -rf {} \;
sleep 60s
/usr/bin/find /back/mysqldb/s198db/ -name "*.sql" -mtime +1 -exec rm -rf {} \;
惆怅上面这种方法只解决了mysql服务端与客服端编码不一样的时候出现问题
,今天我去查备份依然看到 部分database 与mysql 的编码不一样那只能 连接的时候 看库的编码 依据库的编码方式连接了于是我又 该了脚本
#!/bin/bash
IP=IP地址
U=root
P=mima
DB=" 需要备份的库以空格隔开就可以了"
mysqlpath=/usr/local/mysql/bin
BACKDIR=/back/mysqldb/D213db/
date=`date +%Y%m%d`
for data in $DB
do
variables=`$mysqlpath/mysql -u $U -p$P -h$IP -e "show create database $data;" |awk '{print$9}' |sed -n '2'p`
$mysqlpath/mysqldump -u $U -p$P -h$IP --default-character-set=$variables $data > $BACKDIR$data"_"$date.sql && echo "$variables "
echo "$data"
cd $BACKDIR
/bin/tar zcf $data"_"$date.tar.gz $data"_"$date.sql&&echo "$data tar success "
done
sleep 60s
/usr/bin/find $BACKDIR -name "*.tar.gz" -mtime +5 -exec rm -rf {} \;
sleep 60s
/usr/bin/find $BACKDIR -name "*.sql" -mtime +0 -exec rm -rf {} \;
如果表的编码与库的还不一样那样就麻烦了。我们公司的还有表的字段不一样,我就愁死了 这跟程序员有关系 没办法 。
转载于:https://blog.51cto.com/hzcsky/640926