Thursday, October 23, 2008

mysql dump and restore a single table.

Dumping or backing up a table:

mysqldump -u username -ppassword --default-character-set = latin1 --single-transaction database --tables table > /location/table_bk.sql


Restoring a backed up sql.

mysql -u username -ppassword database < /location/table_bk.sql

Wednesday, October 8, 2008

Mysql Backup a single table from the database

and to restore a specific table from the database:

mysqldump -u readonly -preadonly --single-transaction db_name --tables table_name > dir/sql/name

This really worked well for me and will do for you as well...:~))

Backup and restore mysql data....Example.

The example I was taught goes simply like this:

mysql>mysqldump -u root -p DATABASENAME > DATABASENAME.sql

If you wanted to test it, you could drop the database:

mysql>DROP DATABASE DATABASENAME;
(DATABASE referring to DATABASENAME, the one you just backed up)

The quit mysql:

mysql>q

Log back in:

]$ mysql -u root -p
mysql>CREATE DATABASE PREVIOUS_DATABASENAME;
mysql> \q
Bye
]$ mysql -u root -p PREVIOUS_DATABASENAME < DATABASENAME.sql
Enter password:


And it will be restored!


How to Create a Cron Job (Scheduled Task) for Your Website or Blog

by Christopher Heng, thesitewizard.com

On occasion, you might come across the need to create a scheduled task for your site. For example, you may have installed a website statistics software such as Awstats or a content management system such as Drupal that requires a background program to run at a certain time. In such a case, the software's documentation often asks you to schedule a cron job on your web server. This article provides a step-by-step tutorial on how you can schedule such a task using a program named crontab.

System Requirements

  1. An Appropriate Operating System

    Cron jobs, created using the command line program called crontab, require that your website be hosted on a Unix-type web server, such as Linux or one of the BSDs. Generally, if the web server that your site is on is running Windows, you cannot use this tutorial. Note that I am referring to the computer which your web host uses to place your site, not your own computer.

  2. Shell Access or Control Panels Interface to Crontab

    You also need to be able to connect to your web host using telnet or SSH. Alternatively, your web host must provide you a way to set the crontab tasks using their control panel.

Overview

Basically, we will start by creating a schedule. Then, we will feed this schedule to a program called crontab. Crontab will take the schedule and install it into an internal set of tables which it manages. At the appropriate time demanded by your schedule, another program called cron will execute the tasks you have set.

Steps to Setting Up a Cronjob on Your Web Server

  1. Figure out the schedule

    The first thing you need to do is to figure out a schedule for your task. Do you want it run once a day? Hourly? Note that if you use a shared web server, where many websites reside on the same web servers, you should not run your cronjob too frequently, or you will affect the performance of both your website and the other sites hosted on the same computer (and get you booted off the web host as well). If you really need such a high frequency, you may need to get a virtual private server or even a dedicated server where you have the machine to yourself.

  2. Figure out how to write the schedule for crontab

    The next thing you need to do is to write your schedule in a way so that crontab will understand it. The crontab format is somewhat arcane and cryptic, so we will take this step by step.

    The basic format of a crontab schedule consists of 6 fields, separated by spaces, formatted as follows:

    minute hour day month day-of-week command-line-to-execute

    The acceptable values for each of the 6 fields are:

    FieldRange of values
    minute0-59
    hour0-23
    day1-31
    month1-12
    day-of-week0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
    command-line-to-executethe command to run along with the parameters to that command if any

    The fields have to be in that exact order, with no empty or missing fields

  3. Now if you want a job to run every hour on the hour, you will have to set the time component of the crontab line as follows:

    0 * * * *

    Can you see why? The "0" means at the top of the hour, that is, when the minute readout on a digital clock shows "00". The asterisk in the hour field means every single hour. In other words, every hour, on the hour.

    Alternate Hour or 3 Hourly Schedule

    If you want something to run once every two hours, you will have to use the slash, "/", character in your field. The slash character is the "step" character. In the case of a two hourly schedule, your time component of your cron file will read:

    * */2 * * * *

    The second field, "*/2", means every alternate hour.

    Similarly, if you want something to run every 3 hours, you can change that field to "*/3", and so on.

    Other Examples

    If you want a particular command to run only at 8.00am on the 1st and 20th of every month, you should code the time as:

    0 8 1,20 * * *

    The comma, ",", means "and". If you are confused by the above line, remember that spaces are the field separators, not commas.

    What does the following schedule mean?

    2 3 4,5 6 7

    Decoded, the above line says at 3:02 am on the 4th and 5th of June (6) and on every Sunday (7), run your program.

    There are other possibilities in the time fields, and I won't go through all of them, since you already know enough to be able to construct whatever schedule you need.

