免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3297 | 回复: 0
打印 上一主题 下一主题

用awk分割 mysqldump结果 [复制链接]

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-30 17:02 |只看该作者 |倒序浏览
Awk, which has been existing for long time on UNIX like operating
system, may sound legacy for people who live in modern web-based
programming world. However, awk is really sophisticated and useful tool
for various purposes. Today, I'll show you how to use it with mysqldump
;)
One feature which is missing on the current mysqldump command
is to separate dump files per DB or table. It can store all data into a
large single file only, whether we run the command with --result-file
option or redirect the output to some file. Of course it is possible to
run mysqldump command several times per DB or table, but it cannot
produce a consistent snapshot at a certain time, each dump may be
time-shifted in other words, unless all involved tables are locked
during dump. So, we need to backup database using a single mysqldump
instance if we need a consistent data.
Now, you can make use of
awk! It is a really powerful text processor. As an output of mysqldump
is text, you can process the output using awk. See the following awk
script:
#!/usr/bin/awk -f
function is_new_db(db_name) {
  for (i = 1; i  current_file;
      for (i = 1; i > current_file;
    }
  } else if (num_db == 0) {
    num_prelines++;
    prelines[num_prelines] = $0;
  } else if ($2 == "Dump" && $3 == "completed") {
    num_postlines++;
    postlines[num_postlines] = "";
    num_postlines++;
    postlines[num_postlines] = $0;
  } else {
    print $0 >> current_file
  }
  next;
}
/^\/\*.+\*\/;/ {
  if (match($0, "character|collation")) {
    print $0 >> current_file;
  } else if (match($0, "SET")) {
    if (num_db == 0) {
      if (match(prelines[num_prelines], "^\-\-")) {
        num_prelines++;
        prelines[num_prelines] = "";
      }
      num_prelines++;
      prelines[num_prelines] = $0;
    } else {
      num_postlines++;
      postlines[num_postlines] = $0;
    }
  } else {
    print $0 >> current_file;
  }
  next;
}
{ print $0 >> current_file }
END {
  for (i = 1; i > current_file
    for (j = 1; j > current_file;
    }
  }
}
Save
the script into a file named separate-dump.awk etc, and make it
exectable (chmod u+rx). You can separate dump files per DB by using the
script like below:
shell> mysqldump -A --single-transaction --master-data=2 --flush-logs | ./separate-dump.awk
Then,
dump files named like "database_name.sql" are created under your
current directory! Of course, you can also process an existing dump
file like below:
shell> ./separate-dump.awk Making use of "good old fashioned" unixy command tools will make us happy ;)
Enjoy!!
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/90603/showart_2135671.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP