Skip to content

Instantly share code, notes, and snippets.

View joostvanveen's full-sized avatar

Joost van Veen joostvanveen

View GitHub Profile
@joostvanveen
joostvanveen / Reindex catalogrule prices in Magento 2.sh
Created November 13, 2020 06:12
Reindex catalogrule prices in Magento 2
## Reindex catalogrule prices in Magento 2
## Navigate to Magento root, e.g.:
cd ~/magento2
## Reindex the catalogrule prices
php bin/magento indexer:reindex catalogrule_product
## For this indexer to correctely index the new productprices, the
## catalogrules need to be indexed first. This usually happens
@joostvanveen
joostvanveen / Magento 2 chcek for orders with more than one shipment or invoice.sql
Created November 13, 2020 05:59
Magento 2 chcek for orders with more than one shipment or invoice
-- Orders with more than one shipment
SELECT orders.increment_id AS ordernummer, orders.created_at, count(*) as `Aantal verzendingen` FROM sales_shipment as shipments
LEFT JOIN sales_order AS orders ON orders.entity_id = shipments.order_id
GROUP BY shipments.order_id
HAVING `Aantal verzendingen` > 1
ORDER BY orders.created_at;
-- Orders with more than 1 invoice
SELECT orders.increment_id AS ordernummer, orders.created_at, count(*) as `Aantal facturen` FROM sales_invoice as invoices
LEFT JOIN sales_order AS orders ON orders.entity_id = invoices.order_id
@joostvanveen
joostvanveen / search_for_malicious_php_code.sh
Created October 26, 2020 12:17
Search directory for PHP files containing malicious code
## eval() is used to executed shell commands through PHP
grep -ri "eval(" $PWD --include *.php
## exec() is used to executed shell commands through PHP
grep -ri "exec(" $PWD --include *.php
## Malicious code is often obfuscated through base64 encoding
grep -ri "base64" $PWD --include *.php
## Malicious code is often obfuscated through gzip
@joostvanveen
joostvanveen / check_prefixes_and_autoincrements_for_magento_2_orders.sql
Created October 13, 2020 08:00
Find incorrect prefixes for order and invoice settings in Magebto 2. Sometimes, after using the data migration tool, Magento uses the wrong prefixes. This usually becomes unnoticed at first and can quickly become a disaster, leading to duplicate order numbers. Magento uses tables to work out which prefixes to use on which store, and also to work…
-- Get all stores where orders, invoices, etc use the wrong prefix
-- E.g. the '2' in order number '200002345'
SELECT store_id, prefix, entity_type, sequence_table FROM sales_sequence_meta
JOIN sales_sequence_profile ON sales_sequence_profile.meta_id = sales_sequence_meta.meta_id
WHERE prefix <> store_id
ORDER BY store_id;
-- Get all stores that use the wrong prefix autoincrement table for orders, invoices, etc.
-- E.g. the '2345' in order number '200002345'
@joostvanveen
joostvanveen / mysql_alter_charset_and_collation_to_utf8.sql
Created May 29, 2019 05:20
Alter the charset and collation to UTF8 for a database and a table. Also inspects the default charset and collation for the entire database.
-- Inspect the default charset and collation for your database
SELECT default_character_set_name FROM information_schema.schemata WHERE schema_name = 'MYDATABASE_NAME';
-- Alter the default charset and collation for your database
-- CAREFUL: when you change the charset for your database, make sure the data in your tables matches that character set.
-- If it is not, follow these steps: http://www.alphadevx.com/a/420-Converting-a-MySQL-database-from-latin1-to-utf8
ALTER DATABASE MYDATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci;
-- Alter the default charset and collation for your database
ALTER TABLE MYTABLE_NAME CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
@joostvanveen
joostvanveen / top-20-user-agents-and-ips-from-directadmin-logs.sh
Created February 17, 2019 21:25
Loop though all rotated access logs and get the top 20 list of User AgentsThis can be used to identify bots
# Loop though all rotated Directadmin access logs and get the top 20 list of User Agents
# This can be used to identify bots
zcat /home/USER/domains/DOMAIN/logs/LOGNAME.tar.gz* | awk -F\" '{print $6}' | sort | uniq -c | sort -nr | head -20
# Get top 20 IP addresses
zcat /home/USER/domains/DOMAIN/logs/Feb-2019.tar.gz | awk -F\" '{print $1}' | sort | uniq -c | sort -nr | head -20
# Get top 20 most visited URLs
zcat /home/USER/domains/DOMAIN/logs/Feb-2019.tar.gz | awk -F\" '{print $4}' | sort | uniq -c | sort -nr | head -20
@joostvanveen
joostvanveen / delete_files_continaing_a_string.sh
Last active July 27, 2020 17:53
Delete log files containing a certain string. Handy to clean up Magento reports after committing a fix.
# Delete log files containing a certain string.
# Handy to clean up Magento reports after committing a fix.
find var/report/* -exec grep -q 'A non-numeric value encountered' '{}' \; -delete
# If you ant to chcek first:
find var/report/* -exec grep -q 'A non-numeric value encountered' '{}' \; -find
@joostvanveen
joostvanveen / clean_magento_product_images.sh
Created April 25, 2018 11:20
Clean old product images cache and orphaned product images for Magento 1
# Delete all cached images older than 365 days
find ~/public/media/catalog/product/cache/* -type f -atime +365 -exec rm {} \;
# Remove product images that are not present in the database
magerun media:images:removeorphans
@joostvanveen
joostvanveen / count_visitors_by_ip.sh
Last active March 22, 2018 12:21
Display the top X visitors by IP address from a single or all nginx access log files
# Display the top 20 visitors by IP address from a single log file
awk '{print $2}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -20
# Display the top 40 visitors by IP address from all rotated gzipped log files
zcat /var/log/nginx/access.log* | awk '{print $2}' | sort | uniq -c | sort -nr | head -40
# Find IP adrdresses for all users that created a custoter account for Magento
zcat /var/log/nginx/access.log* | awk -v OFS='\t' '/\/customer\/account\/createpost?/ {print $2, $21}' | sort | uniq -c | sort -nr | head -40
@joostvanveen
joostvanveen / csplit.sh
Created March 9, 2018 14:02
Split large SQL files into separate files for each table
## Split large SQL files into separate files for each table if every tabel starts with a 'DROP TABLE IF EXISTS' statement
csplit -k $PWD/filename.sql '/^DROP TABLE IF EXISTS .*/' '{900}'
## Split large SQL files into separate files for each table if every tabel starts with a 'CREATE TABLE' statement
csplit -k $PWD/filename.sql '/^CREATE TABLE .*/' '{900}'