Skip to content

Commit 4db4044

Browse files
author
sonassi
committed
Initial commit
0 parents  commit 4db4044

File tree

6 files changed

+916
-0
lines changed

6 files changed

+916
-0
lines changed

mage-bundle.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
PWD=$(pwd)
4+
KEY=$(wget -qO - http://pwgen.sonassi.com)
5+
FILENAME="http_$KEY.tgz"
6+
EXCLUDES=( "./var/log/*" "./var/cache/*" "./var/session/*" "./var/*port/*" "./var/tmp/*" "./media" "./errors/*" "./export/*" "./.git" "*.tar" "*.tgz" "*.gz" "*.bz2" "*.zip" "*.sql" "*.pdf" "*.mp3" "*.mp4" "*.mov" "*.avi" "./app/etc/local.xml" "./$FILENAME" )
7+
8+
echo -n "Please confirm you want to bundle ($PWD) [y/N]: "
9+
read CONFIRM
10+
[[ ! "$CONFIRM" == "y" ]] && exit 1
11+
12+
function build_excludes()
13+
{
14+
EXCLUDES_ALL=""
15+
for EXCLUDE in "${EXCLUDES[@]}"; do
16+
EXCLUDES_ALL="--exclude=$EXCLUDE $EXCLUDES_ALL"
17+
done
18+
echo $EXCLUDES_ALL
19+
}
20+
21+
wget sys.sonassi.com/mage-dbdump.sh
22+
bash mage-dbdump.sh
23+
tar chvfz $PWD/$FILENAME $(build_excludes) . var/db.sql
24+
25+
cat << EOF
26+
#######################################
27+
28+
MYSQL DUMP & TAR COMPLETE
29+
30+
Backup Location: $FILENAME
31+
32+
########################################
33+
EOF
34+
35+
cat <<EOT
36+
########################################
37+
38+
(_)
39+
___ ___ _ __ __ _ ___ ___ _
40+
/ __|/ _ \| '_ \ / _' / __/ __| |
41+
\__ \ (_) | | | | (_| \__ \__ \ |
42+
|___/\___/|_| |_|\__'_|___/___/_|
43+
44+
45+
Want truly optimised Magento hosting?
46+
47+
Try http://www.sonassihosting.com ...
48+
49+
#########################################
50+
EOT

mage-dbdump.sh

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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

mage-dbpurge.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
3+
CONFIG_FILE="./app/etc/local.xml"
4+
TMP_FILE="./var/.tmp.local.xml"
5+
DUMP_STRING=""
6+
7+
if [ ! -f "$CONFIG_FILE" ]; then
8+
echo "$CONFIG_FILE does not exist"
9+
exit
10+
fi
11+
12+
sed -ne '/default_setup/,/\/default_setup/p' $CONFIG_FILE > $TMP_FILE
13+
14+
function getParam()
15+
{
16+
RETVAL=$(grep -Eoh "<$1>(<!\[CDATA\[)?(.*)(\]\]>)?<\/$1>" $TMP_FILE | sed "s#<$1><!\[CDATA\[##g;s#\]\]><\/$1>##g")
17+
if [[ "$2" == "sanitise" ]]; then
18+
RETVAL=$(echo "$RETVAL" | sed 's/"/\\\"/g')
19+
fi
20+
echo -e "$RETVAL"
21+
}
22+
23+
24+
DBHOST=$(getParam "host")
25+
DBUSER=$(getParam "username")
26+
DBPASS=$(getParam "password" "sanitise" )
27+
DBNAME=$(getParam "dbname")
28+
TABLE_PREFIX=$(getParam "table_prefix")
29+
30+
[ -f $TMP_FILE ] && rm $TMP_FILE
31+
32+
echo "The database selected is: $DBNAME"
33+
echo -n "Are you sure you want to purge? [y/N]: "
34+
read CONFIRM; if [[ ! "$CONFIRM" == "y" ]]; then echo "You dodged a bullet there!"; exit; fi
35+
36+
echo -n "Last chance, are you 110% sure you want to purge $DBNAME? [y/N]: "
37+
read CONFIRM; if [[ ! "$CONFIRM" == "y" ]]; then echo "You dodged a bullet there!"; exit; fi
38+
39+
TABLES=( $(mysql -h$DBHOST -u$DBUSER -p"$DBPASS" $DBNAME -e 'show tables' | awk '{ print $1}' | grep -v '^Tables' ) )
40+
41+
for TABLE in ${TABLES[@]}; do
42+
DUMP_STRING="SET FOREIGN_KEY_CHECKS=0; DROP VIEW $TABLE; SET FOREIGN_KEY_CHECKS=1;"
43+
mysql --force -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" $DBNAME -e "$DUMP_STRING"
44+
DUMP_STRING="SET FOREIGN_KEY_CHECKS=0; DROP TABLE $TABLE; SET FOREIGN_KEY_CHECKS=1;"
45+
mysql --force -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" $DBNAME -e "$DUMP_STRING"
46+
done
47+
48+
cat <<EOT
49+
#######################################
50+
51+
MYSQL DB PURGE COMPLETE
52+
53+
#######################################
54+
EOT
55+
56+
cat <<EOT
57+
########################################
58+
59+
(_)
60+
___ ___ _ __ __ _ ___ ___ _
61+
/ __|/ _ \| '_ \ / _' / __/ __| |
62+
\__ \ (_) | | | | (_| \__ \__ \ |
63+
|___/\___/|_| |_|\__'_|___/___/_|
64+
65+
66+
Want truly optimised Magento hosting?
67+
68+
Try http://www.sonassihosting.com ...
69+
70+
#########################################
71+
EOT

0 commit comments

Comments
 (0)