You may get more details at:
How to Create a Cron Job (Scheduled Task) for Your Website or Blog

Thursday, August 7, 2008

import delimited data into MySQL

Using the LOAD DATA INFILE SQL statement

For security reasons, no one has the mysql FILE privilege , which means you cannot "LOAD DATA INFILE". You can, however, use a "LOAD DATA LOCAL INFILE" statement as long as you have a mysql prompt on our system and have uploaded the data file to your account here first.

The "LOAD DATA LOCAL INFILE" statement will only work from a MySQL prompt on our local system. It will not work from any web-based tool such as phpMyAdmin, and will never pull a file in directly off your own computer.

To import a file this way, first upload your data file to your home directory on our system with FTP or SCP. Then get a shell prompt on our system, and then a MySQL Monitor prompt so that you can issue the SQL that will import your file.

For example, suppose you have a data file named importfile.csv that contains 3 comma separated columns of data on each line. You want to import this textfile into your MySQL table named test_table, which has 3 columns that are named field1, field2 and field3.

To import the datafile, first upload it to your home directory, so that the file is now located at /importfile.csv on our local system. Then you type the following SQL at the mysql prompt:

LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);

The above SQL statement tells the MySQL server to find your INFILE on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, and to put it into your MySQL test_table as columns field1, field2, and field3 respectively.

Wednesday, July 23, 2008

Wednesday, July 9, 2008

Linux Killing a Process using pid or process name

To get rid of a background job, use the kill command with the job number as an argument. For example, to kill job 2:

% kill %2

To get rid of only one process, use the kill command with the process ID as an argument. For example, to kill process 12030:

% kill 12030

If that doesn't kill it, use the -9 option:
% kill -9 12030

Here is a ksh script to kill processes by name.
Usage: kill processName


---------------- cut here ---------------------
#!/usr/bin/ksh

str="$1"

ps -ef grep "$str" {
while read aline;do
cmd=`echo $aline awk '{ print $9 }'`
if [ [ $cmd = $0 ] ]; then
continue
fi

cmd=`echo $aline awk '{ print $8 " " $9 " " $10 }'`
if [ [ $cmd = "grep" ] ]; then
continue
fi

cmd2=`echo $cmd grep "$str"`
if [ [ $cmd2 = "" ] ]; then
continue
fi

print "$aline"
pid=`echo $aline awk '{ print $2 }'`
print "kill pid = $pid"
kill -9 $pid
done
}

Linux Getting info of a process like pid(process id)

there are a collection of command to find the process information. which are:

1) Simply use ps command as follows:

ps aux | grep {process-name}
For example find out if mysqld process (mysqld pid) is running or not:
$ ps aux | grep mysqld
Output:

mysql    28290  1.1  2.7 340496 56812 ?        Sl   Jul31 348:27 /usr/libexec/mysqld


2)Find the process ID of a running program using pidof
pidof command finds the process id’s (pids) of the named programs. It prints those id’s on screen.

# pidof httpd
1874 1873 1872 1863 1862 1861 1860 1859 1829

3)PG grep is also used to know the PID

u can use pgrep like this
pgrep process name
pgrep httpd
pgrep named
pgrep firefox


Thursday, May 8, 2008

/dev/null

