Chinaunix首页 | 论坛 | 博客
  • 博客访问: 708141
  • 博文数量: 112
  • 博客积分: 3889
  • 博客等级: 少校
  • 技术积分: 1448
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-19 16:35
个人简介

追求卓越,成功就会在不经意间追上你

文章分类

全部博文(112)

文章存档

2015年(1)

2014年(2)

2013年(1)

2012年(16)

2011年(86)

2010年(6)

分类: Mysql/postgreSQL

2012-04-24 16:03:47

由于之前一客户服务器异常导致innodb表数据文件损,加innodb_force_recovery参数后,不能使用mysqldump备份(未开启binlog);隧想到用select into outfile来导出相关数据.
但是select into outfile一次只能操作一张表,对于一个有三百多张表的数据库来就,如果一个表一个表地select,简直是一场噩梦!!!
于是便有了下面的脚本,以mysql数据库为例:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #FileName:select_into_bak.sh
  3. #Desc:Use select into outfile to backup db or tables
  4. #Created By:fedoracle
  5. #Date:2012/04/24

  6. DB=mysql
  7. USER=test
  8. PASSWD=test
  9. HOST=192.168.164.129
  10. BAK_DIR=/data/mysql/backup/$DB
  11. DATE=`date "+%Y-%m-%d %H-%M-%S"`

  12. [ -d "$BAK_DIR" ] || /bin/mkdir -p $BAK_DIR && /bin/chown mysql:mysql $BAK_DIR

  13. /usr/local/mysql/bin/mysql -h$HOST -u$USER -p$PASSWD -e "show tables from $DB" | grep -v "Tables_in" > $BAK_DIR/tables.txt

  14. for table in `cat $BAK_DIR/tables.txt`
  15. do
  16.     /usr/local/mysql/bin/mysql -h$HOST -u$USER -p$PASSWD -e "select * from $DB.$table into outfile '"$BAK_DIR/$table".txt'  character set utf8;"
  17. done

  18. cd $BAK_DIR
  19. /bin/tar -czf "$DB-$DATE".tar.gz *.txt
  20. /bin/rm -f *.txt

  21. exit 0
以下为测试结果:

点击(此处)折叠或打开

  1. [root@MySql01 scripts]# sh select_into_bak.sh
  2. [root@MySql01 scripts]# ll /data/mysql/backup/mysql
  3. total 128
  4. -rw-r--r-- 1 root root 125779 Apr 24 16:15 mysql-2012-04-24 16-15-30.tar.gz
  5. [root@MySql01 scripts]# tar -tvf /data/mysql/backup/mysql/mysql-2012-04-24\ 16-15-30.tar.gz
  6. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:30 columns_priv.txt
  7. -rw-rw-rw- mysql/mysql 152 2012-04-24 16:15:30 db.txt
  8. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:30 event.txt
  9. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:30 func.txt
  10. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:30 general_log.txt
  11. -rw-rw-rw- mysql/mysql 2 2012-04-24 16:15:30 global_trans_id.txt
  12. -rw-rw-rw- mysql/mysql 915 2012-04-24 16:15:30 help_category.txt
  13. -rw-rw-rw- mysql/mysql 5605 2012-04-24 16:15:30 help_keyword.txt
  14. -rw-rw-rw- mysql/mysql 7488 2012-04-24 16:15:30 help_relation.txt
  15. -rw-rw-rw- mysql/mysql 429714 2012-04-24 16:15:30 help_topic.txt
  16. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 host.txt
  17. -rw-rw-rw- mysql/mysql 6 2012-04-24 16:15:31 last_exec_tran.txt
  18. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 ndb_binlog_index.txt
  19. -rw-rw-rw- mysql/mysql 40 2012-04-24 16:15:31 plugin.txt
  20. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 procs_priv.txt
  21. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 proc.txt
  22. -rw-rw-rw- mysql/mysql 78 2012-04-24 16:15:31 proxies_priv.txt
  23. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 servers.txt
  24. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 slow_log.txt
  25. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 tables_priv.txt
  26. -rw-r--r-- root/root 308 2012-04-24 16:15:30 tables.txt
  27. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 time_zone_leap_second.txt
  28. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 time_zone_name.txt
  29. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 time_zone_transition.txt
  30. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 time_zone_transition_type.txt
  31. -rw-rw-rw- mysql/mysql 0 2012-04-24 16:15:31 time_zone.txt
  32. -rw-rw-rw- mysql/mysql 1288 2012-04-24 16:15:31 user.txt

阅读(6347) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

xiaozhenggang2012-04-26 08:58:57

重返人生: 博主~~~~~mysql select into outfile如何将远程数据表导出放到本地?.....
select into outfile 语句貌似是不能把remote server上的东东直接导出到本地的;
可以考虑在remote server上导出后再用rsync等工具把相关文档同步到本地

重返人生2012-04-25 15:59:23

博主~~~~~mysql select into outfile如何将远程数据表导出放到本地?