Tutorials in ‘Development’ Category

How to backup and restore your MySQL database or table structure to in Linux

Whenever there is a database involved, an important task that should never be neglected is backing it up. In the case of MySQL on a Linux machine, backing up consists of executing a single command.
Backing up a MySQL database can be done with one simple command:

mysqldump -h <host> -u <user> -p<password> <db_name> > backup.sql

For example if you want to backup a local database called db1 using the user user1 with password pass1 you should write:

mysqldump -h localhost -u user1 -ppass1 db1 > backup.sql

The backup.sql file contains an SQL query for creating the exact same database. Restoring such a dump-file is just a matter of one command too:

mysql -h <host> -u <user> -p<password> <db_name> < backup.sql

Following the naming from our previous example will result in the following command to restore the database:

mysql -h localhost -u user1 -ppass1 db1 < backup.sql

In the perfect environment the backup should be created by a cronjob or anacronjob.
Another useful thing that one may want to backup is the table structure. For tables whose structure is relatively often changed this can prove to be very helpful. It is done by using one simple modification of the command for backing up an entire database:

mysqldump -d -h <host> -u <user> -p<password> <db_name> > backup.sql

Did you notice the difference? It is the -d option, which specifies that data should not be dumped. Using our previous examples this will be transform to:

mysqldump -d -h localhost -u user1 -ppass1 db1 > backup.sql
VN:F [1.9.18_1163]
Rating: 6.3/10 (3 votes cast)

How to force the browser to reload CSS and JavaScript files

When editing the CSS and JavaScript of a site, there is a slight chance that the user may get the old versions, which at best will cause a lot of complains and at worst will cost you a lot of lost customers. The reason for this is that browsers cache static content like CSS and JavaScript. However, telling a browser to reload them is not that hard to do. All it takes is a little trick that will force our friendly browsers into thinking that the CSS and JavaScript are dynamic content.

Before we go on, let us take a look at how we specify the CSS and JavaScript to be used within HTML. Normally we have something in the line of:

<link rel="StyleSheet" type="text/css" href="/static/style.css">
<script src="/static/js/script.js" type="text/javascript"></script>

Where style.css and script.js are our files. Naturally CSS files are specified with the href attribute of the link tag, while JavaScript files are included using the script tag. Now, let’s take a look how we can make our browser reload CSS and JavaScript every time:

<link rel="StyleSheet" type="text/css" href="/static/style.css?version=15">
<script src="/static/js/script.js?version=15" type="text/javascript"></script>

“Now what is that “version”?”, you are probably thinking. Well it’s nothing. No really, it does absolutely nothing. You can use ?favouritesite=onlinehowto.net15 if you want. It would have the absolutely same effect.

“But what does it do?”, you are probably thinking. “This looks just like passing GET variables.” Exactly!

The browser cache uses a pretty complicated mechanism, but there is one thing that you should know for sure index.html?version=14 and index.html?version=15 are two different pages for the browsers, therefore it can’t rely on its cache, therefore it must get them again. But what happens on the server side? Well those arguments are ignored by our server and it simply supplies style.css and script.js.

Whatever language, technology, etc. you are using, you always keep a header file which is included pretty much everywhere so changing the version=<number> part after each revision is pretty easy. You can even make it a part of your site’s logic.

In conclusion, this trick may save you a lot of headaches related to cached CSS and JavaScript, and if you believe you may have some issues, we greatly encourage you to use it!

VN:F [1.9.18_1163]
Rating: 10.0/10 (2 votes cast)

Creating and signing certification requests using OpenSSL

In our previous tutorial about creating your own Certificate Authority, we introduced OpenSSL – an open source commercial-grade toolkit, which implements SSL and TLS (Secure Sockets Layer and Transport Layer Security) and provides a general purpose cryptography library. Naturally we continue with creation of certification requests (also called Certificate Signing Request).

Note: In this tutorial (as well as in the previous one) we assume CentOS is the OS of choice.

To create a certificate request we navigate again to /etc/pki/CA:

cd /etc/pki/CA

Now let’s create the certification request. We’ll request a certificate, which will last one year:

openssl req -config openssl.cnf -new -nodes -keyout private/<domain>.key -out <domain>.csr -days 365