In Unix-like operating systems, /dev/null or the null device is a special file that discards all data written to it (but reports that the write operation succeeded), and provides no data to any process that reads from it (it returns EOF). In Unix programmer jargon, it may also be called the bit bucket or black hole.

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.

Technically /dev/null is defined as a null device. Anything written to it is lost forever. If you read the wiki description of /dev/null, you will get to know lots of jargons/metaphors written/referred by UNIX techies a lot. In this article, we will concentrate on the importance of /dev/null, why it is there, and how to use it.

Importance

When things written to it are lost forever, then why do we need it? This is a valid questions, but we need /dev/null/ to lose information. That's correct, to explain let's take an example:

Example 1:

Suppose, you want to list all files of directory /var/tmp/ having word "foo" in its content. To achieve the task, we will write something as:

]$ grep -l foo /var/tmp/*
/var/tmp/storagedata
/var/tmp/storagedata.0
grep: /var/tmp/td.log: Permission denied

So we got two files having word "foo"; but we also an annoying error message which was part of STDERR. We were not interested in any error message, and we wanted to see only those files which I am permitted to read. So, how do I get rid of this error message? Luckily newer version of grep provides a silent option "-s", using which we can get rid of this message. But what if I am working on a traditional system, having traditional grep? What if the system command is not having the silent option? The solution is given below:

Capturing STDOUT only

  1. Using newer grep:
       ]$ grep -ls foo /var/tmp/*
    /var/tmp/storagedata
    /var/tmp/storagedata.0
  2. Using traditional grep:
       ]$ grep -l foo /var/tmp/* 2>/dev/null
    /var/tmp/storagedata
    /var/tmp/storagedata.0
  3. In general using any system command :
       ]$ cmd 2>/dev/null

Most of you should have captured the importance of /dev/null by now. In simple words, by using "2>/dev/null", we are asking the shell to redirect the STDERR to /dev/null. This is very useful when we execute system command through a program and expect to get only the STDOUT.

There may also be a case, when we are only interested to see the error message (STDERR), and are not at all interested in STDOUT. To get STDERR, and to discard the STDOUT we will redirect the STDERR to STDOUT and will redirect the STDOUT to /dev/null; as given below:

Capturing STDERR only

]$ cmd 2>&1 1>/dev/null

You may be thinking, that if I am redirecting STDERR to STDOUT which in-turn is redirected to /dev/null, so there should not be any output. But that is not the case, and you get the error messages.


Thursday, April 24, 2008

find the Nth max sal in employee table.

Here is the query to find the Nth max salary of an employee:

SELECT distinct(salary) from employee order by salary desc limit(n-1,1);

like for 3rd highest salary query will be:

SELECT distict (salary) from employee order by salary desc limit(2,1);

Tuesday, April 1, 2008

Linux Directory Structure

A brief introduction to the Linux Directory Structure

Overview

One of the most noticable differences between Linux and Windows is the directory structure. Not only is the format different, but the logic of where to find things is different.

The Format

In Windows, you use this format to access a directory:

Code:
C:\Folder1\subfolder\file.txt

In Linux, this is the basic format:

Code:
/Folder1/subfolder/file.txtYou'll notice that the slashes are forward slashes in Linux versus backslashes in Windows. Also, there is no drive name (C:, D:, etc.) in Linux. At boot, the 'root partition' is mounted at /. All files, folders, devices and drives are mounted under /. Though it is not apparent from this example, it is important to note that files and folders in Linux are case sensitive. /Folder1/subfolder/file.txt is not the same as /folder1/subfolder/file.txt.

The Main Directories

These are the basic directories that you (should) have after installing any Linux distribution:

Quote:
/bin/
/dev/
/etc/
/home/
/lib/
/mnt/
/proc/
/root/
/sbin/
/tmp/
/usr/
/var/

/bin/

This is where all your programs that are accessible to all users will be stored once installed.

/dev/

This is a virtual directory where your devices are 'stored.' Devfs allows Linux to list devices (hard drives, input devices, modems, sound cards, etc.) as 'files.'

/etc/

This is where you'll find all your global settings. Daemons such as ssh, telnet, and smtp/pop3 mail servers find their configuration files here. Also in /etc/ is the system's password file, group lists, user skeletons, and cron jobs.

/home/

This is the default directory where non-root users' homes are created. When you add a user, the default home directory is created as /home/username. You can change this default in the proper file in /etc/.

/lib/

This is where shared libraries (perl, python, C, etc.) are stored. Also in /lib/ are your kernel modules.

/mnt/

This is the default location for mounting cdroms, floppy disk drives, USB memory sticks, etc. You can mount anything anywhere, but by default there is a /mnt/floppy (if you have a floppy drive) and /mnt/cdrom.

/proc/

This virtual folder contains information about your system. You can view processor statistics/specifications, PCI bus information, ISA bus information, and pretty much anything else you want to know about the hardware on your system.

/root/

This is the default home directory for the user root.

/sbin/

This is where system programs are installed. These include fdisk, tools to make partitions, certain network tools, and other things that normal users shouldn't have a need for.

/tmp/

This is the default location to place files for temporary use. When you install a program, it uses /tmp/ to put files during installation that won't be needed once the program is installed.

/usr/

This contains various programs, non-daemon program settings and program resources.

/var/

This is where your log files, system mail messages and database of installed programs are स्तोरेड

Thursday, February 7, 2008

AWK Programming

AWK

The AWK utility, with its own self-contained language, is one of the most powerful data processing engines in existence — not only in Linux, but anywhere (named for the last initials of its creators, Alfred Aho, Peter Weinberger, and Brian Kernighan). It allows you to create short programs that read input files, sort data, process it, perform arithmetic on the input, and generate reports, among myriad other functions.

What Is AWK?

To put it the simplest way possible, AWK is a programming-language tool used to manipulate text. The language of the AWK utility resembles the shell-programming language in many areas, although AWK's syntax is very much its own. When first created, AWK was designed to work in the text-processing arena, and the language is based on executing a series of instructions whenever a pattern is matched in the input data. The utility scans each line of a file, looking for patterns that match those given on the command line. If a match is found, it takes the next programming step. If no match is found, it then proceeds to the next line.

While the operations can get complex, the syntax for the command is always:

awk '{pattern + action}' {filenames}

where pattern represents what AWK is looking for in the data, and action is a series of commands executed when a match is found. Curly brackets ({}) are not always required around your program, but they are used to group a series of instructions based on a specific pattern.

Understanding Fields

The utility separates each input line into records and fields. A record is a single line of input, and each record consists of several fields. The default-field separator is a space or a tab, and the record separator is a new line. Although both tabs and spaces are perceived as field separators by default (multiple blank spaces still count as one delimiter), the delimiter can be changed from white space to any other character.

To illustrate, look at the following employee-list file saved as emp_names:

46012   DULANEY     EVAN        MOBILE   AL
46013   DURHAM      JEFF        MOBILE   AL
46015   STEEN       BILL        MOBILE   AL

As AWK reads the input, the entire record is assigned to the variable $0. Each field, as split with the field separator, is assigned to the variables $1, $2, $3, and so on. A line contains essentially an unlimited number of fields, with each field being accessed by the field number. Thus, the command

awk '{print $1,$2,$3,$4,$5}' names

would result in a printout of

46012 DULANEY EVAN MOBILE AL
46013 DURHAM JEFF MOBILE AL
46015 STEEN BILL MOBILE AL
 
$ awk '{print $2,$3}' emp_names
 
DULANEY EVAN
DURHAM JEFF
STEEN BILL

Working with Patterns

You can select the action to take place only on certain records, and not on all records, by including a pattern that must be matched. The simplest form of pattern matching is that of a search, wherein the item to be matched is included in slashes (/pattern/). For example, to perform the earlier action only on those employees who live in Alabama:

$ awk '/AL/ {print $3,$2}' emp_names
EVAN DULANEY
JEFF DURHAM
BILL STEEN

Braces and Field Separators

The field separator differentiating one field from another need not always be white space; it can be any discernible character. To illustrate, assume the emp_names file separated the fields with colons instead of tabs:

$ cat emp_names
46012:DULANEY:EVAN:MOBILE:AL
46013:DURHAM:JEFF:MOBILE:AL
46015:STEEN:BILL:MOBILE:AL

If you attempted to print the last name by specifying that you wanted the second field with

$ awk '{print $2}' emp_names

you would end up with twelve blank lines. Because there are no spaces in the file, there are no discernible fields beyond the first one. To solve the problem, AWK must be told that a character other than white space is the delimiter, and there are two methods by which to inform AWK of the new field separator: Use the command-line parameter -F, or specify the variable FS within the program. Both strategies work equally well, with one exception, as illustrated by the following example:

$ awk '{FS=":"}{print $2}' emp_names
 
DURHAM
STEEN
FELDMAN
 
Replace the content or modify 
 
First, suppose you have a file called 'file1' that has 2 columns of numbers, and you want to make a new file called 'file2' that has columns 1 and 2 as before, but also adds a third column which is the ratio of the numbers in columns 1 and 2. Suppose you want the new 3-column file (file2) to contain only those lines with column 1 smaller than column 2. Either of the following two commands does what you want:



awk '$1 < $2 {print $0, $1/$2}' file1 > file2



-- or --



cat file1 | awk '$1 < $2 {print $0, $1/$2}' > file2
 
This is some of the basic functionality. You can get more content on awk through this links.
 
1.  http://sparky.rice.edu/~hartigan/awk.html
2.  http://www.computing.net/unix/wwwboard/forum/6189.html
3.  http://scitsc.wlv.ac.uk/cgi-bin/mansec?1+awk
 
 
Regards,
Pinky

Friday, January 11, 2008

setup default gateway with a route command

route command show and/or manipulate the IP routing table under Linux and UNIX oses.
Route manipulates the kernel’s IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program. When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.

Display default route
Following three-command display the current routing table:

1. # route
2. $ /sbin/route
3. $ /sbin/route -n

use -n option, to display numerical addresses instead of trying to determine symbolic host names (via dns or /etc/hosts file). This is useful if you are trying to determine why the route to your nameserver has vanished.

Add / setup a new route
The syntax is as follows:

route add default gw {IP-ADDRESS} {INTERFACE-NAME}

Where,
IP-ADDRESS: Specify router IP address
INTERFACE-NAME: Specify interface name such as eth0

For example if your router IP address is 192.168.1.254 type the following command as the root

# route add default gw 192.168.1.254 eth0

OR use hostname such as dsl-router:

# route add default gw dsl-router eth0

Friday, January 4, 2008

Recover your sudoers file .......the mishandled one... ;)

you can use this commands to recover your sudoers file..

# vim -r /etc/sudoers

or

recover -r /etc/sudoers

Yum Installation for php, mysql and apache on linux machine

This command is to install php, mysql and apache on linux machine

# yum -y install httpd php mysql mysql-server php-mysql

Enjoy Linux!!

Backup and Restore Mysql data using Linux commands.

Backup and Restore Mysql data using Linux commands.The steps includes:
A)Backup:

1.Connect to server
2.Create a Directory where you would like to backup your data(like /bak).
3.using shell prompt(terminal) to to the dir(/bak)
4.use the following command to backup the data:

mysqldump -u username -ppassword dbname > filename.sql

please note there shouldn't be space between -p and password.

B)Restore:

1. Open SSh file browser
2. upload dump file somewhere (any directory)
3. Open SSH Secure shell client with your SSH uid and password

$mysql -u dbuid -ppass (Please note that there is no space between -p
and password)
mysql>use dbname
mysql>source /home/pinky/pairdum.sql (location of the dump file)


* for any help use help command

Pinky's Linux Learnings

Pinky's Linux Learnings
Hello Friends,

This blog is dedicated to my learnings in Linux....MyLinuxWiki...says....myLinuxWikiPadia....

that is 'What I Know Is.'...


So lets start with learnings...u can also post here what all u know...and would like to share amongst all our linux friends....!!
 
Custom Search