mysql备份脚本之SELECT INTO OUTFILE

6334阅读 2评论2012-04-24 xiaozhenggang
分类:Mysql/postgreSQL

由于之前一客户服务器异常导致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

上一篇:关于mysql复制的一些问题
下一篇:Mongodb学习笔记--Mongodb 安装

文章评论