Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, October 2, 2008

Synchronize directories/files between servers

I came across the scenario where, we have to synchronize files and directories between different servers. For example[Refer image1] my application update images in server1 only. But the same image shoule be synchronized in server2 also.







There is tool 'Rsync' for linux to acheive this scenario.
Install Rsync in two servers. We are going to sync server1 with server2. So here destination server is server2.


In server2 add the following configration files.

i) Create rsync.conf file in /etc/rsync.conf
Add the following code given in between two lines in this file.
#############################
motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[thumbnails]
path = /var/www/html/images
comment = Image Server
uid = apache
gid = apache
read only = no
list = yes

#############################
Explanation:
First four lines are log,pid,lock and motd files path

[thumbnails] is the label. This will refered in server1 to send the directories to server2.
path is the place where the directories and files will get stored.

ii) Create rsyncd.motd file in /ect/rsyncd.motd

Add some text. This will shown at the time of sync.
For expample add text like in between two lines.
#############################
File transfering.........
#############################

iii) By default rsync is disabled. So we have to enable Rsync. You can edit the file /etc/xinetd.d/rsync and chage the line as disable = no [by default disable = yes]

iv) Start the rsync server
/etc/init.d/xinetd start
Server2 [Destination server] configuration over.


In server1, only one line command will sync the file or directories to server2.

rsync -rptgou /absolute path/images 192.168.100.50::thumbnails

Explanation:
rsync ==> is the command
-rptgou ===> is the options
/absolute path/images ===> images is the directory we are going to transfer to server2
192.168.100.50::thumbnails ===> this is the server2 ip and thumbnails is the lable we have added in "rsync.conf" configuration file in server2

We can add the above command in crontab so that two servers in the sync.
Enjoy........

Monday, September 29, 2008

MySql ERROR 1054

I was breaking my head when i was doing import data from mysqldump in Linux machine. The problem was "ERROR 1054 (42S22) at line 1564: Unknown column 'cust.CUSTOMER_NAME' in 'field list' ". I surfed the net but i couldn't get any stuff.

Then i put this problem on my guru[Mukund]. He explained the problem.

MySql behave as case-in-sensitive in microsoft environment. So while writing function, procedure and trigger case is no issues in microsoft environment. But in case of Linux, MySql behave as case-sensitive. All the development machines were Microsoft Environment. Production environment is Linux.

To fix this problem [To make Linux case insensitive] add the following line in the file /etc/mysql/my.cnf
lower_case_table_name = 1

This line make MySql case-in-sensitive in linux environment.

Thursday, September 18, 2008

How to Run Unix shell script in java

I came across the scenario wherein i have to call/ Run the unix shell script. I think this would help you guys.

I have written a script called custom_script.sh.

custom_script.sh code:

cp /home/Example.java /home/Example_copy.java

In this script just i am copying a file to another.

The java code to call that script.

import java.io.BufferedWriter;
import java.io.*;
public class CallScript
{
public static void main(String s[]) throws Exception{
Runtime rt = Runtime.getRuntime();
try {
String str = "/absolute_path/test.sh";
Process child = rt.exec("/bin/bash");
BufferedWriter outCommand = new BufferedWriter(new Output
StreamWriter(child.getOutputStream()));
outCommand.write(str +"\n");
outCommand.flush();

} catch (Exception e) {
e.printStackTrace();
}
}
}

Thursday, July 31, 2008

clone hard disk in linux

Install all software for development machine is tedious job. Just think of a team of 5 or above. Linux offer DD command to reduce this job as much as easier.
What we could do with the help of DD command is, just install all the software in one machine and clone that hard disk to other hard disks. Here the constraints is hard disks[source and destination] should be same size.

Step 1:
Install all softwares you want.
Step 2:
Connect the second hard disk to machine where in all software has been installed.
Stop 3:
Mount command list all the media connected in that machine.
Stop 4:
Now you have find out in which hard disk you have already installed all software.
Now two hard disk connected to your machine.
but you can view only one hard disk unless you mount another one. To confirm which media is source and which media is destination, You have to mount.
While run DD command we have to give the name of the media namely sda or sdb etc... So we have to confirm the media name.
To mount
i) Make a directory in /media. For ex /media/hd
ii) run a follwoing command to mount media to a directory.
mount /dev/sda7 /media/hd
/dev directory will contains all media details.
iii) after confirmation of which media is source[for ex sdb] and which media is destination[for ex sda] have to unmount the device using the following command.
umount /media/hd
step 5:
finally you have to run the following command to clone the hard disk from one to another with root user.
dd if=/dev/sdb of=/dev/sda
Explanation:

