<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[利用SELECT ... OUTFILE来备份MySQL数据库并加上字段间的分隔符]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 27 Oct 2009 02:59:30 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	 <br/><div class="code">select FQQ,FScoreCount from Tbl_User into outfile &quot;/tmp/terminatedtest.txt&quot; fields terminated by &quot;,&quot;;</div><br/>TERMINATED BY &#039;n&#039;...TERMINATED 表示字段分隔<br/><br/>我写这个脚本的目的是利用MySQL的select * from tablename into outfile ...语句来备份MySQL数据库，虽然没有MYSQLDUMP导出数据快，可是恢复的时候却非常快。<br/>注意：<br/>1）、我这个不包含表结构的备份，所以如果用之前先备份一下表结构。<br/>2）、运行此脚本的用户必须具有select,insert,以及GLOBAL的FILE权限。<br/>3）、注意导入时候的字符集要跟你的库一致。<br/>1、备份脚本内容：<br/><br/>[root@localhost mysql]# cat fast_full_backup<br/><br/>#!/bin/sh<br/><br/>#<br/><br/># Created by david yeung.<br/><br/>#<br/><br/># 20080707.<br/><br/>#<br/><br/># Use outfile syntax to backup mysql&#039;s full data.<br/><br/>#<br/><br/>DBNAME=$1<br/>BACKUPDIR=/home/mysql/backup<br/>USERNAME=backup_file_user<br/>PASSWD=123456<br/>TARNAME=$1`date &#039;+%Y%m%d&#039;`.tar<br/># Add your own database name here.<br/>case &quot;$1&quot; in<br/>&nbsp;&nbsp;t_girl);;<br/>&nbsp;&nbsp;*) exit;;<br/>esac<br/># Get all the tables&#039; name.<br/>NUM=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e &quot;show tables&quot; -D $DBNAME&#124;wc -l`<br/>HEADNUM=`expr $&#123;NUM&#125; - 3`<br/>TAILNUM=`expr $&#123;NUM&#125; - 7`<br/>ARR1=`/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -s -vv -e &quot;show tables&quot; -D $DBNAME&#124; head -n&quot;$HEADNUM&quot; &#124; tail -n &quot;$TAILNUM&quot;`<br/>ARR2=($ARR1)<br/><br/>i=0<br/>while [ &quot;$i&quot; -lt &quot;$&#123;#ARR2[@]&#125;&quot; ]<br/>do<br/> tmpFileName=$&#123;ARR2[$i]&#125;<br/> # The real dump process.<br/> /usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -D$DBNAME -vv -e &quot;select * from $tmpFileName into outfile &#039;&quot;$BACKUPDIR/$tmpFileName&quot;.dat&#039; fields terminated by &#039;,&#039; enclosed by &#039;&#92;&quot;&#039; lines terminated by &#039;&#92;n&#039;&quot;<br/> let &quot;i++&quot;<br/>done<br/><br/># Compress all the files.<br/><br/>#<br/><br/>cd $BACKUPDIR<br/>tar cvf $TARNAME `ls *.dat`<br/>gzip -f $TARNAME<br/>rm -rf *.dat<br/><br/>2、恢复脚本内容：<br/><br/>[root@localhost mysql]# cat fast_full_recovery<br/><br/>#!/bin/sh<br/><br/>#<br/><br/># Created by david yeung.<br/><br/>#<br/><br/># 20080707.<br/><br/>#<br/><br/># Use outfile syntax to restore mysql&#039;s full data.<br/><br/>#<br/><br/>DBNAME=$1<br/>GZNAME=$2<br/>GZDIR=`dirname $GZNAME`<br/>USERNAME=backup_file_user<br/>PASSWD=123456<br/>if [ -z $&#123;DBNAME&#125; ]<br/>then<br/> exit<br/>fi<br/>if [ -z $&#123;GZNAME&#125; ]<br/>then<br/>&nbsp;&nbsp;exit<br/>fi<br/>TARNAME=`gzip -l &quot;$GZNAME&quot; &#124; awk &#039;&#123; print $4 &#125;&#039;&#124;tail -n1`<br/>gzip -d &quot;$GZNAME&quot;<br/>tar xvf &quot;$TARNAME&quot; -C &quot;$GZDIR&quot;<br/>ARR1=(`ls &quot;$GZDIR&quot; &#124; grep &#039;.dat&#039; &#124; grep -v &#039;grep&#039;`)<br/><br/>i=0<br/>while [ &quot;$i&quot; -lt &quot;$&#123;#ARR1[@]&#125;&quot; ]<br/>do<br/> TMPFILENAME=$&#123;ARR1[$i]&#125;<br/> TBNAME=`echo $TMPFILENAME &#124; cut -d &#039;.&#039; -f1`<br/> /usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWD -D$DBNAME -vv -e &quot;load data infile &#039;&quot;$GZDIR&quot;/$TMPFILENAME&#039; ignore into table &quot;$TBNAME&quot; character set utf8 fields terminated by &#039;,&#039; enclosed by &#039;&#92;&quot;&#039; lines terminated by &#039;&#92;n&#039;&quot;<br/> let &quot;i++&quot;<br/>done<br/><br/>rm -rf &quot;$GZDIR&quot;/*.dat<br/><br/>3、实际运行例子：<br/><br/>1）、备份过程：<br/><br/>[root@localhost mysql]# ./fast_full_backup t_girl<br/>--------------<br/>select * from admin into outfile &#039;/home/mysql/backup/admin.dat&#039; fields terminated by &#039;,&#039; enclosed by &#039;&quot;&#039; lines terminated by &#039;&#92;n&#039;<br/>--------------<br/><br/>Query OK, 0 rows affected (0.00 sec)<br/><br/>Bye<br/>...<br/><br/>Bye<br/>--------------<br/>select * from ww into outfile &#039;/home/mysql/backup/ww.dat&#039; fields terminated by &#039;,&#039; enclosed by &#039;&quot;&#039; lines terminated by &#039;&#92;n&#039;<br/>--------------<br/><br/>Query OK, 9 rows affected (0.00 sec)<br/><br/>Bye<br/>admin.dat<br/>...<br/>ww.dat<br/>[root@localhost mysql]#<br/><br/><br/>2）、恢复过程：<br/><br/><br/>[root@localhost mysql]# ./fast_full_recovery t_girl /home/mysql/backup/t_girl20080707.tar.gz<br/>admin.dat<br/>...<br/>ww.dat<br/>--------------<br/>load data infile &#039;/home/mysql/backup/admin.dat&#039; ignore into table admin character set utf8 fields terminated by &#039;,&#039; enclosed by &#039;&quot;&#039; lines terminated by &#039;&#92;n&#039;<br/>--------------<br/><br/>Query OK, 0 rows affected (0.00 sec)<br/>Records: 0&nbsp;&nbsp;Deleted: 0&nbsp;&nbsp;Skipped: 0&nbsp;&nbsp;Warnings: 0<br/><br/>Bye<br/>...<br/><br/>Query OK, 9 rows affected, 3 warnings (0.00 sec)<br/>Records: 9&nbsp;&nbsp;Deleted: 0&nbsp;&nbsp;Skipped: 0&nbsp;&nbsp;Warnings: 0<br/><br/>Bye<br/>[root@localhost mysql]#<br/><br/>4、与MYSQLDUMP导出导入时间比较：<br/><br/>前提：2G的数据量。<br/>1）、用OUTFILE 方式花费。<br/>导出：<br/>real&nbsp;&nbsp;&nbsp;&nbsp;5m19.003s<br/>user&nbsp;&nbsp;&nbsp;&nbsp;2m20.211s<br/>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m11.053s<br/>导入：<br/>real&nbsp;&nbsp;&nbsp;&nbsp;6m28.006s<br/>user&nbsp;&nbsp;&nbsp;&nbsp;0m19.723s<br/>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m13.647s<br/>2）、用MYSQLDUMP 方式花费。<br/>导出：<br/>real&nbsp;&nbsp;&nbsp;&nbsp;4m16.682s<br/>user&nbsp;&nbsp;&nbsp;&nbsp;2m52.976s<br/>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m13.026s<br/>导入：<br/>real&nbsp;&nbsp;&nbsp;&nbsp;7m49.480s<br/>user&nbsp;&nbsp;&nbsp;&nbsp;1m2.702s<br/>sys&nbsp;&nbsp;&nbsp;&nbsp; 0m10.545s
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 利用SELECT ... OUTFILE来备份MySQL数据库并加上字段间的分隔符]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>