说成mysql的纯文本格式备份与恢复好像不太正确,但是为了体现这种备份的好处,还是这样叫好了。所谓的mysql纯文本格式数据,就是在数据表中没有insert into这样的查询sql语句的存在,而只有类似如下:
'123','xxx','xxx','xxx'
'123','xxx','xxx','xxx'
'123','xxx','xxx','xxx'
'123','xxx','xxx','xxx'
的纯数据结构,这样的备份好处显而易见,当数据库中的数据特别大的时候,这样可以为我们节约很多存储空间,当然要实现这种备份的方式,当然难度也要比传统的mysqldump实现的难度要大。下面我以mysql数据库中的test数据库为例子,用shell脚本实现这种备份
先上备份的shell脚本:
#!/bin/bash
tables=`sudo /Applications/XAMPP/xamppfiles/bin/mysql -uroot -p test -e 'show tables'`
echo $tables
for i in $tables
do
if [ $i = 'Tables_in_test' ]
then
continue
fi
sudo /Applications/XAMPP/xamppfiles/bin/mysqldump -uroot -l -T /tmp/ test $i --fields-enclosed=\" --fields-terminated-by=,
done
执行这段shell以后,就可以把数据和sql文件全部备份到/tmp/目录下了,下面是恢复的shell脚本
#!/bin/bash
sqlList=`ls /tmp/*.sql`
txtList=`ls /tmp/*.txt`
for i in $sqlList
do
sudo /Applications/XAMPP/xamppfiles/bin/mysql -uroot test < $i
done
for i in $txtList
do
sudo /Applications/XAMPP/xamppfiles/bin/mysqlimport --user=root test --fields-enclosed-by=\" --fields-terminated-by=, $i
done
恢复的时候,我们需要把sql文件和txt分别导入进来,先把所有的sql文件导入进来,然后再把具体的数据文件txt导入进来