Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1091571
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: Python/Ruby

2012-02-01 14:44:37

  1. # the final version ,by hfu

  2. #!/bin/sh
  3.  # dump or load the tables under the specified databases automatically.
  4.  # only apply to infobright ,untest on mysql.
  5.  # the following leaves just for debugging :)
  6.  
  7.  #set -x
  8.  
  9.  unalias -a

  10.  #source ~/.bash_profile

  11.  USER=$1
  12.  PWD=$2
  13.  HOST=$3
  14.  PORT=$4
  15.  DB=$5
  16.  LOCATION=$6
  17.  MODE=$7
  18.  # how much threads you want to run when dump/load tables

  19.  threads=1

  20.  function usage(){

  21. cat <<!
  22.       
  23. Usage : sh $0 user pwd host port dbname path {load|dump}
  24. database is the specified db name which you plan to load/dump data ,while path is the location where the export files store"
  25. e.g sh $0 mysql pwd 10.1.1.1 3306 app /tmp dump (Warning: /tmp couldn't type as /tmp/)

  26. !
  27.       }

  28.  if [ $# -ne 7 ] ;then
  29.     
  30.      echo "==================================================================================="
  31.      echo "first of all, you must be authorized to access remote mysql servers ,or the exception will be raised when you try to run this script"
  32.      usage
  33.      exit 1
  34.  fi

  35.  my_cmd="/usr/bin/mysql-ib -u$USER -p$PWD -h$HOST -P$PORT"

  36.  [ ! -d "$LOCATION/$DB" ] && sudo mkdir -p $LOCATION/$DB && sudo chown -R mysql:mysql $LOCATION/$DB || sudo chown -R mysql:mysql $LOCATION
  37.  
  38.  function dump_tables () {
  39.       
  40.       # get the table lists belong to the database that you specified in advance.
  41.       
  42.      $my_cmd -e "show tables from $DB\\G" |awk -F : '/Tables/{print $2}' > $LOCATION/$DB/${DB}_table_lists
  43.  
  44.      # obtain the sql statement "create database $DB "

  45.      $my_cmd -e "show create database $DB\\G" | awk -F "Create Database:" 'NR==3{v=$2}NR>3{v=v"\n"$0}END{print v";"}' > $LOCATION/$DB/create_${DB}_and_tabs.sql
  46.      
  47.      # now create_${DB}_and_tabs.sql starts with the line "create database xxx;",but we should append "use $DB" so that this sql statement could run smoothly

  48.      echo " USE $DB;" >> $LOCATION/$DB/create_${DB}_and_tabs.sql
  49.      
  50.       ############################ run multiple threads #################################

  51.       i=0
  52.       counter=0
  53.  
  54.       lines=$(awk 'END{print NR}' $LOCATION/$DB/${DB}_table_lists )
  55.       
  56.       # make sure that threads no. is smaller than the no. of tables
  57.       if [ $lines -lt $threads ] ;then threads=$lines ;fi

  58.       N=$(echo "$lines/$threads" |bc )
  59.  
  60.       for j in `cat $LOCATION/$DB/${DB}_table_lists ` ; do

  61.             $my_cmd -A $DB -e "show create table $j\\G" | awk -F : 'NR==3{v=$2}NR>3{v=v"\n"$0}END{print v";"}' >> $LOCATION/$DB/create_${DB}_and_tabs.sql

  62.             $my_cmd -A $DB -e "show create table $j\\G" | awk -F : 'NR==3{v=$2}NR>3{v=v"\n"$0}END{print v";"}' > $LOCATION/$DB/create_${j}.sql
  63.            
  64.            {
  65.             # generate sql statement ,like "select id ,name,xxx from xxx fields terminate by xxx escaped by xxx;
  66.  
  67.             awk -F \` -v d=$DB 'NR==1{t=$2}NR>1&&!/ENGINE/{if(v)v=v",replace("$2",\047\\\\\047,\047\\\\\\\\\047)";else v="replace("$2",\047\\\\\047,\047\\\\\\\\\047)"}END{print "select "v " from " t " in
  68. to OUTFILE \047/tmp/"d"/"t "\047 fields terminated by \047;\047 escaped by \047\\\\\047" }' $LOCATION/$DB/create_${j}.sql > $LOCATION/$DB/source_${j}.sql

  69.              $my_cmd -A $DB -e "source $LOCATION/$DB/source_${j}.sql";
  70.             
  71.             echo "export table $j .... done "
  72.             } &
  73.  
  74.             #get the sql statement "create table"


  75.            ((i++))
  76.  
  77.         if [ $counter -lt $N ] ;then
  78.  
  79.                if [ $i -ge $threads ] ; then
  80.                   wait
  81.                   i=0

  82.                    ((counter++))
  83.                    echo " "
  84.                    echo "================================$((lines-counter*threads)) tables left ==========================="
  85.                    echo " "
  86.                fi
  87.  
  88.        else
  89.            wait
  90.        fi
  91.  
  92.        done
  93.           
  94.           }


  95.  function load_tables () {
  96.       
  97. cat <<eof
  98. ------------------------------------------------------
  99.  
  100.   "now create the database and table structure "
  101.     
  102. -------------------------------------------------------
  103. eof
  104.  
  105.       $my_cmd -e "DROP DATABASE IF EXISTS $DB"
  106.       $my_cmd -e "source $LOCATION/$DB/create_${DB}_and_tabs.sql"
  107.             
  108.       echo " $DB and related tables rebuilt ....done "
  109.       i=0
  110.       counter=0

  111.      lines=$(awk 'END{print NR}' $LOCATION/${DB}/${DB}_table_lists )

  112.      N=$(echo $lines/$threads |bc )
  113.     
  114.       for j in `cat $LOCATION/${DB}/${DB}_table_lists`; do
  115.           echo "load ... $j "
  116.           { $my_cmd -A $DB -e "SET character_set_database=utf8;load data local infile '$LOCATION/$DB/$j' into table $j fields terminated by ';' escaped by '\\\\';"
  117.             } &
  118.  
  119.            ((i++))
  120.  
  121.         if [ $counter -lt $N ] ;then
  122.  
  123.                if [ $i -ge $threads ] ; then
  124.                   wait
  125.                   i=0

  126.                    ((counter++))
  127.                    echo " "
  128.                    echo "================================$((lines-counter*threads)) tables left =============================="
  129.                    echo " "
  130.  
  131.                fi
  132.  
  133.        else
  134.  
  135.            wait
  136.        fi

  137.       done
  138.          
  139.           }


  140. case $MODE in

  141.    dump)
  142.         dump_tables;
  143.         echo "----------------------------------------------------------------------------------------"
  144.         echo "now please move towards to $LOCATION, tar jcvf $DB.tar.bz2 $LOCATION/$DB,then sync to the objective hosts "
  145.         echo " "
  146.     ;;

  147.    load)
  148. cat <<!

  149. Best practice : if your dumped package located at /tmp/$DB.tar.{gz|bz2} ,cd /tmp and umcompress it, then run the script,pay much attention to the path

  150. in this case,it
阅读(1261) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

expert12012-02-26 10:41:37

generate all the create table sql statement under the specified db and redirect to the tmp file ,then extract the "select FIELDS from TAB ,use the abover script ,just like the Label(b,t) of Sed

expert12012-02-26 10:38:35

awk -F \`  '/CREATE/{if(tab)print "select " v, "from " tab  " into OUTFILE \047/tmp/"tab" \047fields terminated by \042;\042  escaped by \047\\\\\047"
;v="";tab=$2;next}!/ENGINE/{if(v)v=v","$2;else v=$2}END{print "select " v " from " tab  " into OUTFILE \047/tmp/"tab " \047fields terminated by \042;\042  escaped by \047\\\\\047"
}'