Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. # @author: Christian Noel Reyes <darkcolonist@gmail.com>
  2. # @date: 2015-05-13
  3. # @description: data / table-per table migration script.
  4. # @disclaimer:
  5. # source and target databases must have the same table structure for this script to work with no issues
  6. # tested with mysql 5.1 (client)
  7.  
  8. # usage:
  9. # bash migrate.sh --shost <source host> --sdb <source database> --suser <source username> --spass <source password>\
  10. # --thost <target host> --tdb <target database> --tuser <target username> --tpass <target password>\
  11. # --limit 10000;
  12. #
  13. # example:
  14. # bash migrate.sh --shost localhost --sdb test_db1_s --suser root --spass mypassword\
  15. # --thost localhost --tdb test_db1_t --tuser root --tpass mypassword\
  16. # --limit 10000;
  17.  
  18. # default arguments {
  19. migratION_shost="localhost";
  20. migratION_sdb="db_loop";
  21. migratION_suser="dev";
  22. migratION_spass="dev";
  23. migratION_sport="3306";
  24. migratION_limit="500000";
  25. # } default arguments
  26.  
  27. # parse arguments {
  28. for ((i=1;i<=$#;i++));
  29. do
  30.  
  31. # master/host details {
  32. if [ ${!i} = "--shost" ]
  33. then ((i++))
  34. migratION_shost=${!i};
  35.  
  36. elif [ ${!i} = "--sdb" ];
  37. then ((i++))
  38. migratION_sdb=${!i};
  39.  
  40. elif [ ${!i} = "--suser" ];
  41. then ((i++))
  42. migratION_suser=${!i};
  43.  
  44. elif [ ${!i} = "--spass" ];
  45. then ((i++))
  46. migratION_spass=${!i};
  47.  
  48. elif [ ${!i} = "--sport" ];
  49. then ((i++))
  50. migratION_sport=${!i};
  51. # } master details
  52.  
  53. elif [ ${!i} = "--limit" ];
  54. then ((i++))
  55. migratION_limit=${!i};
  56. fi
  57.  
  58. done;
  59. # } parse arguments
  60. iteration=0;
  61.  
  62. echo "process initiating...";
  63.  
  64. mysql --skip-column-names --silent -P$migratION_sport -h$migratION_shost -u$migratION_suser -p$migratION_spass $migratION_sdb --execute="SHOW TABLES;" | while read -r a_table ; do
  65. iteration=$((iteration+1))
  66.  
  67. echo "[$iteration] > exporting from $migratION_shost.$migratION_sdb.$a_table";
  68. mysqldump --replace --single-transaction --no-create-db --no-create-info -P$migratION_sport -h$migratION_shost -u$migratION_suser -p$migratION_spass --where="1=1 ORDER BY id DESC LIMIT $migratION_limit" $migratION_sdb $a_table > $migratION_sdb.$a_table.sql
  69.  
  70. tar cvzf $migratION_sdb.$a_table.sql.tar.gz $migratION_sdb.$a_table.sql;
  71. rm $migratION_sdb.$a_table.sql;
  72.  
  73. # sleep necessary for freeing up memory and/or cpu proc (uncomment this if and only if necessary)
  74. sleep .25
  75. done;
  76.  
  77. echo "process ended!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement