|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +IGNORE_TABLES=( dataflow_batch_export dataflow_batch_import log_customer log_quote log_summary log_summary_type log_url log_url_info log_visitor log_visitor_info log_visitor_online report_event index_event enterprise_logging_event_changes core_cache core_cache_tag core_session core_cache_tag ) |
| 4 | +IGNORE_TABLES_AGGRESSIVE=( report_compared_product_index report_viewed_product_index sales_flat_quote_address sales_flat_quote_shipping_rate enterprise_customer_sales_flat_quote enterprise_customer_sales_flat_quote_address sales_flat_quote ) |
| 5 | +TRUNCATE_TABLES=( dataflow_batch_export dataflow_batch_import log_customer log_quote log_summary log_summary_type log_url log_url_info log_visitor log_visitor_info log_visitor_online report_viewed_product_index report_compared_product_index report_event index_event index_process_event ) |
| 6 | +CONFIG_FILE="./app/etc/local.xml" |
| 7 | +DUMP_FILE="./var/db.sql" |
| 8 | + |
| 9 | +function usage() |
| 10 | +{ |
| 11 | +cat <<EOF |
| 12 | +Usage: $0 [OPTIONS] |
| 13 | +Version: 1.03 |
| 14 | +Author: www.sonassihosting.com |
| 15 | +Download: sys.sonassi.com/mage-dbdump.sh |
| 16 | +
|
| 17 | +This script is used to dump or restore Magento databases by reading |
| 18 | +the local.xml file and parsing the DB credentials. It strips out |
| 19 | +superfluous data (logs etc.) for smaller and quicker dumps. It also |
| 20 | +optimises the dump by avoiding locks where possible. |
| 21 | +
|
| 22 | +Dumps to $DUMP_FILE(.gz) |
| 23 | +
|
| 24 | +If you have pigz installed, it will use that over gzip for parallel |
| 25 | +compression/de-compression. |
| 26 | +
|
| 27 | +OPTIONS: |
| 28 | + -a Advertise awesome hosting |
| 29 | + -d Dump the database |
| 30 | + -r Restore the databse |
| 31 | + -z Use gzip compression (use with -d or -r) |
| 32 | + -e Use extended inserts |
| 33 | + -h Show help/usage |
| 34 | + -f Full dump, do not exclude any tables |
| 35 | + -A Aggressive dump, exclude (${IGNORE_TABLES_AGGRESSIVE[@]}) |
| 36 | + -B Exclude additional custom tables (space separated, within "double quotes") |
| 37 | + -F Do not ask questions and force all actions |
| 38 | + -i Interactive, enter a mysql> prompt |
| 39 | + -c Clean log and index tables |
| 40 | +
|
| 41 | +EOF |
| 42 | +} |
| 43 | + |
| 44 | +function error() |
| 45 | +{ |
| 46 | + echo -e "Error: $1" |
| 47 | + [[ ! "$2" == "noexit" ]] && exit 1 |
| 48 | +} |
| 49 | + |
| 50 | +function getParam() |
| 51 | +{ |
| 52 | + RETVAL=$(grep -Eoh "<$1>(<!\[CDATA\[)?(.*)(\]\]>)?<\/$1>" $TMP_FILE | sed "s#<$1><!\[CDATA\[##g;s#\]\]><\/$1>##g") |
| 53 | + if [[ "$2" == "sanitise" ]]; then |
| 54 | + RETVAL=$(echo "$RETVAL" | sed 's/"/\\\"/g') |
| 55 | + fi |
| 56 | + echo -e "$RETVAL" |
| 57 | +} |
| 58 | + |
| 59 | +function compress() |
| 60 | +{ |
| 61 | + while read DATA; do |
| 62 | + [[ ! "$OPT_z" == "" ]] && (echo "$DATA" | $GZIP_CMD -) || echo "$DATA" |
| 63 | + done |
| 64 | +} |
| 65 | + |
| 66 | +function mysqldumpit() |
| 67 | +{ |
| 68 | + |
| 69 | + if [[ "$OPT_f" == "" ]]; then |
| 70 | + [[ ! "$OPT_A" == "" ]] && IGNORE_TABLES=( ${IGNORE_TABLES[@]} ${IGNORE_TABLES_AGGRESSIVE[@]} ) |
| 71 | + [[ ! "$OPT_B" == "" ]] && IGNORE_TABLES=( ${IGNORE_TABLES[@]} $OPT_B ) |
| 72 | + for TABLE in "${IGNORE_TABLES[@]}"; do |
| 73 | + IGNORE_STRING="$IGNORE_STRING --ignore-table=$DBNAME.$TABLE_PREFIX$TABLE" |
| 74 | + done |
| 75 | + fi |
| 76 | + |
| 77 | + # We use --single-transaction in favour of --lock-tables=false , its slower, but less potential for unreliable dumps |
| 78 | + echo "SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';" |
| 79 | + ( mysqldump -p"$DBPASS" $MYSQL_ARGS --no-data --routines --triggers --single-transaction; \ |
| 80 | + mysqldump -p"$DBPASS" $MYSQL_ARGS $IGNORE_STRING --no-create-db --single-transaction ) | sed 's/DEFINER=[^*]*\*/\*/g' |
| 81 | +} |
| 82 | + |
| 83 | +function question() |
| 84 | +{ |
| 85 | + [[ ! "$OPT_F" == "" ]] && return 0 |
| 86 | + echo -n "$1 [y/N]: " |
| 87 | + read CONFIRM |
| 88 | + [[ "$CONFIRM" == "y" ]] || [[ "$CONFIRM" == "Y" ]] && return 0 |
| 89 | + return 1 |
| 90 | +} |
| 91 | + |
| 92 | +function message() |
| 93 | +{ |
| 94 | + STRIP=$(for i in {1..38}; do echo -n "#"; done) |
| 95 | + echo -e "$STRIP\n$1\n$STRIP" |
| 96 | +} |
| 97 | + |
| 98 | +function banner() |
| 99 | +{ |
| 100 | +cat <<EOT |
| 101 | +###################################### |
| 102 | +
|
| 103 | + (_) |
| 104 | + ___ ___ _ __ __ _ ___ ___ _ |
| 105 | +/ __|/ _ \| '_ \ / _' / __/ __| | |
| 106 | +\__ \ (_) | | | | (_| \__ \__ \ | |
| 107 | +|___/\___/|_| |_|\__'_|___/___/_| |
| 108 | +
|
| 109 | +For truly optimised Magento hosting |
| 110 | +Use http://www.sonassihosting.com ... |
| 111 | +
|
| 112 | +##################################### |
| 113 | +
|
| 114 | +EOT |
| 115 | +} |
| 116 | + |
| 117 | +[ ! -f "$CONFIG_FILE" ] && error "$CONFIG_FILE does not exist" |
| 118 | + |
| 119 | +while getopts "B:AdrzehfFaic" OPTION; do |
| 120 | + case $OPTION in |
| 121 | + a) |
| 122 | + banner |
| 123 | + exit 0 |
| 124 | + ;; |
| 125 | + h) |
| 126 | + usage |
| 127 | + exit 0 |
| 128 | + ;; |
| 129 | + :) |
| 130 | + error "Error: -$OPTION requires an argument" noexit |
| 131 | + exit 1 |
| 132 | + ;; |
| 133 | + \?) |
| 134 | + error "Error: Unknown option -$OPTION" noexit |
| 135 | + exit 1 |
| 136 | + ;; |
| 137 | + *) |
| 138 | + [[ "$OPTARG" == "" ]] && OPTARG='"-'$OPTION' 1"' |
| 139 | + OPTION="OPT_$OPTION" |
| 140 | + eval ${OPTION}=$OPTARG |
| 141 | + ;; |
| 142 | + esac |
| 143 | +done |
| 144 | + |
| 145 | +[[ "$OPT_c$OPT_i$OPT_d$OPT_r" == "" ]] && usage && exit 1 |
| 146 | + |
| 147 | +which mktemp >/dev/null 2>&1 |
| 148 | +[ $? -eq 0 ] && TMP_FILE=$(mktemp ./var/local.xml.XXXXX) || TMP_FILE="./var/.tmp.local.xml" |
| 149 | +sed -ne '/default_setup/,/\/default_setup/p' $CONFIG_FILE > $TMP_FILE |
| 150 | + |
| 151 | +which pigz >/dev/null 2>&1 |
| 152 | +[ $? -eq 0 ] && GZIP_CMD="pigz" || GZIP_CMD="gzip" |
| 153 | + |
| 154 | +IGNORE_STRING="" |
| 155 | +DBHOST=$(getParam "host") |
| 156 | +DBUSER=$(getParam "username") |
| 157 | +DBPASS=$(getParam "password" "sanitise" ) |
| 158 | +DBNAME=$(getParam "dbname") |
| 159 | +TABLE_PREFIX=$(getParam "table_prefix") |
| 160 | +[ -f $TMP_FILE ] && rm $TMP_FILE |
| 161 | +[[ ! "$OPT_z" == "" ]] && DUMP_FILE="$DUMP_FILE"".gz" |
| 162 | + |
| 163 | +MYSQL_ARGS="-f -h $DBHOST -u $DBUSER $DBNAME" |
| 164 | +[[ ! "$OPT_e" == "" ]] && MYSQL_ARGS="$MYSQL_ARGS --extended-insert=FALSE --complete-insert=TRUE" |
| 165 | + |
| 166 | +if [[ ! "$OPT_r" == "" ]]; then |
| 167 | + |
| 168 | + [ ! -f "$DUMP_FILE" ] && error "SQL file does not exist" |
| 169 | + question "Are you sure you want to restore $DUMP_FILE to $DBNAME?" |
| 170 | + if [ $? -eq 0 ]; then |
| 171 | + [[ ! "$OPT_z" == "" ]] && $GZIP_CMD -d <$DUMP_FILE | mysql $MYSQL_ARGS -p"$DBPASS" || mysql $MYSQL_ARGS -p"$DBPASS" <$DUMP_FILE |
| 172 | + message "MYSQL IMPORT COMPLETE" |
| 173 | + banner |
| 174 | + fi |
| 175 | + exit 0 |
| 176 | + |
| 177 | +elif [[ ! "$OPT_c" == "" ]]; then |
| 178 | + |
| 179 | + for TABLE in ${TRUNCATE_TABLES[@]}; do |
| 180 | + echo "Cleaning $TABLE ..." |
| 181 | + mysql $MYSQL_ARGS -p"$DBPASS" -e "TRUNCATE ${TABLE_PREFIX}$TABLE" |
| 182 | + done |
| 183 | + |
| 184 | +elif [[ ! "$OPT_i" == "" ]]; then |
| 185 | + |
| 186 | + mysql $MYSQL_ARGS -p"$DBPASS" |
| 187 | + |
| 188 | +elif [[ ! "$OPT_d" == "" ]]; then |
| 189 | + |
| 190 | + [[ ! "$OPT_z" == "" ]] && mysqldumpit | $GZIP_CMD > $DUMP_FILE || mysqldumpit > $DUMP_FILE |
| 191 | + message "MYSQL DUMP COMPLETE" |
| 192 | + exit 0 |
| 193 | + |
| 194 | +fi |
0 commit comments