Archive for กุมภาพันธ์, 2009

Linux Set Date

date -s "2 OCT 2006 18:00:00"

  • Comments Off

MySQL Backup ใน Linux (Debian)

การทำสำรองข้อมูลของฐานข้อมูลถือว่าเป็นงานที่สำคัญสำหรับผู้ดูแลระบบเครือข่ายที่จะ
ต้องรับผิดชอบในการรักษาข้อมูลของหน่วยงาน การสำรองข้อมูลของ MySQL นั้นจะมี
คำสั่งง่าย ๆ ในการ สำรองข้อมูลดังนี้

# mysqldump --host=10.0.1.10 --user=root --password=data123 engineer > engineer.sql

โดย ?host จะเป็นการระบุเครื่องที่เป็น database server ส่วน ?user จะระบุชื่อของ user
?password เป็นรหัสผ่าน ส่วนคำว่า engineer เป็นชื่อของฐานข้อมูลครับ สุดท้ายให้ส่งข้อมูล
ไปยังไฟล์ที่ชื่อ engineer.sql เราก็จะได้ไฟล์ที่เก็บ SQL ของฐานข้อมูลนั้น ๆ ในกรณีที่ต้องการ
backup ทุกฐานข้อมูลเลยก็ให้ใช้

# mysqldump --host=10.0.1.10 --user=root --password=data123 --all-databases > database.sql

เมื่อทำการ backup แล้ว ในส่วนของการ restore ให้ใช้คำสั่งดังนี้ครับ

# mysql --host=10.0.1.10 --user=root --password=data123 < database.sql

ในการทำสำรองข้อมูลนั้น บางครั้งมานั่งสั่งเองมันอาจจะไม่สะดวก มีโอกาสที่ผู้ดูแลเครือข่าย
อาจจะลืมสำรอง เราก็สามารถทำการเขียน script ให้ทำการสำรองข้อมูลโดยอัตโนมัติได้
โดยผมจะสร้างไฟล์ที่ชื่อ /backup/backup_sql สำหรับทำการสำรองข้อมูล โดยมี script ดังนี้

File : /backup/backup_sql

rm /backup/x.sql -f
mysqldump –host=10.0.1.10 –user=root –password=data123 –all-databases > /backup/x.sql
if [ -f /backup/x.sql ]
then
rm /backup/database2.sql -f
mv /backup/database1.sql /backup/database2.sql
mv /backup/x.sql /backup/database1.sql
fi
exit 0

เมื่อสร้าง script เสร็จแล้วอย่าลืมกำหนด permission ให้สามารถรันได้ดังนี้ครับ

# chmod a+x /backup/backup_sql

จากนั้นไปเพิ่มในไฟล์ /etc/crontab เพื่อตั้งเวลาสำหรับเรียก script ดังนี้ครับ

File : /etc/crontab

20 3	* * * 	root	/bin/sh /backup/backup_sql

สุดท้ายครับ สำหรับการ restore ฐานข้อมูลแบบทั้ง server นั้น มันจะทำการ update ตัว user
ด้วย ซึ่งเมื่อทำการ restore แล้วอย่าลืมเข้าตัว phpmyadmin แล้วทำการ reload the privileges
และตัว user ที่ชื่อ debian-sys-maint จะต้องตั้ง password ใหม่โดยให้ไปเอา password ของ user
ที่ชื่อ debian-sys-maint ได้จากไฟล์ /etc/mysql/debian.cnf เอาล่ะ แค่นี้งานดูแลฐานข้อมูลก็
เป็นเรื่องเด็ก ๆ แล้วครับ

  • Comments Off

Have you ever had the problem that you have set up the “perfect system”, and now you want to back up this system before you make changes to it so that you can restore the original state if you changes are not satisfying? Or are you a system administrator in a large company where you have to maintain hundreds of Linux machines that run exactly the same software, but are sick of installing each machine manually? Or did you develop a Linux-based hardware appliance, and now you want to sell it in big numbers on different hardware platforms (i.e., different hard disks, etc., not different processor architectures!) without having to maintain an image for each platform? Or do you want to distribute this solution to your resellers overseas, so that you do not have to ship expensive hardware and your resellers can sell their own hardware*? This is where SystemImager comes into play!

From the SystemImager website:

“SystemImager is software that automates Linux installs, software distribution, and production deployment.

