Tag Archive for unix

Create Symbolic Link on Unix / Linux

ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]

source

from_unixtime sql

	public function listAllMyPublishedArticles($uid) {
$query="SELECT article_tbl.aid,header,introduction,body,published_state, FROM_UNIXTIME(published_date, '%d/%m/%y %H:%i') as published_date FROM article_tbl, user_has_art_tbl WHERE user_has_art_tbl.uid= $uid AND user_has_art_tbl.aid = article_tbl.aid AND article_tbl.published_state = 1 ORDER BY aid desc";
$query=mysql_query($query)or die(mysql_error());
return $query;
}

source

Recursively compress a directory with zip in Unix

cd directory_to_compress
zip -r ../directory_to_compress.zip *

source

Shell script to make lists of certain file types in a web site file system

mkdir file_lists
find . -name '*.pdf' -o -name '*.PDF' > ./file_lists/all_pdf_files.txt
find . -name '*.pdf' -o -name '*.PDF' -mtime +365 > ./file_lists/pdf_files_mtime_gt_1yr.txt

find . -name '*.jpg' -o -name '*.JPG' -o -name '*.gif' -o -name '*.GIF' -o -name '*.png' -o -name '*.PNG' > ./file_lists/all_image_files.txt

find . -name '*.log' -o -name '*.LOG' > ./file_lists/all_log_files.txt

find . -name '*.css' -o -name '*.CSS' -o -name '*.js' -o -name '*.JS' > ./file_lists/all_design_assets_files.txt

find . -name '*.ram' -o -name '*.RAM' -o -name '*.aif' -o -name '*.AIF' -o -name '*.mp3' -o -name '*.MP3' > ./file_lists/all_audio_files.txt
find . -name '*.ram' -o -name '*.RAM' -o -name '*.aif' -o -name '*.AIF' -o -name '*.mp3' -o -name '*.MP3' -mtime +365 > ./file_lists/audio_files_mtime_gt_1yr.txt

find . -name '*.html' -o -name '*.HTML' -o -name '*.htm' -o -name '*.HTM' > ./file_lists/all_html_files.txt
find . -name '*.html' -o -name '*.HTML' -o -name '*.htm' -o -name '*.HTM' -mtime +365 > ./file_lists/html_files_mtime_gt_1yr.txt

source

Find all files and directories in project excluding .svn

find . -type d ! ( -name '.svn' -prune ) -print # find exclusive to directories within current directory
find . ! ( -name '.svn' -prune ) -print # list all items (files and dirs) in current directory

source

Bootstrap a Django setup

#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example/
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /path/to/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/example/media
#        |-- static
#        |-- trac
#        |-- uploads

if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=15`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/projects

bootstrap_user () {
echo "rand user pass: $USER_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin
}

create_database () {
# Create database
NAME=$PROJECT
USER=$USER
echo "$USER db pass: $DB_PASS"
echo "enter mysql root pass"
mysql -u root -p  << EOF
create database $NAME default character set = 'utf8' default collate = 'utf8_general_ci';
GRANT ALL ON ${NAME}.* TO '${NAME}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF
}

# Setup Trac
setup_trac() {
mkdir -p $HOME_DIR/sites/$SITE/trac/env
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <jonas@edgewall.com>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at <a href="http://trac.edgewall.org/wiki/TracLicense." >http://trac.edgewall.org/wiki/TracLicense.</a>
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at <a href="http://trac.edgewall.org/log/." >http://trac.edgewall.org/log/.</a>
#
# Author: Jonas Borgström <jonas@edgewall.com>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
from trac.web import fcgi_frontend
fcgi_frontend.run()
except SystemExit:
raise
except Exception, e:
print 'Content-Type: text/plain

',
print 'Oops...'
print
print 'Trac detected an internal error:'
print
print e
print
import traceback
import StringIO
tb = StringIO.StringIO()
traceback.print_exc(file=tb)
print tb.getvalue()
EOF
}

create_wsgi () {
# Copy .htaccess and dispatch.wsgi into public dir
# This also depends on the proper configuration in apache.
cat >>$HOME_DIR/sites/$SITE/public/dispatch.wsgi <<EOF
#!/usr/bin/python2.5
import sys, os

# make stdout go to stderr for wsgi apps
sys.stdout = sys.stderr

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/projects" % (USERNAME,SITE) )
os.environ['PYTHON_EGG_CACHE'] = '/home/%s/sites/%s/.python-eggs'
# Import our django handler
import django.core.handlers.wsgi

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT
application = django.core.handlers.wsgi.WSGIHandler()
EOF
}

create_fcgi () {
# Add .htaccess only under fcgi
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF

cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/apps/%s" % (USERNAME,SITE,PROJECT) )
sys.path.insert(0, "/home/%s/lib/python" % (USERNAME,) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) )

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
EOF

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi
}

setup_django () {
# Build django project
cd $LIB_DIR &&
/usr/bin/django-admin.py startproject $PROJECT &&
cd $PROJECT &&
mkdir media &&
mkdir templates

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .
create_wsgi
#create_fcgi

# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
from local_settings import *
except ImportError, exp:
pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
os.path.join (os.getcwd(), 'templates'),
'$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.cache.CacheMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.markup',
'template_utils',
'django.contrib.flatpages',
)
ADMIN_MEDIA_PREFIX = '/admin_media/'

EOF

echo "local_settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

# Hardening app
cd $HOME_DIR &&
find . -name *.py -type f -exec chmod -w {} ;
find . -type d -exec chmod -w {} ;

}

cleanup_perms () {
# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "chmod +w $HOME_DIR/sites/$SITE/logs"
chmod +w $HOME_DIR/sites/$SITE/logs
}

bootstrap_user
create_database
setup_django
#setup_trac
cleanup_perms

source

Remove .SVN files from a directory structure

find . -name ".svn" -exec rm -rf {} ;

source

Editing files from a find result

vim `find . -name *.html -exec grep -l search_str {} ;`

source

Monitor process id with dtruss on Leopard

sudo dtruss -fp 4149

source

Drop Privileges

# Get the UID the application was started with
# which must have been 'root' if you want this process
# to have 'root' like abilities.
privUID = os.geteuid()
# Get the UID that the application should use for
# most of its processing, 'nobody' is usually
# a good choice.
normalUID = pwd.getpwnam( 'nobody' )[2]

def runAsNormal():
"""Switch this process to normal privileges, we shouldn't be able to
do anything naughty in this mode."""
os.seteuid( normalUID )
def runAsPrivileged():
"""Switch to super user privileges, here we can do anything we want."""
os.seteuid( privUID )

# Once program has initialized, drop privileges
runAsNormal()

# ... some normal application code...

# Do do something that requires super user privileges
try:
runAsPrivileged()
# ... do the stuff we need to be super for
finally:
# Switch out of super mode and back to normal
runAsNormal()

source