Mysql 一般备份脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 一般备份脚本相关的知识,希望对你有一定的参考价值。
mysql 一般备份脚本
cat mysql_backup.sql
#!/bin/sh . /etc/profile cd /home/mysql/backup exe=/usr/local/mysql/bin/mysqldump mysql_bin=/usr/local/mysql/bin/mysql user=root password=xxxxx suffix=`date +‘%Y%m%d%H%M‘` bakdir=/home/mysql/backup remotedir=/home/mysql/backup remotehost=‘xxx.xxx.xx.xx‘ copy=5 bakdata(){ $exe -u$user -p$password --single-transaction -R --default-character-set=utf8 --triggers --events --hex-blob --databases xxx1 >$bakdir/xxx1_$suffix.sql $exe -u$user -p$password --single-transaction -R --default-character-set=utf8 --triggers --events --hex-blob --databases xxx2 >$bakdir/xxx2_$suffix.sql $exe -u$user -p$password --single-transaction -R --default-character-set=utf8 --triggers --events --hex-blob --flush-logs --master-data=2 -A >$bakdir/all_$suffix.sql } remotebak(){ scp $bakdir/xxx2_$suffix.sql $remotehost:$remotedir scp $bakdir/xxx2_$suffix.sql $remotehost:$remotedir scp $bakdir/all_$suffix.sql $remotehost:$remotedir scp $bakdir/privileges_backup_$suffix.sql $remotehost:$remotedir } clearbak(){ ssh $remotehost "find $remotedir/*.sql -mtime +$copy -exec rm {} \;" find $bakdir/*.sql -mtime +$copy -exec rm {} \; } optimize_tab(){ mysql -u$user -p$password xxx1 -e "show tables " | sed ‘1d‘ | awk ‘{print "optimize table "$0";\nanalyze table "$0";"}‘|mysql -u$user -p$password xxx1; mysql -u$user -p$password xxx2 -e "show tables " | sed ‘1d‘ | awk ‘{print "optimize table "$0";\nanalyze table "$0";"}‘|mysql -u$user -p$password xxx2; } do_backup_grant_priv() { split=\` special=`$mysql_bin -u$user -p$password --default-character-set=utf8 -Bse "select user, host from mysql.user;" | grep "$split" |wc -l ` if [ $special -eq 0 ];then split=\` else special=`$mysql_bin -u$user -p$password --default-character-set=utf8 -Bse "select user, host from mysql.user;" | grep -E "‘" |wc -l ` if [ $special -eq 0 ];then split=\‘\‘ else echo "Error:the username have special char" exit fi fi for SQLUSER in `$mysql_bin -u$user -p$password --default-character-set=utf8 -Bse "select concat(‘$split‘, user, ‘$split‘, ‘@‘, ‘‘‘‘, host, ‘‘‘‘) from mysql.user;"` do $mysql_bin -u$user -p$password --default-character-set=utf8 -Bse "show grants for $SQLUSER;" | sed ‘s/$/;/g‘ >> $bakdir/privileges_backup_$suffix.sql done } echo "----------------------------------------" date +‘===DATA BACKUP BEGIN:%Y%m%d %T=====‘ bakdata date +‘===DATA BACKUP END:%Y%m%d %T=====‘ date +‘===PRI BACKUP BEGIN:%Y%m%d %T=====‘ do_backup_grant_priv date +‘===PRI BACKUP END:%Y%m%d %T=====‘ date +‘===SCP BEGIN:%Y%m%d %T=====‘ remotebak date +‘===SCP END:%Y%m%d %T=====‘ date +‘===CLS BEGIN:%Y%m%d %T=====‘ clearbak date +‘===CLS END:%Y%m%d %T=====‘ date +‘===OPT BEGIN:%Y%m%d %T=====‘ flag=`date +"%w"` if [ $flag -eq 6 ] ; then optimize_tab fi date +‘===OPT END:%Y%m%d %T=====‘ echo -e "\n" echo -e "\n"
本文出自 “小鱼的博客” 博客,谢绝转载!
以上是关于Mysql 一般备份脚本的主要内容,如果未能解决你的问题,请参考以下文章