Skip to content

Instantly share code, notes, and snippets.

@antonpirker
antonpirker / flush_memcached_cache.sh
Created January 6, 2012 10:38
Flush memcached cache (if your netcat executable (nc or netcat) cannot be found in /usr/bin/nc change the path)
echo "flush_all" | /bin/netcat 127.0.0.1 11211
@antonpirker
antonpirker / start_smtpd.sh
Created January 6, 2012 10:39
Start smtp server for debugging
python -m smtpd -n -c DebuggingServer localhost:1025
@antonpirker
antonpirker / gist:1632084
Created January 18, 2012 09:06
Create PostgreSQL database and user
# switch to user postgres
sudo su - postgres
# create user (no superuser, no create db, ...)
createuser -S -D -R -E -P username
# create utf8 encoded db owned by username
createdb -E utf8 -O username dbname
# edit /etc/postgresql/9.1/main/pg_hba.conf and add the following line (so your user is allowed to connect from localhost):
@antonpirker
antonpirker / gist:1632135
Created January 18, 2012 09:26
Create geospatial database in PostgreSQL
# create an empty template database
createdb -U postgres template_postgis
# add the postgis sql to our template
psql -d template_postgis -f /usr/share/postgresql/9.1/contrib/postgis-1.5/postgis.sql
# add pgsql suport to template
createlang plpgsql template_postgis
# add a geospatial reference system to the template
@antonpirker
antonpirker / resize_images
Created February 23, 2012 09:01
Resize Images to 1920x1080
find . -maxdepth 1 -iname '*jpg' -exec convert -resize 1920x1080^ -gravity center -extent 1920x1080 {} out/{} \;
@antonpirker
antonpirker / gist:1934631
Created February 28, 2012 19:40
Copy all files of all sub directories to a specific output directory
# find all files in all subdirectories and copy them to ../out_directory/
find . -name "*" -type f -printf "processing %p\n" -exec cp '{}' ../out_directory/ \;
@antonpirker
antonpirker / gist:5394384
Last active December 16, 2015 06:49
Show (estimated) number of rows and size of all Tables in PostgreSQL Database and store it to a text file.
\copy (select relname,
CAST(reltuples as INT) tuples,
pg_total_relation_size(CAST(relname as TEXT)) size,
pg_size_pretty(pg_total_relation_size(CAST(relname as TEXT))) pretty_size
from pg_class
where relnamespace = 2200 and
relname not like '%_id_seq' and
relname not like '%_pkey' and
relname not like '%_key' and
relname not like '%_id'
@antonpirker
antonpirker / gist:5467440
Last active December 16, 2015 16:59
Show number of rows and size of all tables in MySQL Database.
SELECT
table_name AS "Table",
table_rows as "Tuples",
round(data_length/1024/1204,2) as "Data in MB",
round(index_length/1024/1024,2) as "Index in MB",
round(((data_length + index_length) / 1024 / 1024), 2) "Combined in MB"
FROM information_schema.TABLES
WHERE table_schema = "[db_name]";
@antonpirker
antonpirker / show_postgres_locks.sql
Last active December 17, 2015 06:18
Shows current exclusive locks in Postgres Database
select
l.mode as "lock",
substring(sa.current_query from 0 for 50) as "query",
c.relname as "relation",
l.database,
sa.query_start as "start",
age(now(), sa.query_start) as "age",
sa.procpid as "pid"
from
pg_stat_activity sa,
@antonpirker
antonpirker / user_from_session_key.py
Last active November 13, 2022 16:24
Get user object from the given Django session key
def user_from_session_key(session_key):
"""
Returns the User related to the given Django session key.
"""
from django.conf import settings
from django.contrib.auth import SESSION_KEY, BACKEND_SESSION_KEY, load_backend
from django.contrib.auth.models import AnonymousUser
session_engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
session_wrapper = session_engine.SessionStore(session_key)