dd => disk to disk command
if => input file
of => output file

This will take an hour to copy. You could do the same stuff for any number of machines within an hour per machine.
Enjoy maadi.

Tuesday, July 8, 2008

Sort process by memory

The following command help to sort list of process running in linux machine.
ps -eo pmem,pid,args | sort -k 1 -n -r | less

Explanation:
pmem -> list the memory occupied by the process
pid -> process ID
args -> process

Monday, June 9, 2008

compile lighttpd server from source

To install any server like 'lighttpd' for windows operating system we can download and install using .exe file.
Same way we could do it for other operating system [sun solaris, linux and mac]also. But for some specific distribution there won't be any executable version of software.

In this case, we can compile from source code and install the same.

I explained installation steps in the ubuntu[linux distribution] server.

Login as root.

There is two way to download the source code

1) Using terminal:
Copy the url of the software which we want to install. That might be any kind of compressed format. for ex:[tar.gz,tar.bz2,etc...] and use the following command
wget {URL}
this will downloads source code using terminal.
or
2) Using GUI
Click on download link and save to local file system.

Steps to compile and install:
i) Uncompress the file which one we downloaded. Using the following command in terminal
if downloaded file is .tar.gz format use tar -xzvf {downloaded file name}
if downloaded file is .tar.bz2 use tar -xjvf {downloaded file name}

ii) Get inside the folder and there is file named "configure" and type the following command
./configure
if we want install in any specific directory, mention directory using the following command.
./configure --prefix = {directory}.... [default installation directory is /opt/
]
The above command will configure the compilation environment.

iii) Type the following to compile
make
now source code compiled.

iv) To install
make install
Now server is installed and ready for you.

Sunday, April 20, 2008

clear session files - Ruby

Session is important for web application as track user. Session grow likely same as log files. This will cause memory overhead. We can clear session using Linux crontab command.

This will remove 300 minutes old session files by every day 00:10 hrs.

Place this command in the crontab file in the /usr/bin/ directroy.

Can edit the crontab file using following command.

crontab -e

* * * * * find absolute_path -name 'sess.*' -amin +300 -exec rm -rf {} \;
Explanation:
10 0 * * * find /home/tmp -name 'ruby_sess.*' -amin +300 -exec rm -rf {} \;

find - find the file that start with the name sess.

min +300
- find the session file with 300 minutes old

exec rm -rf - execute the command remove the files which is 300 minutes old.

Monday, April 7, 2008

Database backup script

The script to take a backup of postgres database

Steps:
Step i)
Write a shell script to dump the database.
Create a .sh file using vi editor inside the /bin as follows...
vi /bin/backup-script.sh

1) #!/bin/bash
2) BACKUP_DIR="/home/MyApps/regular_backup"
3) PGHOST="192.168.100.167"
4) PGUSER="postgres"
5) /usr/bin/pg_dumpall -h $PGHOST -U $PGUSER | gzip > $BACKUP_DIR/backup.gz


Explanation:
The first line #!/bin/bash is just a declaration that this is a BASH script.

2) BACKUP_DIR="/home/MyApps/regular_backup"
This line tells the target folder where our .gz file to be stored

3) PGHOST="192.168.100.167"
4) PGUSER="postgres"
Host IP where our postgres database is running and user name. If some other database have to add password .

5) /usr/bin/pg_dump -h $PGHOST -U $PGUSER | gzip > $BACKUP_DIR/backup.gz
This key statement is dump the database using command[/usr/bin/pg_dumpall] and user name and password[-h $PGHOST -U $PGUSER] and zip it up using gzip and target file backup.gz

after saved this file should the file permission to 755
chmod 755 /bin/backup-script.sh

Step ii) Add this script to crontab [Crontab (CRON TABle)is a program that manipulates the CRON daemon] and configure time to run script.

1) Edit crontab file in the directory /usr/bin/crontab file using crontab -e -e for edit/add entry in the crontab file.

2) Add this line in crontab file
10 0 * * * /bin/backup-script.sh >/dev/null 2>&1

This crontab run the script /bin/backup-script.sh on the time frame.

10 0 * * * is 10 minute 0 hours of every day every month end every year run the script which we have created in step 1[/bin/backup-script.sh]

Your script is ready and will run and backup the database everyday morning 00:10