Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sunday, October 7, 2012

Sync your development environment with a remote one

While the developing process you need sometimes to sync you local development database with a remote one (i.e. production).

Below there is a script allowing to recreate the development database and fill in with data from the remote server.

MySQL
#!/bin/sh

DBUSER_REMOTE="root"
DBPASSWORD_REMOTE="root"
DBUSER_LOCAL="root"
DBPASSWORD_LOCAL="root"
DBNAME_REMOTE=""
DBNAME_LOCAL=""
TMPFILE="$(mktemp db.XXXXXXX)"
HOST="www.example.com"

ssh $HOST "mysqldump -u$DBUSER_REMOTE -p$DBPASSWORD_REMOTE $DBNAME_REMOTE | gzip" | gunzip > $TMPFILE

mysqladmin -u$DBUSER_LOCAL -p$DBPASSWORD_LOCAL -f drop $DBNAME_LOCAL
mysqladmin -u$DBUSER_LOCAL -p$DBPASSWORD_LOCAL create $DBNAME_LOCAL
mysql -u$DBUSER_LOCAL -p$DBPASSWORD_LOCAL $DBNAME_LOCAL < $TMPFILE
rm $TMPFILE

PostgreSQL
#!/bin/sh

DBUSER_REMOTE="postgres"
DBUSER_LOCAL="postgres"
DBNAME_REMOTE=""
DBNAME_LOCAL=""
TMPFILE="$(mktemp db.XXXXXXX)"
HOST="www.example.com"

ssh $HOST "pg_dump -U $DBUSER_REMOTE -Fc $DBNAME_REMOTE" > $TMPFILE

dropdb -U $DBUSER_LOCAL $DBNAME_LOCAL
createdb -U $DBUSER_LOCAL $DBNAME_LOCAL
pg_restore -U $DBUSER_LOCAL -Fc -d $DBNAME_LOCAL $TMPFILE
rm $TMPFILE

Sync media files
$HOST="www.example.com"
$PATH="/absolute/path/to/media"
$LOCAL_DIR="."
rsync -rltDvH $HOST:$PATH $LOCAL_DIR

Tuesday, February 7, 2012

Trap Ctrl C in Bash

#!/bin/bash

# call trapped in case Ctrl+C pressed
trap trapped INT

function trapped() {
    echo "CTRL-C pressed"
}

while true; do
    sleep 1
    echo -n "."
done

Wednesday, January 4, 2012

Tab-completion for django-admin.py and manage.py

Download django_bash_completion file and save in some convenient place in your system. In .bash_profile (or .bashrc if it doesn't envoke .bash_profile) add
. ~/path/to/django_bash_completion
Settings will take effect the next time you log in.

Monday, August 15, 2011

Avoid command repeating in Bash

If you are already done with mess of the same commands in bash history, just put that in your ~/.bashrc
export HISTCONTROL=ignoredups:erasedups
ignoredups prevents the history from duplicates
erasedups if a command is already in the list it will be removed at the old position and put in the beginning of the history list

Monday, April 11, 2011

How to satisfy dependencies for your python projects

You need a text file in your project with your dependencies, i.e. REQUIREMENTS, with a list of packages your project depends on.

PIP's requirement file format (detailed):
<package-name>[==<version>]
-e <repository address>#egg=<desirable-package-name>

And when you need to init the project in a new place you'd like to run:
# init or activate a virtual environment
(virtual-env)$ pip install -r REQUIREMENTS

That's all. You packages will be automatically installed. And you can run that command whenever you want (i.e. a new dependency has been added) - it just recheck all mentioned packages and install new ones.

Thursday, April 7, 2011

Python's virtual environment

You probably would like to use separate sets of packages for your projects and whilst not to spoil your system's environment. This problem can be solved by using virtual environments.

Make initial setup:


To configure virtualenvwrapper:

  1. Create a directory for the virtual environments:
    mkdir ~/projects/.envs
  2. Add to ~/.bashrc:
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    export WORKON_HOME=~/projects/.envs
    export PROJECT_HOME=~/projects
    
    source /usr/local/bin/virtualenvwrapper.sh
  3. Run:
    source ~/.bashrc

Initial setup has been done. Now let's create a new virtual enviroment for Test project in ~/projects/test
mkvirtualenv test
workon test
Now you're in the 'test' environment. To install new packages you can use, for instance, pip.

Friday, April 1, 2011

Big brother is watching you

Do you doubt whether Google knows about your Wi-Fi router location? Try this on your *nix-machine:

iwlist wlan0 scan | sed -n 's/.* Address: //p;T;s/ //g;q' | sed 's/.*/{version:1.1.0,host:maps.google.com,request_address:true,address_language:'${LANG/.*/}',wifi_towers:[{mac_address:"&",signal_strength:8,age:0}]}/' | curl -sX POST -d @- www.google.com/loc/json | sed -e 'h;s/.*latitude":\([^,]*\).*/\1/;G;s/\n[^\n]*longitude":\([^,]*\).*/,\1\n/;s|^|http://maps.google.com/maps?q=|;x;s/[,{]/\n/g;s/["}]//g;s/:/\t/g;s/\n//;G'

Nice, isn't it?

Tuesday, February 15, 2011

Launch Virualbox image from command line

To launch a VirtualBox image directly without GUI interface do the following

vboxmanage startvm name

where name is exactly name is shown in VirtualBox GUI.

Tuesday, November 17, 2009

Script to easily create a Postgres database

#! /bin/sh
if [ ${#} -ne 3 ]
then
 echo "Usage: $0 db_name username password"
 exit 2
fi

DBNAME=$1
USER=$2
PWD=$3

echo "
CREATE USER $USER WITH PASSWORD '$PWD';
CREATE DATABASE "$DBNAME"
    WITH OWNER "$USER"
    ENCODING 'UTF-8'
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
    TEMPLATE = "template0";
GRANT ALL PRIVILEGES ON DATABASE $DBNAME to $USER;
" | psql -U postgres -d template1

or if you want just to exec it once:
echo "
CREATE USER $USER WITH PASSWORD '$PWD';
CREATE DATABASE "$DBNAME"
    WITH OWNER "$USER"
    ENCODING 'UTF-8'
    LC_COLLATE = 'en_US.UTF-8'
    LC_CTYPE = 'en_US.UTF-8'
TEMPLATE = "template0"; GRANT ALL PRIVILEGES ON DATABASE $DBNAME to $USER; " | sudo -u postgres psql -U postgres -d template1