Generating a 2048 bit RSA private key
..............................+++
.................................+++
writing new private key to 'private/<domain>.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:<country>
State or Province Name (full name) []:<state>
Locality Name (eg, city) [Default City]:<city>
Organization Name (eg, company) [Default Company Ltd]:<organization>
Organizational Unit Name (eg, section) []:<department>
Common Name (eg, your name or your server's hostname) []:<url>
Email Address []:<email>
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

This creates two files, <domain>.key, which is the private key and <domain>.csr, which is the Certificate Signing Request. Restrict the rights to the private key so it readable only by root and the user that will use it:

chown root:globus private/<domain>.key

chmod 0440 private/<domain>.key

So now that we have created the certification request it is time to sign it. Navigate to /etc/pki/CA:

cd /etc/pki/CA

Sign the certificate using this command:

openssl ca -config openssl.cnf -policy policy_anything -out certs/<domain>.crt -infiles <domain>.csr

Using configuration from openssl.cnf
Enter pass phrase for ./private/ca.key:<ca_password>
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Nov 15 18:52:08 2011 GMT
            Not After : Nov 14 18:52:08 2012 GMT
… CERTIFICATE INFORMATION …
Certificate is to be certified until Nov 14 18:52:08 2012 GMT (365 days)
Sign the certificate? [y/n]:y
 
 
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

After that, you need to remove the certificate request:

rm -f <domain>.csr

After the whole procedure two files are created. <domain>.crt – this is a domain specific certificate for the request. It is put in the certs directory. <certificate_number>.pem is the second file. It is put in the newcerts folder. This is a ready to use X.509 file which contains the information from <domain>.key and from <domain>.crt.

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

Create your own Certificate Authority using OpenSSL on CentOS

OpenSSL is an implementation of the SSL and TLS protocols. It is open-source and is the de-facto standard toolkit for Secure Sockets Layer (SSL) and Transport Layer Security (TLS). It is written in C and also contains a general purpose cryptography library. Being written in C allows for various wrappers for other programming languages to exist.

Installing OpenSSL on CentOS is actually quite easy. All you have to do is enter the following command:

yum install openssl

Note: Depending on your installation configurations OpenSSL may already be installed on your system.

Note: This tutorial uses OpenSSL 1.0.0.

After we install OpenSSL, we need to setup our own Certificate Authority. To do so we first navigate to /etc/pki/CA:

cd /etc/pki/CA

Now we will make a directory where our certificates will be stored:

mkdir certs

Another necessary directory is for the revocation list:

mkdir crl

Note: crl stands for Certificate Revocation List.

Now we must create a directory for storing the unencrypted certificates:

mkdir newcerts

Next, create an empty file index.txt (you may use touch). The index.txt file is the database for certificates. Additionally, create two files containing the next serial number for a certificate and the next serial number for the revocation list:

echo ’01’ > serial

echo ’01’ > crlnumber

Almost ready. Copy the standard openssl config file to you current directory:

cp /etc/pki/tls/openssl.cnf openssl.cnf

And edit the config file (the one stored at /etc/pki/CA/openssl.cnf):

Change this line:

dir             = /etc/pki/CA           # Where everything is kept

to

dir             = .           # Where everything is kept

Change this line:

certificate     = $dir/cacert.pem       # The CA certificate

to

certificate     = $dir/certs/ca.crt       # The CA certificate

And this line:

private_key     = $dir/private/cakey.pem# The private key

to

private_key     = $dir/private/ca.key # The private key

And last, but certainly not least, make /etc/pki/CA/openssl.cnf readable only for you:

chmod 0600 openssl.cnf

After that, the process of creating a certificate authority is actually quite easy. Navigate to /etc/pki/CA:

cd /etc/pki/CA

Enter the following command:

openssl req -config openssl.cnf -new -x509 -extensions v3_ca -keyout private/ca.key -out certs/ca.crt -days 3650

if you want to create a CA valid for 10 years. You will be prompted:

Generating a 2048 bit RSA private key
........+++
.......+++
writing new private key to 'private/ca.key'
Enter PEM pass phrase:<password>
Verifying - Enter PEM pass phrase:<password>
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:<country>
State or Province Name (full name) []:<state>
Locality Name (eg, city) [Default City]:<city>
Organization Name (eg, company) [Default Company Ltd]:<organization>
Organizational Unit Name (eg, section) []:<department>
Common Name (eg, your name or your server's hostname) []:<common_name>
Email Address []:<email>

Where common_name is usually formed like ca.<your_domain>, and <email> is usually ca@<your_domain>

Finally, don’t forget to restrict the access to your private key:

chmod 0400 private/ca.key

For more on OpenSSL checkout how to create and sign certificates.

VN:F [1.9.18_1163]
Rating: 10.0/10 (3 votes cast)

Function overloading by return type in C++

Commercial modern day languages often get criticized because they don’t support function overloading by return type. That is to say, functions cannot be distinguished by return type. For example:

int myfunction(int a, int b); float myfunction(int a, int b); char myfunction(int a, int b);

Are not valid. That is kind of bad, because the possibility to overload by return type often improves the quality of code.

But why can’t we overload by return type? Because the return type is not a part of a function’s signature. However, function overloading by type is not only possible in C++, it is actually pretty easy too.

Enter the cast operator. Remember this guy?

class C{ public: operator int() {…} };

“That’s cool, but it cannot be passed arguments!” you may say. True, but we don’t need it to be passed arguments, we can pass arguments using the constructor. Let’s review that with a simple example:

#include <iostream> using namespace std; class TestFunction{ private: int ia, ib; public: TestFunction(int a, int b): ia(a), ib(b){ } operator int(){ return ia * ib; } operator float(){ return (float)ia / (float)ib; } }; int main(){ int int_res = TestFunction(1, 13); //13 float float_res = TestFunction(1, 13); //0.0769231 cout << int_res << endl << float_res; return 0; } Output: 13 0.0769231

We should note, that an essential part of our program is the way we call the “function” like this:

float float_res = TestFunction(1, 13); //0.0769231

If we, for example call:

cout << TestFunction(1, 13);

we will get a compilation error, stating that the <<operator is ambiguous. Because the << operator expects a type from his right-side argument and our TestFunction expect a type from its left-side. Naturally an expression like:

cout << (float)TestFunction(1, 13);

is perfectly valid.

Of course this approach of overloading by return type, can be combined with the ordinary overloading by signature, by simply introducing a little more advanced logic in our constructor (overloading it, saving which constructor was called,  etc.).

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

Using *args and **kwargs in Python

Python provides a special syntax for function definitions to allow the usage of variable length argument lists. The syntax is the so called *args and **kwargs. Basically the argument preceded with a single asterisk (not necessarily called args) is used for passing the so called non-keyworded argument list, while the double asterisk is used to pass the keyworded argument list. But let’s show all that in a few examples, shall we?

The following code:

def var_args_function(a, b, *args):
    print('a =', a)
    print('b =', b)
    for i in args:
    print('extra arg =', i)

var_args_function(1, 2, 'arg1', -2, 'three', 'last_one')

Results in this output:

a = 1
b = 2
extra arg = arg1
extra arg = -2
extra arg = three
extra arg = last_one

In the example, our function is passed two normal (or so called positional) arguments and four more arguments, all of which are contained in the args variable. The args variable is actually a tuple, and as any tuple it can be iterated, indexed, etc.

Now what about kwargs?

def var_kwargs_function(a, **kwargs):
    print('a =', a)
    for i in kwargs:
        print('kwargs[%s] = %s' % (i, kwargs[i]))

var_kwargs_function(2, var1=1, var2='two', var3='three')

Results in:

a = 2
kwargs[var1] = 1
kwargs[var3] = three
kwargs[var2] = two

But what is kwargs? It is actually a dict and you can do with it anything you do to a normal dict (yes, including changing it, although that is considered bad practice).

A word about order of the arguments – there are two rules you must follow – non-keworded arguments before keworded and formal arguments before argument lists.

For example:

def f(a, b, c, *args, **kwargs) is OK

but

def f(*args, a, **kwargs, b, c) is NOT OK

A word about positioning of default arguments – consider these two functions:

def f(a, *args, b=17):
    print('Function f got', a, args, b)

def g(a, b=17, *args):
    print('Function g got', a, args, b)

The following calls:

f(2,3,4,5)
f(2,3,4,5,b=8)
g(2,3,4,5)
g(2,3,4,5,b=8)

Result in the following:

Function f got 2 (3, 4, 5) 17
Function f got 2 (3, 4, 5) 8
Function g got 2 (4, 5) 3
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
    g(2,3,4,5,b=8)
TypeError: g() got multiple values for keyword argument 'b'

Let’s finish with calling a function with args and kwargs:

def var_args_n_kwargs_f(a, *args, **kwargs):
    print('a =', a)
    for i in args:
        print('extra arg =', i)
    for i in kwargs:
        print('kwargs[%s] = %s' %(i, kwargs[i]))

args = (-1, -2, -3, 'onearg')
kwargs = {'key1': 'val1', 'key2': 'val2', 'last_key': 'last_val'}

var_args_n_kwargs_f(13, *args, **kwargs)

Will result in:

a = 13
extra arg = -1
extra arg = -2
extra arg = -3
extra arg = onearg
kwargs[key2] = val2
kwargs[key1] = val1
kwargs[last_key] = last_val

Note: You can pass anything that can be casted to tuple as args and

VN:F [1.9.18_1163]
Rating: 6.3/10 (3 votes cast)

Run cron jobs within Django using django-cron

Quite often web applications require cron-like behavior which more or less contradicts with the request-response model.

Not many host providers allow users to create a cron jobs, and even if it is possible, if your site relies on an external script it loses its integrity. It no longer is a website, it is a website relying on cron jobs.

Now at first glance, there is not much difference, but such things make migrating harder and/or may cause a lot of problems in the long run.

As we all know Django is an open source web application framework, designed for rapid development. It follows the so called DRY principle and therefore greatly emphasizes on “pluggability”. In this short tutorial we will show you how to “plug-in” a little but very useful Django application called django-cron, which allows you to easily get cron line behavior.

You can visit the project’s site in at http://code.google.com/p/django-cron

Installation is fairly easy:

  • Download the source
  • Place django_cron on your python path
  • Add “django_cron” in the list of installed apps in your settings.py:
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'django_cron',
)
  • In the beginning of your global urls.py file add:
import django_cron
django_cron.autodiscover()
  • Write your crons in a file named cron.py inside the application you want write crons to. Note: The application must be installed in your settings.py

A cron job is fairly easy to write.

Here is an example cron.py file which deletes old files from a fixed directory:

import os
import time
from django_cron import cronScheduler, Job

class DeleteOldFilesCron(Job):
“””A cron which deletes files older than a minute from a fixed directory”””

    run_every = 60*60*24 # run every day

    def job(self):
        path = ‘/var/www/html/media/tmp/’
        files = os.listdir(path)
        for file in files:
            if(time.time() – os.path.getmtime(path + file) > 60):
                os.remove(path + file)

cronScheduler.register(DeleteOldFilesCron)

Naturally you can have several crons in your cron.py

Note: There are some Apache specific configurations which must be taken into account. For more information visit http://code.google.com/p/django-cron/wiki/Install

VN:F [1.9.18_1163]
Rating: 3.0/10 (2 votes cast)

Working with PHP functions

Many times we need to use code to do one thing with different input values. If put this code at each place where we need this funtionality files in our PHP project will grow up with more lines of code. On this we say code mess.

For this we have to use PHP Functions. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 3.5/10 (2 votes cast)

Example of using ArrayList in Java 5

ArrayList in Java is most frequently used collection class after HashMap in Java. Java ArrayList represents an automatic resizable array and used in place of array. Since we can not modify size of an array after creating it, we prefer to use ArrayList in Java which resize itself automatically once it gets full. ArrayList in Java implements List interface and allow null. Java ArrayList also maintains insertion order of elements and allows duplicates opposite to any Set implementation which doesn”t allow duplicates. ArrayList supports both Iterator and ListIterator for iteration but it’s recommended to use ListIterator as it allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator”s current position in the list. But while using ListIterator you need to be little careful because ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous () and the element that would be returned by a call to next (). In this Java ArrayList tutorial we will see how to create Java ArrayList and perform various operations on Java ArrayList.

ArrayList has been modified in Java5 (Tiger) to support Generics which makes Java ArrayList even more powerful because of enhanced type-safety. Before Java5 since there was no generics no type checking at compile time which means there is chance of storing different type of element in an ArrayList which is meant for something and ultimately results in ClassCastException during runtime. with generics you can create Java ArrayList which accepts only type of object specified during creation time and results in compilation error if someone tries to insert any other object into ArrayList in Java; for example if you create an ArrayList of String object you can not store Integer on it because add() method of ArrayList will check Type before adding object into ArrayList in Java opposite to add() method of Java4 which accepts any object.

It’s also important to remember that ArrayList is not synchronized and should not be shared between multiple threads. If multiple threads access a Java ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (As per Java doc a structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. It’s recommended to synchronize the list at the creation time to avoid any accidental unsynchronized access to the list. Another better option is to use CopyOnWriteArrayList which is added from Java 5 and optimized for multiple concurrent read. In CopyOnWriteArrayList all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array and that”s why it is called as "CopyOnWrite"

[ View Full Tutorial ]

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

20 Points about Synchronization in Java

In this Java synchronization tutorial we will see what is meaning of Synchronization in Java, Why do we need Synchronization in java, what is java synchronized keyword, example of using java synchronized method and blocks and important points about synchronization in Java. Synchronization in Java is an important concept since Java is a multi-threaded language where multiple threads run in parallel to complete program execution. In multi-threaded environment synchronization of java object or synchronization of java class becomes extremely important. Synchronization in Java is possible by using java keyword "synchronized" and "volatile”. Concurrent access of shared objects in Java introduces to kind of errors: thread interference and memory consistency errors and to avoid these errors you need to properly synchronize your java object to allow mutual exclusive access of critical section to two threads.

This Java Synchronization tutorial is in continuation of my article 10 tips on java logging , 10 points about java heap and Top 20 core java interview questions if you haven’t read already you may find some useful information based on my experience in Java Collections.

5.java synchronized keyword is re-entrant in nature it means if a java synchronized method calls another synchronized method which requires same lock then current thread which is holding lock can enter into that method without acquiring lock.

6. Java Synchronization will throw NullPointerException if object used in java synchronized block is null e.g. synchronized (myInstance) will throws NullPointerException if myInstance is null Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)