SystemImager makes it easy to do automated installs (clones), software distribution, content or data distribution, configuration changes, and operating system updates to your network of Linux machines. You can even update from one Linux release version to another!

It can also be used to ensure safe production deployments. By saving your current production image before updating to your new production image, you have a highly reliable contingency mechanism. If the new production enviroment is found to be flawed, simply roll-back to the last production image with a simple update command!

Some typical environments include: Internet server farms, database server farms, high performance clusters, computer labs, and corporate desktop environments.”

SystemImager lets you create images of your installations. To do so, you need an image server (should have enough disk space to store your images) and a so-called golden client (i.e., your “perfect system” of which you want to make an image). This means that you have to install some software on your image server and on your golden client in order to run SystemImager. SystemImager provides Debian packages as well as rpm packages for rpm-based distributions such as RedHat, Fedora, SUSE or Mandrake. In this example both my image server and my golden client are running under Debian.

This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.

This document comes without warranty of any kind!

*This is what we do in the company I am working for. We distribute images of our spamfilter appliance to our resellers over the internet, so that they can install and sell it on their own hardware platforms. If you are interested, write to info@projektfarm.de.

Important: In new SystemImager versions (I think starting with 3.4) you have to prepend the string “si_” to all commands, so getimage becomes si_getimage, prepareclient becomes si_prepareclient, mkautoinstalldiskette becomes si_mkautoinstalldiskette and so on.

1 Install SystemImager On The Image Server

Add

deb http://download.systemimager.org/debian stable main

to /etc/apt/sources.list and run

apt-get update

To install the SystemImager server simply run

apt-get install systemimager-server

2 Install SystemImager On The Golden Client

Add

deb http://download.systemimager.org/debian stable main

to /etc/apt/sources.list and run

apt-get update

To install the SystemImager client simply run

apt-get install systemimager-client

3 Create An Image Of your Golden Client On The Image Server

Now let’s assume that my image server has the IP address 192.168.0.2, and my golden client has the IP address 192.168.0.100.

In order to pull an image from the golden client, you should shut down any firewall on the golden client, or at least allow connection to port 873 TCP (rsync). Now, on the golden client, run the following command:

prepareclient –server 192.168.0.2

On the image server, run

getimage -golden-client 192.168.0.100 -image my_golden_client -ip-assignment replicant

where my_golden_client is the name of the image you want to create. -ip-assignment can have one of the following 4 values (see man getimage):

? static_dhcp — A DHCP server will assign the same static address each time to clients installed with this image. The DHCP server in this case also assigns the corresponding host name. It is possible therefore, when using static dhcp, to change a machine’s hostname and IP address by simply changing one value in the dhcpd.conf file. Some may find this desirable. Also see the mkdhcpstatic command.

? dynamic_dhcp — A DHCP server will assign IP addresses dynamically to clients installed with this image. In dynamic dhcp, the IP address of a machine may change, but you want it to retain the same host name. Therefore the hostname is set by the machine itself. They may be assigned a different address each time.

? static — The IP address the client uses during autoinstall will be permanently assigned to that client.

? replicant — Don’t mess with the network settings in this image. I’m using it as a backup and quick restore mechanism for a single machine.

4 Create A Boot Diskette

SystemImager lets you install images on systems that have nothing installed on it. But we have to tell such a system where to get the image from. This can be done with a boot diskette (which means that your new system needs an IDE floppy drive, it will not work with a USB floppy drive). To create a boot diskette, insert an empty diskette in your image server and run

mkautoinstalldiskette

This will create a standard boot diskette. Now we have to create a configuration file called local.cfg:

HOSTNAME=www
DOMAINNAME=example.com
DEVICE=eth0
IPADDR=192.168.0.100
NETMASK=255.255.255.0
NETWORK=192.168.0.0
BROADCAST=192.168.0.255
GATEWAY=192.168.0.1
GATEWAYDEV=eth0
IMAGESERVER=192.168.0.2
IMAGENAME=my_golden_client

It should contain the desired network settings of your new machine. DEVICE and GATEWAYDEV should be the Linux device name of your network card. IMAGESERVER should be the IP address of your image server, and IMAGENAME should be the name of the image to be retrieved.

Save this file to your boot diskette. You can even do this on your Windows workstation, but be sure to save the file with UNIX linebreaks!


Another way of creating your boot diskette is this:

