Advertisement
Guest User

Untitled

a guest
May 19th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #!/bin/bash -
  2. #title :magedev_db.sh
  3. #description :For those times when n98-magerun db:dump is unavailable
  4. #author :Tegan Snyder
  5. #notes :dev db backups in Magento via bash (no production data)
  6. #==============================================================================
  7.  
  8. current_time=$(date "+%Y.%m.%d-%H.%M.%S")
  9.  
  10. # clean up tmp files
  11. rm -rf /tmp/mage_skip_tables.tmp
  12.  
  13. # setup connection
  14. read -p "Enter your MySQL server: " server
  15. read -p "Enter your MySQL database: " db
  16. read -p "Enter your MySQL username: " u
  17. read -sp "Enter your MySQL password: " p
  18.  
  19.  
  20. # wildcard tables that are not needed in a dev db
  21. QUERY="$(cat <<EOF
  22. SHOW TABLES
  23. FROM $db
  24. WHERE
  25. Tables_in_$db LIKE '%customer_address%'
  26. OR Tables_in_$db LIKE '%customer_entity%'
  27. OR Tables_in_$db LIKE '%sales_order_aggregated%'
  28. OR Tables_in_$db LIKE '%sales_order_tax%'
  29. OR Tables_in_$db LIKE '%sales_flat%'
  30. OR Tables_in_$db LIKE '%sales_recurring_%'
  31. OR Tables_in_$db LIKE '%sales_refunded_%'
  32. OR Tables_in_$db LIKE '%sales_payment_%'
  33. OR Tables_in_$db LIKE '%enterprise_sales_%'
  34. OR Tables_in_$db LIKE '%enterprise_customer_sales_%'
  35. OR Tables_in_$db LIKE '%sales_bestsellers_%'
  36. OR Tables_in_$db LIKE '%report_viewed_%'
  37. OR Tables_in_$db LIKE '%catalogsearch_%';
  38. EOF
  39. )"
  40.  
  41. # query for wildcard tables to skip
  42. mysql -h $server -u $u -p"$p" $db -N -B -e "$QUERY" > /tmp/mage_skip_tables.tmp
  43.  
  44. # add additional tables to skip to tmp file
  45. echo "log_url" >> /tmp/mage_skip_tables.tmp
  46. echo "log_url_info" >> /tmp/mage_skip_tables.tmp
  47. echo "log_visitor" >> /tmp/mage_skip_tables.tmp
  48. echo "log_visitor_info" >> /tmp/mage_skip_tables.tmp
  49. echo "log_visitor_online" >> /tmp/mage_skip_tables.tmp
  50. echo "report_event" >> /tmp/mage_skip_tables.tmp
  51. echo "report_compared_product_index" >> /tmp/mage_skip_tables.tmp
  52. echo "dataflow_batch" >> /tmp/mage_skip_tables.tmp
  53. echo "dataflow_batch_export" >> /tmp/mage_skip_tables.tmp
  54. echo "dataflow_batch_import" >> /tmp/mage_skip_tables.tmp
  55. echo "dataflow_import_data" >> /tmp/mage_skip_tables.tmp
  56. echo "dataflow_session" >> /tmp/mage_skip_tables.tmp
  57. echo "dump_command.sql" >> /tmp/mage_skip_tables.tmp
  58.  
  59.  
  60. # read through tables we need to ignore and create
  61. # concat them one by one into the ignore tables variable
  62. IGNORETABLES=""
  63. while read line
  64. do
  65. tbl=$line
  66. if [ -n "$tbl" ]; then
  67. IGNORETABLES="$IGNORETABLES --ignore-table=$db.$tbl"
  68. fi
  69. done < /tmp/mage_skip_tables.tmp
  70.  
  71. # dump the table definitons
  72. mysqldump --no-data -h $server -u $u -p"$p" $db > /tmp/mage_table_definitions.sql
  73.  
  74. # dump the tables
  75. mysqldump --no-create-info --skip-triggers --single-transaction -h $server -u $u -p"$p" $db $IGNORETABLES > /tmp/mage_database.sql
  76.  
  77. # combine the table definitons and table dumps to one file
  78. cat /tmp/mage_table_definitions.sql /tmp/mage_database.sql > /tmp/backup-$current_time-$db.sql
  79.  
  80. echo "dump saved: /tmp/backup-$current_time-$db.sql"
  81.  
  82. # clean up
  83. rm -rf /tmp/mage_skip_tables.tmp
  84. rm -rf /tmp/mage_table_definitions.sql
  85. rm -rf /tmp/mage_database.sql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement