Guest User

Untitled

a guest
Jun 21st, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -e -u
  3.  
  4. print_usage() {
  5. echo "Usage:
  6. mage-dump <OPTIONS> [database]
  7. Dumps a magento database without logging data
  8. -u <db_user> DB username
  9. -p <db_pass> DB password
  10. -h <db_host> DB host
  11. -o <output_file> Output file - Default: mysqldb-<db_name>-<date>.sql.gz
  12. -f <local_xml> Path to Magento local.xml file - Default: app/etc/local.xml
  13.  
  14. [Database] argument is required if local.xml can not be located
  15. "
  16. }
  17.  
  18. localxml_path="app/etc/local.xml"
  19. db_host=localhost
  20. db_user=root
  21. db_pass=
  22. db_name=
  23. db_output=
  24. tmp_file_prefix=/tmp/$(basename $0).$RANDOM
  25. trap "rm -f $tmp_file_prefix.*" EXIT
  26.  
  27. while getopts u:p:h:o:f: opt; do
  28. case $opt in
  29. u)
  30. db_user=$OPTARG
  31. ;;
  32. p)
  33. db_pass=$OPTARG
  34. ;;
  35. h)
  36. db_host=$OPTARG
  37. ;;
  38. o)
  39. db_output=$OPTARG
  40. ;;
  41. f)
  42. localxml_path=$OPTARG
  43. ;;
  44. ?)
  45. print_usage
  46. exit
  47. ;;
  48. esac
  49. done
  50.  
  51. shift $((OPTIND - 1))
  52.  
  53. if [[ $# -gt 0 ]]; then
  54. db_name=$1
  55. shift
  56. fi
  57.  
  58. # Try to get database settings from magento... PHP time!
  59. if [[ -z "$db_pass" ]] || [[ -z "$db_name" ]]; then
  60. if [[ -f "$localxml_path" ]]; then
  61. php_code_file=$tmp_file_prefix.$RANDOM
  62. php_result_file=$tmp_file_prefix.$RANDOM
  63.  
  64. php_code='<?php
  65. $xml = simplexml_load_file($argv[1]);
  66. $dbs = array();
  67. $db = null;
  68. foreach($xml->global->resources->children() as $k=>$v) {
  69. if($k == "db") continue;
  70. $dbs[] = array($k, $v);
  71. $db = $v;
  72. }
  73.  
  74. if(count($dbs) > 1) {
  75. while(true) {
  76. echo "Possible database configurations:\n";
  77. $n = 1;
  78. foreach($dbs as $db) {
  79. echo " $n) " . $db[0] . " (" .
  80. $db[1]->connection->username . "@" .
  81. $db[1]->connection->host . " " .
  82. $db[1]->connection->dbname . ")\n";
  83.  
  84. $n++;
  85. }
  86.  
  87. $choice = readline("Use connection: ");
  88. readline_add_history($choice);
  89. if(isset($dbs[$choice - 1])) {
  90. $db = $dbs[$choice - 1][1];
  91. break;
  92. }
  93. }
  94. }
  95.  
  96. $fh = fopen($argv[2], "w");
  97. fwrite($fh, "db_host=" . $db->connection->host . "\n");
  98. fwrite($fh, "db_user=" . $db->connection->username . "\n");
  99. fwrite($fh, "db_pass=" . $db->connection->password . "\n");
  100. fwrite($fh, "db_name=" . $db->connection->dbname . "\n");
  101. fclose($fh);'
  102.  
  103. echo $php_code > $php_code_file
  104. php "$php_code_file" "$localxml_path" "$php_result_file"
  105. . $php_result_file
  106. rm -f $php_result_file
  107. fi
  108. fi
  109.  
  110. if [[ -z "$db_name" ]]; then
  111. print_usage
  112. exit 1
  113. fi
  114.  
  115. if [[ -z "$db_output" ]]; then
  116. db_output=mysqldb-${db_name}-`date +"%Y-%m-%d_%H-%M"`.sql.gz
  117. fi
  118.  
  119. if [[ -f "$db_output" ]]; then
  120. echo "$db_output already exist!"
  121. exit 1
  122. fi
  123.  
  124. if [[ -z "$db_pass" ]]; then
  125. echo -n "MySQL Password for ${db_user}@${db_host} for ${db_name}: "
  126. read -s db_pass
  127. echo -e -n "\n"
  128. fi
  129.  
  130. echo "Starting dump to ${db_output}"
  131.  
  132. (
  133. check_last_line() {
  134. lastLine=
  135. while read line; do
  136. echo $line
  137. lastLine=$line
  138. done
  139.  
  140. if [[ $lastLine != --\ Dump\ completed\ on\ * ]]; then
  141. echo "Backup failed?! Aborting! Got last line:" 1>&2
  142. echo $lastLine 1>&2
  143. exit 1
  144. fi
  145. }
  146.  
  147. mysqldump -p${db_pass} -h${db_host} -u${db_user} ${db_name} \
  148. --ignore-table=${db_name}.core_cache \
  149. --ignore-table=${db_name}.core_cache_tag \
  150. --ignore-table=${db_name}.log_url \
  151. --ignore-table=${db_name}.log_url_info \
  152. --ignore-table=${db_name}.log_visitor \
  153. --ignore-table=${db_name}.log_visitor_info \
  154. --ignore-table=${db_name}.log_customer \
  155. --ignore-table=${db_name}.log_quote \
  156. --ignore-table=${db_name}.report_event \
  157. --ignore-table=${db_name}.report_viewed_product_index \
  158. --ignore-table=${db_name}.index_event \
  159. --ignore-table=${db_name}.index_process_event \
  160. --ignore-table=${db_name}.dataflow_batch_export \
  161. --ignore-table=${db_name}.dataflow_batch_import \
  162. | check_last_line
  163.  
  164. mysqldump -p${db_pass} -h${db_host} -u${db_user} ${db_name} \
  165. --no-data \
  166. core_cache \
  167. core_cache_tag \
  168. log_url \
  169. log_url_info \
  170. log_visitor \
  171. log_visitor_info \
  172. log_customer \
  173. log_quote \
  174. report_event \
  175. report_viewed_product_index \
  176. index_event \
  177. index_process_event \
  178. dataflow_batch_export \
  179. dataflow_batch_import \
  180. | check_last_line
  181. ) | gzip -9 > ${db_output}
  182.  
  183. if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
  184. rm -f ${db_output}
  185. else
  186. echo "Finished dump!"
  187. fi
Add Comment
Please, Sign In to add comment