Create a file /var/lib/systemimager/my_golden_client.local.cfg on your image server. /var/lib/systemimager/my_golden_client.local.cfg should be filled with the same contents as local.cfg above. Then run

mkautoinstalldiskette -config /var/lib/systemimager/my_golden_client.local.cfg

This will create your boot diskette with your local.cfg included!


A third way of creating a boot diskette is this: Download this boot diskette image and write it to a diskette. On a Linux system, you can do this with the following command:

dd if=systemimager-floppy-image.img of=/dev/fd0

On Windows, you can use a tool like RawWrite to achieve this. Afterwards, you still have to create a local.cfg on the diskette as described in the first method to make a boot diskette above.

5 Install Image On A New System

On your image server, execute the following command:

/etc/init.d/systemimager-server start

or

/etc/init.d/systemimager-server-rsyncd start (depends on your version of SystemImager)

Be sure that port 873 TCP can be accessed from the outside (firewall!).

Now insert your boot diskette into your new system and boot from the diskette. Your new system will install the image my_golden_client automagically, and you are done!

6 Update An Image

Let’s say you have made some important changes to your golden client, and now you want to update your previously created image my_golden_client. The steps are the same as in paragraph 3:

On your golden client, run

prepareclient –server 192.168.0.2

(Be aware of your firewall!)

On the image server, run:

getimage -golden-client 192.168.0.100 -image my_golden_client -ip-assignment replicant

7 Delete An Image

If you want to delete an image on your image server, simply run

rmimage my_golden_client

(my_golden_client should be replaced with the name of the image to be deleted.)

Links

SystemImager: http://www.systemimager.org/

  • Comments Off
คำสั่งที่เกี่ยวข้อง
1. fdisk เป็นคำสั่งที่เอาไว้ใช้จัดการเรื่อง partition ของ Hard drive
2. partprobe เป็นคำสั่งที่เอาไว้สำหรัรันเพื่อ update partition table
3. mkfs.ext3 คำสั่งในการสร้าง file system ext3
4. e2lable คำสั่งสำหรับไว้ทำ label ของ partition
5. mount คำสั่งสำหรับการนำ partition ที่สร้างเสร็จแล้วมาใช้งาน
[root@localhost ~]# fdisk -l

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn’t contain a valid partition table

; จากตรงนี้ จะมี hdd ขึ้นมาใหม่ชื่อว่า sdb แต่ยังไม่มี partition table 
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.

The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n  ;เพื่อ create partition
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1305, default 1305): +10GB ;ระบุขนาดตามที่เราต้องการจะสร้าง

Command (m for help): p   ;p เพื่อดูว่า partition ที่เราสร้างขึ้นมามีหรือยัง

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          17      136521   83  Linux

Command (m for help): w   ;เพื่อทำการ save
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          17      136521   83  Linux

[root@localhost ~]# apt-get install parted
[root@localhost ~]# partprobe  ;เป็นคำสั่งที่เอาไว้รันเพื่อ update partition table

[root@localhost ~]# mkfs.ext3 /dev/sdb1  ;สร้าง file system ext3
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
34136 inodes, 136520 blocks
6826 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
17 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@localhost ~]# e2label /dev/sdb1 /label_hdd2    ; สร้าง label (จะไม่สร้างก็ได้)

[root@localhost ~]# mkdir /mnt/hdd2

[root@localhost ~]# mount -t ext3 /dev/sdb1 /mnt/hdd2/

[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5             7.7G  3.0G  4.4G  41% /
/dev/sda1              99M   15M   80M  16% /boot
tmpfs                 252M     0  252M   0% /dev/shm
/dev/sda3             965M   18M  898M   2% /home
/dev/sdb1             130M  5.6M  117M   5% /mnt/hdd2   <<<< ตรวจสอบ partition ที่เราได้ทำการ mount 

;ถ้าต้องการให้ auto mount ทุกครั้งที่ restart
[root@localhost ~]# vi /etc/fstab
LABEL=/label_hdd2       /mnt/hdd2               ext3    defaults        1 2
  • Comments Off

how to partition a Linux disk

To format a disk in Linux, first it should be partitioned, this how-to assumes you have already partitioned the hard drive, if you need to to partition your disk, read the how to partition a Linux disk

Linux can format in various file systems, here we will use only ext2, ext3 or swap, which are some of the most used file systems in Linux.

Lets suppose the drive you need to format is: /dev/hda1

To format it using the filesystem ext2, enter

sudo mkfs -t ext2 /dev/hda1

If it is ext3 what you want enter:

sudo mkfs -t ext3 /dev/hda1

you can also use the mkfs.ext2, mkfs.ext3 or mkswap to format directly in the file system you want.

  • Comments Off

การติดตั้ง Tomcat ซึ่งเป็น Servlets container ต้องมี JAVA ติดตั้งอยู่ด้วย ปรกติ Tomcat ที่อยู่บน repository ของ Debian จะผูกติดกับ gij หรือ GNU interpreter for Java bytecode ซึ่งเป็นของ GNU แต่ส่วนใหญ่แล้วเรามักติดตั้ง Tomcat ควบคู่กับ JAVA ของ SUN

การใช้งานใช้ java บนของ sun บน debian นั้นต้องเพิ่ม repository ใน /etc/apt/sources.list ให้มีในส่วนของ contrib และ non-free ก่อน เช่น (สำหรับผู้ใช้ในประเทศไทยครับ)

Mirror แห่งชาติ ครับ

deb http://mirror.in.th/debian/ etch main contrib non-free
deb-src http://mirror.in.th/debian/ etch main contrib non-free

ของ Linux.thai.net ครับ ตามสะดวก

deb http://linux.thai.net/debian/ etch main contrib non-free
deb-src http://linux.thai.net/debian/ etch main contrib non-free

เสร็จแล้วก็

$ sudo aptitude update

เพื่อเพิ่มชื่อของโปรแกรมต่างๆ หลังจากนั้น ให้ติดตั้ง java ของ sun ครับ

$ sudo aptitude install sun-java5-jdk

รอจน install เรียบร้อยหลังจากนั้น ก็ install Tomcat ต่อครับ

$ sudo aptitude install tomcat5.5 tomcat5.5-admin tomcat5.5-webapps

รอจน install ให้เรียบร้อย หลังจากนั้นให้ตรวจสอบดูว่าตอนนี้เราใช้ Java ของ sun อยู่หรือเปล่า โดยใช้คำสั่งนี้ครับ

$ sudo update-alternatives –config java
There are 2 alternatives which provide `java’.

Selection Alternative
———————————————–
+ 1 /usr/lib/jvm/java-1.5.0-sun/jre/bin/java
* 2 /usr/bin/gij-wrapper-4.1

Press enter to keep the default[*], or type selection number: 1
Using `/usr/lib/jvm/java-1.5.0-sun/jre/bin/java’ to provide `java’.

$ sudo update-alternatives –config javac
There are 2 alternatives which provide `javac’.

Selection Alternative
———————————————–
1 /usr/lib/jvm/java-1.5.0-sun/bin/javac
*+ 2 /usr/bin/ecj-bootstrap

Press enter to keep the default[*], or type selection number: 1
Using `/usr/lib/jvm/java-1.5.0-sun/bin/javac’ to provide `javac’.

เพื่อให้โปรแกรมตัวอื่นสามารถรู้จัก JAVA_HOME ครับต้องไปแก้ที่ /etc/profile โดยเพิ่มสิ่งเหล่านี้ลงไปครับ ทุกครังที่รีบูทเครื่อง เราสามารใช้งานตัวแปร JAVA_HOME ได้

JAVA_HOME="/usr/lib/jvm/java-1.5.0-sun"
export JAVA_HOME

ตรวจสอบ java อีกครั้งเพื่อความแน่ใจ หากขึ้นประมาณนี้ถือว่าใช้ได้ครับ

$ java -version
java version “1.5.0_10″
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_10-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_10-b03, mixed mode)

ทดสอบดูว่าเราพร้อมจะเขียน JSP แล้วยัง โดยพิมพ์ที่ Web browser ว่า

http://192.168.119.129:8180/
  • Comments Off

Time

Mp3

Msn status

  • manon2029@hotmail.com is

Chat with Meeh

Donate

    If you find an article useful, then please make a donation.

หมวดหมู่

UserOnline

Counter

  • Visitors today: 32
  • Visitors yesterday: 83
  • Visitors per day: 320
  • Max. visitors per day: 255
  • Total page views: 166,285
  • Page views of this page: 320
  • Currently online: 1
  • Max. online: 36
  • Total visitors: 35,951
  • counterStatistics