Skip to content

Instantly share code, notes, and snippets.

View miceno's full-sized avatar

Orestes Sanchez miceno

  • Barcelona, Spain
View GitHub Profile
@miceno
miceno / update_json.sql
Created February 12, 2018 23:34
Massively update in place json in PostgreSQL
UPDATE sns_log_testing AS u
SET message = jsonb_set(t.message, '{type}', JSONB '"DEVICE_EVENT"')
FROM (SELECT *
FROM sns_log_testing AS s
WHERE s.message ->> 'type' = 'CLIENT_EVENT'
AND s.message ->> 'event_type' = 'NOTIF_REQUEST'
LIMIT 10000) AS t
WHERE t.id = u.id
@miceno
miceno / export-import-csv.sql
Created February 2, 2018 16:27
Export and import CSV in postgres
-- On the server.
-- \copy is run on the client side, so if you run in your local machine to connect to the remote
-- server, you will transfer the results probably in plain. So better run it on local.
\copy (Select * From sns_log where received > '2018-01-01') To '/tmp/log-20180101.csv' With CSV delimiter ','
-- On the client side.
\COPY sns_log FROM 'log-20180101.csv' WITH delimiter as ',' csv
@miceno
miceno / sniff-header.sh
Created January 5, 2018 17:24
Sniff HTTP headers directly with tcpdump
#!/usr/bin/env bash
INTERFACE=eno16777984
PORT=80
tcpdump -i $INTERFACE -A -s 10240 'tcp port '$PORT' and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' \
| egrep --line-buffered "^........(GET |HTTP\/|POST |HEAD )|^[A-Za-z0-9-]+: " \
| sed -r 's/^........(GET |HTTP\/|POST |HEAD )/\n\1/g'
@miceno
miceno / remove-indexes.sh
Created January 4, 2018 17:21
Bulk remove elasticsearch indexes
#!/usr/bin/env bash
SERVER="http://elasticsearch.server.example.com:9200"
curl "$SERVER/_cat/indices?v" | awk '{ print $3}' | sort > /tmp/indexes.txt
while read -u 3 f;
do
echo $f
COMMAND="curl -XDELETE $SERVER/$f"
@miceno
miceno / count-index.sh
Last active December 28, 2017 01:00
Number of documents of an elasticsearch index
# elasticsearch url
URL=https://smartnotifications-9383428543.eu-central-1.bonsaisearch.net
curl -s $URL/logstash-nginx-*/_count | sed -e 's/^.*"count":\([^,]*\),.*$/\1/'
@miceno
miceno / json-text.sh
Created December 27, 2017 12:33
Extract json text value
echo '{"count":"7363","_shards":{"total":1,"successful":1,"failed":0}}' | sed -e 's/^.*"count":"\([^"]*\)".*$/\1/'
@miceno
miceno / json-number.sh
Created December 27, 2017 12:30
Extract number value from json
echo '{"count":7363,"_shards":{"total":1,"successful":1,"failed":0}}' | sed -e 's/^.*"count":\([^,]*\),.*$/\1/'
@miceno
miceno / rsync.sh
Created October 29, 2017 17:56
Rsync remote
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress SOURCE HOST:TARGET
@miceno
miceno / .gitignore
Last active March 22, 2019 20:16
Postgresql scripts
.idea
@miceno
miceno / crontab.sh
Created December 26, 2016 01:29
Crontab debugging
Add
* * * * * env > ~/cronenv
to your crontab, let it run once, turn it back off, then run
env - `cat ~/cronenv` /bin/sh
And you are now inside a sh session which has cron's environment