-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathanonymizer.sh
executable file
·333 lines (296 loc) · 15.7 KB
/
anonymizer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
echo "*** This script is anonymizing a DB-dump of the LIVE-DB in the DEMO-Environment ***"
PATH_TO_ROOT=$1
if [[ "$PATH_TO_ROOT" == "" && -f "app/etc/local.xml" ]]; then
PATH_TO_ROOT="."
fi
if [[ "$PATH_TO_ROOT" == "" ]]; then
echo "Please specify the path to your Magento store"
exit 1
fi
CONFIG=$PATH_TO_ROOT"/.anonymizer.cfg"
if [[ 1 < $# ]]; then
if [[ "-c" == "$1" ]]; then
PATH_TO_ROOT=$3
CONFIG=$2
if [[ ! -f $CONFIG ]]; then
echo -e "\E[1;31mCaution: \E[0mConfiguration file $CONFIG does not exist, yet! You will be asked to create it after the anonymization run."
echo "Do you want to continue (Y/n)?"; read CONTINUE;
if [[ ! -z "$CONTINUE" && "$CONTINUE" != "y" && "$CONTINUE" != "Y" ]]; then
exit;
fi
fi
fi
fi
while [[ ! -f $PATH_TO_ROOT/app/etc/local.xml ]]; do
echo "$PATH_TO_ROOT is no valid Magento root folder. Please enter the correct path:"
read PATH_TO_ROOT
done
HOST=`grep host $PATH_TO_ROOT/app/etc/local.xml | grep CDATA | sed 's/ *<host>\(.*\)<\/host>/\1/' | sed 's/<!\[CDATA\[//' | sed 's/\]\]>//'`
USER=`grep username $PATH_TO_ROOT/app/etc/local.xml | grep CDATA | sed 's/ *<username>\(.*\)<\/username>/\1/' | sed 's/<!\[CDATA\[//' | sed 's/\]\]>//'`
PASS=`grep password $PATH_TO_ROOT/app/etc/local.xml | grep CDATA | sed 's/ *<password>\(.*\)<\/password>/\1/' | sed 's/<!\[CDATA\[//' | sed 's/\]\]>//'`
NAME=`grep dbname $PATH_TO_ROOT/app/etc/local.xml | grep CDATA | sed 's/ *<dbname>\(.*\)<\/dbname>/\1/' | sed 's/<!\[CDATA\[//' | sed 's/\]\]>//'`
if [[ -f "$CONFIG" ]]; then
echo "Using configuration file $CONFIG"
source "$CONFIG"
fi
if [[ -z "$DEV_IDENTIFIERS" ]]; then
DEV_IDENTIFIERS=".*(dev|stage|staging|test|anonym).*"
fi
if [[ $NAME =~ $DEV_IDENTIFIERS ]]; then
echo "We are on the TEST environment, everything is fine"
else
echo ""
echo "IT SEEMS THAT WE ARE ON THE PRODUCTION ENVIRONMENT!"
echo ""
echo "If you are sure, this is a test environment, please type 'test' to continue"
read force
if [[ "$force" != "test" ]]; then
echo "Canceled"
exit 2
fi
fi
if [ "$PASS" = "" ]; then
DBCALL="mysql -u$USER -h$HOST $NAME -e"
else
DBCALL="mysql -u$USER -p$PASS -h$HOST $NAME -e"
fi
echo "* Step 1: Anonymize Names and eMails"
if [[ -z "$RESET_ADMIN_PASSWORDS" ]]; then
echo " Do you want me to reset admin user passwords (Y/n)?"; read RESET_ADMIN_PASSWORDS
fi
if [[ "$RESET_ADMIN_PASSWORDS" == "y" || "$RESET_ADMIN_PASSWORDS" == "Y" || -z "$RESET_ADMIN_PASSWORDS" ]]; then
RESET_ADMIN_PASSWORDS="y"
# admin user
$DBCALL "UPDATE admin_user SET password=MD5(CONCAT(username,'123'))"
else
RESET_ADMIN_PASSWORDS="n"
fi
if [[ -z "$RESET_API_PASSWORDS" ]]; then
echo " Do you want me to reset API user passwords (Y/n)?"; read RESET_API_PASSWORDS
fi
if [[ "$RESET_API_PASSWORDS" == "y" || "$RESET_API_PASSWORDS" == "Y" || -z "$RESET_API_PASSWORDS" ]]; then
RESET_API_PASSWORDS="y"
# api user
$DBCALL "UPDATE api_user SET api_key=MD5(CONCAT(username,'123'))"
else
RESET_API_PASSWORDS="n"
fi
if [[ -z "$ANONYMIZE" ]]; then
echo " Do you want me to anonymize your database (Y/n)?"; read ANONYMIZE
fi
if [[ "$ANONYMIZE" == "y" || "$ANONYMIZE" == "Y" || -z "$ANONYMIZE" ]]; then
ANONYMIZE="y"
# customer address
ENTITY_TYPE="customer_address"
ATTR_CODE="firstname"
$DBCALL "UPDATE customer_address_entity_varchar SET value=CONCAT('firstname_',entity_id) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
ATTR_CODE="lastname"
$DBCALL "UPDATE customer_address_entity_varchar SET value=CONCAT('lastname_',entity_id) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
ATTR_CODE="telephone"
$DBCALL "UPDATE customer_address_entity_varchar SET value=CONCAT('0341 12345',entity_id) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
ATTR_CODE="fax"
$DBCALL "UPDATE customer_address_entity_varchar SET value=CONCAT('0171 12345',entity_id) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
ATTR_CODE="street"
$DBCALL "UPDATE customer_address_entity_text SET value=CONCAT(entity_id,' test avenue') WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
# customer account data
if [[ -z "$KEEP_EMAIL" ]]; then
echo " If you want to keep some users credentials, please enter corresponding email addresses quoted by '\"' separated by comma (default: none):"; read KEEP_EMAIL
fi
ERRORS_KEEP_MAIL=`echo "$KEEP_EMAIL" | grep -vP -e '(\"[^\"]+@[^\"]+\")(, ?(\"[^\"]+@[^\"]+\"))*'`
if [[ ! -z "$ERRORS_KEEP_MAIL" && "$KEEP_EMAIL" != '"none"' ]]; then
while [[ ! -z "$errors_keep_mail" ]]; do
echo -e "\e[1;31minvalid input! \E[0mExample: \"[email protected]\",\"[email protected]\"."
echo " If you want to keep some users credentials, please enter corresponding email addresses quoted by '\"' separated by comma (default: none):"; read KEEP_EMAIL
ERRORS_KEEP_MAIL=`echo "$KEEP_EMAIL" | grep -vP -e '(\"[^\"]+@[^\"]+\")(, ?(\"[^\"]+@[^\"]+\"))*'`
if [[ -z "$KEEP_MAIL" ]]; then
break
fi
done
if [[ ! -z "$KEEP_EMAIL" ]]; then
echo " Keeping $KEEP_EMAIL"
fi
else
KEEP_EMAIL='"none"'
fi
ENTITY_TYPE="customer"
$DBCALL "UPDATE customer_entity SET email=CONCAT('dev_',entity_id,'@trash-mail.com') WHERE email NOT IN ($KEEP_EMAIL)"
ATTR_CODE="firstname"
$DBCALL "UPDATE customer_entity_varchar SET value=CONCAT('firstname_',entity_id) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
ATTR_CODE="lastname"
$DBCALL "UPDATE customer_entity_varchar SET value=CONCAT('lastname_',entity_id) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE'))"
ATTR_CODE="password_hash"
$DBCALL "UPDATE customer_entity_varchar v SET value=MD5(CONCAT('dev_',entity_id,'@trash-mail.com')) WHERE attribute_id=(select attribute_id from eav_attribute where attribute_code='$ATTR_CODE' and entity_type_id=(select entity_type_id from eav_entity_type where entity_type_code='$ENTITY_TYPE')) AND (SELECT email FROM customer_entity e WHERE e.entity_id=v.entity_id AND email NOT IN ($KEEP_EMAIL))"
# credit memo
$DBCALL "UPDATE sales_flat_creditmemo_grid SET billing_name='Demo User'"
# invoices
$DBCALL "UPDATE sales_flat_invoice_grid SET billing_name='Demo User'"
# shipments
$DBCALL "UPDATE sales_flat_shipment_grid SET shipping_name='Demo User'"
# quotes
$DBCALL "UPDATE sales_flat_quote SET customer_email=CONCAT('dev_',entity_id,'@trash-mail.com'), customer_firstname='Demo', customer_lastname='User', customer_middlename='Dev', remote_ip='192.168.1.1', password_hash=NULL WHERE customer_email NOT IN ($KEEP_EMAIL)"
$DBCALL "UPDATE sales_flat_quote_address SET firstname='Demo', lastname='User', company=NULL, telephone=CONCAT('0123-4567', address_id), street=CONCAT('Devstreet ',address_id), email=CONCAT('dev_',address_id,'@trash-mail.com')"
# orders
$DBCALL "UPDATE sales_flat_order SET customer_email=CONCAT('dev_',entity_id,'@trash-mail.com'), customer_firstname='Demo', customer_lastname='User', customer_middlename='Dev'"
$DBCALL "UPDATE sales_flat_order_address SET email=CONCAT('dev_',entity_id,'@trash-mail.com'), firstname='Demo', lastname='User', company=NULL, telephone=CONCAT('0123-4567', entity_id), street=CONCAT('Devstreet ',entity_id)"
$DBCALL "UPDATE sales_flat_order_grid SET shipping_name='Demo D. User', billing_name='Demo D. User'"
# payments
$DBCALL "UPDATE sales_flat_order_payment SET additional_data=NULL, additional_information=NULL"
# newsletter
$DBCALL "UPDATE newsletter_subscriber SET subscriber_email=CONCAT('dev_newsletter_',subscriber_id,'@trash-mail.com') WHERE subscriber_email NOT IN ($KEEP_EMAIL)"
else
ANONYMIZE="n"
fi
if [[ -z "$TRUNCATE_LOGS" ]]; then
echo " Do you want me to truncate log tables (Y/n)?"; read TRUNCATE_LOGS
fi
if [[ "$TRUNCATE_LOGS" == "y" || "$TRUNCATE_LOGS" == "Y" || -z "$TRUNCATE_LOGS" ]]; then
TRUNCATE_LOGS="y"
# truncate unrequired tables
$DBCALL "TRUNCATE log_url"
$DBCALL "TRUNCATE log_url_info"
$DBCALL "TRUNCATE log_visitor"
$DBCALL "TRUNCATE log_visitor_info"
$DBCALL "TRUNCATE report_event"
else
TRUNCATE_LOGS="n"
fi
echo "* Step 2: Mod Config."
# disable assets merging, google analytics and robots
if [[ -z "$DEMO_NOTICE" ]]; then
echo " Do you want me to enable demo notice (Y/n)?"; read DEMO_NOTICE
fi
if [[ "$DEMO_NOTICE" == "y" || "$DEMO_NOTICE" == "Y" || -z "$DEMO_NOTICE" ]]; then
DEMO_NOTICE="y"
$DBCALL "UPDATE core_config_data SET value='1' WHERE path='design/head/demonotice'"
else
DEMO_NOTICE="n"
fi
$DBCALL "UPDATE core_config_data SET value='0' WHERE path='dev/css/merge_css_files' OR path='dev/js/merge_files'"
$DBCALL "UPDATE core_config_data SET value='0' WHERE path='google/analytics/active'"
$DBCALL "UPDATE core_config_data SET value='NOINDEX,NOFOLLOW' WHERE path='design/head/default_robots'"
# set mail receivers
$DBCALL "UPDATE core_config_data SET value='[email protected]' WHERE path='trans_email/ident_general/email'"
$DBCALL "UPDATE core_config_data SET value='[email protected]' WHERE path='trans_email/ident_sales/email'"
$DBCALL "UPDATE core_config_data SET value='[email protected]' WHERE path='trans_email/ident_support/email'"
$DBCALL "UPDATE core_config_data SET value='[email protected]' WHERE path='trans_email/ident_custom1/email'"
$DBCALL "UPDATE core_config_data SET value='[email protected]' WHERE path='trans_email/ident_custom2/email'"
# set base urls
if [[ -z "$RESET_BASE_URLS" ]]; then
echo " Do you want to reset base urls (Y/n)?"; read RESET_BASE_URLS
fi
if [[ "$RESET_BASE_URLS" == "y" || "$RESET_BASE_URLS" == "Y" || -z "$RESET_BASE_URLS" ]]; then
RESET_BASE_URLS="y"
if [[ -z "$SPECIFIC_BASE_URLS" ]]; then
echo " Do you want to specify base urls explicitly (Y/n)?"; read SPECIFIC_BASE_URLS
fi
if [[ "$SPECIFIC_BASE_URLS" == "y" || "$SPECIFIC_BASE_URLS" == "Y" || -z "$SPECIFIC_BASE_URLS" ]]; then
SPECIFIC_BASE_URLS="y"
if [[ -z "$SCOPES" ]]; then
SCOPES=()
SCOPE_IDS=()
BASE_URLS=()
SCOPE_ID="(to be specified)"
while [[ "$SCOPE_ID" != "" ]]; do
echo "Enter scope id [Hit enter to finish]: "
read SCOPE_ID
if [[ "$SCOPE_ID" != "" ]]; then
echo "Enter scope [stores]: "
read SCOPE
if [[ "$SCOPE" == "" ]]; then
SCOPE="stores"
fi
echo "Enter base url: "
read BASE_URL
SCOPES=("${SCOPES[@]}" $SCOPE)
SCOPE_IDS=("${SCOPE_IDS[@]}" $SCOPE_ID)
BASE_URLS=("${BASE_URLS[@]}" $BASE_URL)
fi
done
else
echo "using preconfigured scope base urls"
fi
for i in "${!SCOPES[@]}"; do
SCOPE=${SCOPES[$i]}
SCOPE_ID=${SCOPE_IDS[$i]}
BASE_URL=${BASE_URLS[$i]}
$DBCALL "UPDATE core_config_data SET value='$BASE_URL' WHERE path IN ('web/unsecure/base_url', 'web/secure/base_url') AND SCOPE_ID=$SCOPE_ID AND SCOPE='$SCOPE'"
$DBCALL "UPDATE core_config_data SET value='${BASE_URL}media/' WHERE path IN ('web/unsecure/base_media_url', 'web/secure/base_media_url') AND SCOPE_ID=$SCOPE_ID AND SCOPE='$SCOPE'"
COOKIE_DOMAIN=`echo $BASE_URL|sed -r 's/https?:\/\/([^:\/]*)[\/:].*/\1/g'`
$DBCALL "UPDATE core_config_data SET value='$COOKIE_DOMAIN' WHERE path = 'web/cookie/cookie_domain' AND SCOPE_ID=$SCOPE_ID AND SCOPE='$SCOPE'"
done
else
SPECIFIC_BASE_URLS="n"
$DBCALL "UPDATE core_config_data SET value='{{base_url}}' WHERE path='web/unsecure/base_url'"
$DBCALL "UPDATE core_config_data SET value='{{base_url}}' WHERE path='web/secure/base_url'"
fi
else
RESET_BASE_URLS="n"
fi
# increase increment ids
## generate random number from 10 to 100
function genRandomChar() {
factor=$RANDOM;
min=65
max=90
let "factor %= $max-$min"
let "factor += $min";
printf \\$(printf '%03o' $(($factor)))
}
PREFIX="`genRandomChar``genRandomChar``genRandomChar`"
$DBCALL "UPDATE eav_entity_store SET increment_last_id=NULL, increment_prefix=CONCAT(store_id, '-', '$PREFIX', '-')"
# set test mode everywhere
$DBCALL "UPDATE core_config_data SET value='test' WHERE value LIKE 'live'"
$DBCALL "UPDATE core_config_data SET value='test' WHERE value LIKE 'prod'"
$DBCALL "UPDATE core_config_data SET value=1 WHERE path LIKE '%/testmode'"
# handle PAYONE config
PAYONE_TABLES=`$DBCALL "SHOW TABLES LIKE 'payone_config_payment_method'"`
if [ ! -z "$PAYONE_TABLES" ]; then
echo " * Mod PAYONE Config."
$DBCALL "UPDATE payone_config_payment_method SET mode='test' WHERE mode='live'"
if [[ -z "$PAYONE_MID" && -z "$PAYONE_PORTALID" && -z "$PAYONE_AID" && -z "$PAYONE_KEY" ]]; then
echo -e "\E[1;31mCaution: \E[0mYou probably need to change portal IDs and keys for your staging/dev PAYONE payment methods!"
echo "Please enter your testing/staging/dev merchant ID: "
read PAYONE_MID
echo "Please enter your testing/staging/dev portal ID: "
read PAYONE_PORTALID
echo "Please enter your testing/staging/dev sub account ID: "
read PAYONE_AID
echo "Please enter your testing/staging/dev security key: "
read PAYONE_KEY
fi
$DBCALL "UPDATE core_config_data SET value='$PAYONE_MID' WHERE path='payone_general/global/mid'"
$DBCALL "UPDATE core_config_data SET value='$PAYONE_PORTALID' WHERE path='payone_general/global/portalid'"
$DBCALL "UPDATE core_config_data SET value='$PAYONE_AID' WHERE path='payone_general/global/aid'"
$DBCALL "UPDATE core_config_data SET value='$PAYONE_KEY' WHERE path='payone_general/global/key'"
$DBCALL "UPDATE payone_config_payment_method SET mid='$PAYONE_MID' WHERE mid IS NOT NULL"
$DBCALL "UPDATE payone_config_payment_method SET portalid='$PAYONE_PORTALID' WHERE portalid IS NOT NULL"
$DBCALL "UPDATE payone_config_payment_method SET aid='$PAYONE_AID' WHERE aid IS NOT NULL"
$DBCALL "UPDATE payone_config_payment_method SET \`key\`='$PAYONE_KEY' WHERE \`key\` IS NOT NULL"
fi
echo "Done."
if [[ ! -f $CONFIG ]]; then
echo "Do you want to create an anonymizer configuration file based on your answers (Y/n)?"; read CREATE
if [[ "$CREATE" == "y" || "$CREATE" == "Y" || -z "$CREATE" ]]; then
echo "DEV_IDENTIFIERS=$DEV_IDENTIFIERS">>$CONFIG
echo "RESET_ADMIN_PASSWORDS=$RESET_ADMIN_PASSWORDS">>$CONFIG
echo "RESET_API_PASSWORDS=$RESET_API_PASSWORDS">>$CONFIG
echo "KEEP_EMAIL=$KEEP_EMAIL">>$CONFIG
echo "ANONYMIZE=$ANONYMIZE">>$CONFIG
echo "TRUNCATE_LOGS=$TRUNCATE_LOGS">>$CONFIG
echo "DEMO_NOTICE=$DEMO_NOTICE">>$CONFIG
if [ ! -z "$PAYONE_TABLES" ]; then
echo "PAYONE_MID=$PAYONE_MID">>$CONFIG
echo "PAYONE_PORTALID=$PAYONE_PORTALID">>$CONFIG
echo "PAYONE_AID=$PAYONE_AID">>$CONFIG
echo "PAYONE_KEY=$PAYONE_KEY">>$CONFIG
fi
echo "RESET_BASE_URLS=$RESET_BASE_URLS">>$CONFIG
echo "SPECIFIC_BASE_URLS=$SPECIFIC_BASE_URLS">>$CONFIG
if [[ ! -z $SCOPES ]]; then
echo "SCOPES=(${SCOPES[@]})">>$CONFIG
echo "SCOPE_IDS=(${SCOPE_IDS[@]})">>$CONFIG
echo "BASE_URLS=(${BASE_URLS[@]})">>$CONFIG
fi
fi
fi