教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 mysql纯文本格式备份的shell脚本

mysql纯文本格式备份的shell脚本

发布时间:2014-11-24   编辑:jiaochengji.com
本文分享一段shell脚本,用于实现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 
执行脚本,即可把mysql数据和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即可。

您可能感兴趣的文章:
mysql备份与同步脚本
vps自动备份shell脚本代码
mysql纯文本格式备份的shell脚本
mysql命令之备份数据库
Linux下备份与还原整个MySQL整个数据库的脚本
shell脚本实现mysql查询结果保存到文件中
备份mysql数据库且删除十五天前备份的shell脚本
mysql备份并压缩存档的shell脚本
linux 下mysql自动备份的shell脚本
mysql数据库备份脚本一例

[关闭]
~ ~