Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- set -e -u
- print_usage() {
- echo "Usage:
- mage-dump <OPTIONS> [database]
- Dumps a magento database without logging data
- -u <db_user> DB username
- -p <db_pass> DB password
- -h <db_host> DB host
- -o <output_file> Output file - Default: mysqldb-<db_name>-<date>.sql.gz
- -f <local_xml> Path to Magento local.xml file - Default: app/etc/local.xml
- [Database] argument is required if local.xml can not be located
- "
- }
- localxml_path="app/etc/local.xml"
- db_host=localhost
- db_user=root
- db_pass=
- db_name=
- db_output=
- tmp_file_prefix=/tmp/$(basename $0).$RANDOM
- trap "rm -f $tmp_file_prefix.*" EXIT
- while getopts u:p:h:o:f: opt; do
- case $opt in
- u)
- db_user=$OPTARG
- ;;
- p)
- db_pass=$OPTARG
- ;;
- h)
- db_host=$OPTARG
- ;;
- o)
- db_output=$OPTARG
- ;;
- f)
- localxml_path=$OPTARG
- ;;
- ?)
- print_usage
- exit
- ;;
- esac
- done
- shift $((OPTIND - 1))
- if [[ $# -gt 0 ]]; then
- db_name=$1
- shift
- fi
- # Try to get database settings from magento... PHP time!
- if [[ -z "$db_pass" ]] || [[ -z "$db_name" ]]; then
- if [[ -f "$localxml_path" ]]; then
- php_code_file=$tmp_file_prefix.$RANDOM
- php_result_file=$tmp_file_prefix.$RANDOM
- php_code='<?php
- $xml = simplexml_load_file($argv[1]);
- $dbs = array();
- $db = null;
- foreach($xml->global->resources->children() as $k=>$v) {
- if($k == "db") continue;
- $dbs[] = array($k, $v);
- $db = $v;
- }
- if(count($dbs) > 1) {
- while(true) {
- echo "Possible database configurations:\n";
- $n = 1;
- foreach($dbs as $db) {
- echo " $n) " . $db[0] . " (" .
- $db[1]->connection->username . "@" .
- $db[1]->connection->host . " " .
- $db[1]->connection->dbname . ")\n";
- $n++;
- }
- $choice = readline("Use connection: ");
- readline_add_history($choice);
- if(isset($dbs[$choice - 1])) {
- $db = $dbs[$choice - 1][1];
- break;
- }
- }
- }
- $fh = fopen($argv[2], "w");
- fwrite($fh, "db_host=" . $db->connection->host . "\n");
- fwrite($fh, "db_user=" . $db->connection->username . "\n");
- fwrite($fh, "db_pass=" . $db->connection->password . "\n");
- fwrite($fh, "db_name=" . $db->connection->dbname . "\n");
- fclose($fh);'
- echo $php_code > $php_code_file
- php "$php_code_file" "$localxml_path" "$php_result_file"
- . $php_result_file
- rm -f $php_result_file
- fi
- fi
- if [[ -z "$db_name" ]]; then
- print_usage
- exit 1
- fi
- if [[ -z "$db_output" ]]; then
- db_output=mysqldb-${db_name}-`date +"%Y-%m-%d_%H-%M"`.sql.gz
- fi
- if [[ -f "$db_output" ]]; then
- echo "$db_output already exist!"
- exit 1
- fi
- if [[ -z "$db_pass" ]]; then
- echo -n "MySQL Password for ${db_user}@${db_host} for ${db_name}: "
- read -s db_pass
- echo -e -n "\n"
- fi
- echo "Starting dump to ${db_output}"
- (
- check_last_line() {
- lastLine=
- while read line; do
- echo $line
- lastLine=$line
- done
- if [[ $lastLine != --\ Dump\ completed\ on\ * ]]; then
- echo "Backup failed?! Aborting! Got last line:" 1>&2
- echo $lastLine 1>&2
- exit 1
- fi
- }
- mysqldump -p${db_pass} -h${db_host} -u${db_user} ${db_name} \
- --ignore-table=${db_name}.core_cache \
- --ignore-table=${db_name}.core_cache_tag \
- --ignore-table=${db_name}.log_url \
- --ignore-table=${db_name}.log_url_info \
- --ignore-table=${db_name}.log_visitor \
- --ignore-table=${db_name}.log_visitor_info \
- --ignore-table=${db_name}.log_customer \
- --ignore-table=${db_name}.log_quote \
- --ignore-table=${db_name}.report_event \
- --ignore-table=${db_name}.report_viewed_product_index \
- --ignore-table=${db_name}.index_event \
- --ignore-table=${db_name}.index_process_event \
- --ignore-table=${db_name}.dataflow_batch_export \
- --ignore-table=${db_name}.dataflow_batch_import \
- | check_last_line
- mysqldump -p${db_pass} -h${db_host} -u${db_user} ${db_name} \
- --no-data \
- core_cache \
- core_cache_tag \
- log_url \
- log_url_info \
- log_visitor \
- log_visitor_info \
- log_customer \
- log_quote \
- report_event \
- report_viewed_product_index \
- index_event \
- index_process_event \
- dataflow_batch_export \
- dataflow_batch_import \
- | check_last_line
- ) | gzip -9 > ${db_output}
- if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
- rm -f ${db_output}
- else
- echo "Finished dump!"
- fi
Add Comment
Please, Sign In to add comment