Home > Database > Mysql Tutorial > 改良版本mysqldump来备份MYSQL数据库_MySQL

改良版本mysqldump来备份MYSQL数据库_MySQL

WBOY
Release: 2016-06-01 14:01:09
Original
918 people have browsed it

mysqldump

我的备份脚本都是在凌晨执行的,经常在慢查询日志里面看到这样的信息:select * from table1;
之前一直很纳闷,最后才了解到原来是MYSQLDUMP搞的鬼。
因为MYSQLDUMP会为整个库来加一个全局锁定。
如果单纯用MYSQLDUMP进行全库备份会造成以下三个方面的影响。
1、服务器CPU严重阻塞。
2、磁盘I/O直线增加。
3、所有的查询都成了慢查询。
我现在的网站数据库大概是5个G左右,而且每天都有增大。
表结构是MYISAM,INNODB,MEMORY三者混合。
所以单纯用HOTCOPY工具恐怕有点困难。所以我今天简单变了一下我上次写的关于用OUTFILE来备份MYSQL的脚本。
可以解决上面说的三个缺点。
 
1、备份脚本内容

[david_yeung@localhost ~]$ cat fast_backup
#!/bin/sh
#
# Created by david yeung.
#
# 20080707.
#
# Backup mysql's full data.
#
DBNAME=$1
BACKUPDIR=/home/david_yeung/backup_new
USERNAME=backup_user
PASSWD=123456
TARNAME="$BACKUPDIR"/backup"$1"`date '+%Y%m%d'`
# Add your own database name here.
case "$1" in
  my_site);;
  *) exit;;
esac
# Get all the tables' name.
NUM=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e "show tables" -D $DBNAME|wc -l`
HEADNUM=`expr ${NUM} - 3`
TAILNUM=`expr ${NUM} - 7`
ARR1=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e "show tables" -D $DBNAME| head -n"$HEADNUM" | tail -n "$TAILNUM"`
ARR2=($ARR1)
i=0
while [ "$i" -lt "${#ARR2[@]}" ]
do
 tmpFileName=${ARR2[$i]}
 # The real dump process.
 /usr/local/mysql/bin/mysqldump -u$USERNAME -p"$PASSWD" "$DBNAME" "$tmpFileName" >> "$TARNAME"
 let "i++"
done

2、因为我们一直用存储过程,所以得单独备份出来。

[david_yeung@localhost ~]$ cat fast_sp
#!/bin/sh
# Created by david yeung 20080122.
#
# Backup site's routine.
TARNAME=/home/david_yeung/backup_new/spBackup"$1"`date '+%Y%m%d'`
/usr/local/mysql/bin/mysqldump -ubackup_user -p123456 -n -t -d -R my_site > "$TARNAME"
 
3、丢到计划任务里面去,就不管了。

[root@localhost backup_new]# crontab -l
0 01 * * * /home/david_yeung/fast_backup my_site
0 0 * * 5 /home/david_yeung/fast_sp
 
每天凌晨1点备份数据,每个周五凌晨备份存储过程。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template