Chinaunix首页 | 论坛 | 博客
  • 博客访问: 454709
  • 博文数量: 97
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 1091
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 17:05
个人简介

专注于大规模运维场景运维工具解决方案。欢迎有这方面兴趣的朋友跟我联系。

文章分类

全部博文(97)

文章存档

2014年(12)

2013年(25)

2012年(60)

我的朋友

分类: Python/Ruby

2012-08-01 12:42:08

场景:需要将线上的数据同步到测试环境库里面。可以用这个脚本跑一下

点击(此处)折叠或打开

  1. <?php
  2. define('BASE_DIR',dirname(__FILE__)."/");
  3. $db_conf = array(
  4.     "armory"=>array(
  5.             "dbhost"=>'',
  6.             "dbport"=>'3306',
  7.             "dbuser"=>'',
  8.             "dbpass"=>'',
  9.             "dbname"=>'',
  10.             ),
  11.     "armory_dev"=>array(
  12.             "dbhost"=>'',
  13.             "dbport"=>'3306',
  14.             "dbuser"=>'',
  15.             "dbpass"=>'',
  16.             "dbname"=>'',
  17.             ),
  18.     "armory_test"=>array(
  19.             "dbhost"=>'',
  20.             "dbport"=>'3306',
  21.             "dbuser"=>'',
  22.             "dbpass"=>'56',
  23.             "dbname"=>'',
  24.             ),
  25. );
  26. $armory_link = db_connect('armory');
  27. if(isset($_SERVER['argv'][1]) && ($_SERVER['argv'][1] == 'dev')){
  28.     $armory_dev_link = db_connect('armory_dev');
  29. } else {
  30.     $armory_dev_link = db_connect('armory_test');
  31. }
  32. function db_connect($db_token = "armory_test"){
  33.     global $db_conf;    
  34.     $dblink = null;
  35.     $conf = $db_conf[$db_token];
  36.     $dblink = mysql_connect($conf['dbhost'].':'.$conf['dbport'],$conf['dbuser'],$conf['dbpass'],true);
  37.     if(!$dblink){
  38.         exit("DB connect error\n");
  39.     }
  40.     mysql_select_db($conf['dbname'],$dblink);
  41.     mysql_query("SET NAMES utf8",$dblink);
  42.     return $dblink;
  43. }
  44. // php synsql dev cabinet
  45. if(isset($_SERVER['argv'][2])){
  46.     $tableName = $_SERVER['argv'][2];
  47.     truncateTable($tableName);
  48.     if ($tableName == "device_base") {
  49.         $start = 0;
  50.      for($i = 0;$i < 9;$i++){
  51.             insertTable($value,$start);
  52.                $start = $start + 10000;
  53.         }    
  54.     } else {
  55.         insertTable($value,0);
  56.     }
  57.     //数据准确性校验
  58.     countTables($value);
  59.     exit(0);
  60. }

  61. /**
  62.  * 将线上的库导到测试环境
  63.  * */
  64. $syntable = array("machine_room","machine_idc");

  65. foreach($syntable as $value){
  66.     truncateTable($value);
  67.     if ($value == "app_server"){
  68.      $start = 0;
  69.      for($i = 0;$i <= 9;$i++){
  70.             insertTable($value,$start);
  71.      $start = $start + 10000;
  72.      }
  73.     } else if ($value == "device_base" || $value == "server" || $value == "os" || $value == "cpu") {
  74.         $start = 0;
  75.      truncateTable($value);
  76.      for($i = 0;$i < 9;$i++){
  77.             insertTable($value,$start);
  78.                $start = $start + 10000;
  79.         }    
  80.     } else {
  81.      truncateTable($value);
  82.      insertTable($value,0);
  83.     }
  84.     //数据准确性校验
  85.     countTables($value);
  86. }


  87. /**
  88.  * 输入一个表名查看两边的数据记录个数
  89.  * */
  90. function countTables($value) {
  91.     global $armory_link,$armory_dev_link;
  92.     $armory_query = mysql_query("select count(*) as num from $value limit 1",$armory_link);
  93.     $armory_result = mysql_fetch_array($armory_query);
  94.     $dev_query = mysql_query("select count(*) as num from $value limit 1",$armory_dev_link);
  95.     $dev_result = mysql_fetch_array($dev_query);
  96.     print "Table $value count is : " .$armory_result[0] . "\t" . $dev_result[0] . "\n";
  97. }



  98. /**
  99.  * 输入一个表名返回一个字段列表
  100.  * */
  101. function fieldByTable($tableName) {
  102.     global $armory_link;
  103.     $sql = "select * from $tableName limit 1";
  104.     $result = mysql_query($sql,$armory_link);
  105.     $field = array();
  106.     while($propery = mysql_fetch_field($result)) {
  107.         $field[] = $propery->name;
  108.     }
  109.     return $field;    
  110. }
  111. /**
  112.  * 输入一个表名将原opsdb的表给truncate掉
  113.  * */
  114. function truncateTable($tableName) {
  115.     global $armory_dev_link;
  116.     // 建议使用truncate 的方式delete的好像auto_increment的ID会有问题[fix bug]
  117.     $sql = "truncate table $tableName";
  118.     mysql_query($sql,$armory_dev_link);
  119. }

  120. /**
  121.  * 查询指定的表从线上取记录插入到测试库
  122.  * */
  123. function insertTable($tableName,$start) {
  124.     global $armory_dev_link,$armory_link;
  125.     $query = mysql_query("select * from $tableName limit $start,10000",$armory_link);
  126.     $field_array = fieldByTable($tableName);
  127.     while($arr = mysql_fetch_array($query)){
  128.         $armory_sql = "insert into $tableName(";
  129.         $filed_str = '';
  130.         $value_str = '';
  131.         foreach($field_array as $value) {
  132.     /**
  133.   排除掉null与empty string类型的字段,但0是需要同步的.[fix bug]
  134.               */
  135.             if(!is_null($arr[$value]) && ($arr[$value] ==0 || !empty($arr[$value])){            
  136.                 $filed_str.="`$value`,";
  137.                 $value_str.="'".addslashes($arr[$value])."',";
  138.             }
  139.         }
  140.         $filed_str = substr($filed_str,0,strlen($filed_str)-1);
  141.         $value_str = substr($value_str,0,strlen($value_str)-1);
  142.         $armory_sql = $armory_sql.$filed_str.") VALUES (".$value_str.")";
  143.         $result = mysql_query($armory_sql,$armory_dev_link);
  144.         if (!$result){
  145.          echo $armory_sql . "\n";
  146.          exit(0);
  147.         }
  148.     }
  149. }

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