Archive for the ‘Linux&Unix’ Category

MRTG + lm_sensors + CPU溫度 on linux

Monday, June 23rd, 2008

用 lm_sensors套件取得 cpu溫度

[1] apt-get install lm_sensors

[2] 執行 sensors-detect指令
     此用來偵測設定值,全Enter即可,
     之後它自已會將該要的module放於/etc/sysconfig/lm_sensors裡

[3] 載入/etc/sysconfig/lm_sensors裡的模組
      指令如下
         modprobe module1
         modprobe module2

     如果是 RPM安裝
        service lm_sensors start

     執行 sensors指令可看到溫度資訊 (more…)

在Linux系统下多线路ADSL的配置

Friday, June 6th, 2008

双ADSL及多ADSL增加线路的配置过程。

Linux配置多线路ADSL的方法

powered by KindGeorge http://kindgeorge.at.3322.org\经过一段时间的观察,证明运行良好,现把设置过程及方法总结一下,欢迎指正.

此文档可以说明双adsl及多adsl增加线路的配置过程.

实验环境:

操作系统: RedHat7.3

两条adsl,长期观察线路稳定,动态ip,带宽2M,

三块网卡: eth0 tulip,接内网

eth1 3c59x,接第一条adsl

eth2 8139too,接第二条adsl

ethn xxxx,(如果还有的话….)

目的:用两条adsl共同上网,分担负载,实现一般的简单负载平衡,带动内网上网.

1.添加网卡

插入网卡,启动机器,如果需要双线路上网,就要三块网卡了,配置网卡也可以参考其他资料.

配置为找到相应的型号模块,例如:eth2是8139的网卡

[root@kindgeorge root]# vi /etc/modules.conf
alias parport_lowlevel parport_pc
alias eth0 tulip
alias eth1 3c59x
alias eth2 8139too

把新的线路插在eth2上

2.配置adsl

方法一:

(1)运行adsl-setup程序,会一步一步的提示你完成配置过程.

创建ppp0和ppp1的拨号配置文件,并保存配置,一般保存在/etc/sysconfig/network-scripts/ifcfg-ppp1

(2)确定/etc/sysconfig/network-scripts/ifcfg-ppp0文件,其中的PIDFILE参数设为:

PIDFILE=/var/run/ppp-adsl.pid

修改/etc/sysconfig/network-scripts/ifcfg-ppp1文件,将其中的PIDFILE参数设为:

PIDFILE=/var/run/ppp-adsl1.pid

如果你有更多的线路,可以继续增加0,1,2,3….等

这是为了使不同的拨好用不同的pid,如果不修改此参数将无法启动第二条线路接口。

(3)配置文件一般是:

USERCTL=no
     BOOTPROTO=dialup
     NAME=DSLppp1
     DEVICE=ppp1
     TYPE=xDSL
     ONBOOT=yes
     PIDFILE=/var/run/pppoe-adsl1.pid
     FIREWALL=NONE
     PING=.
     PPPOE_TIMEOUT=20
     LCP_FAILURE=3
     LCP_INTERVAL=20
     CLAMPMSS=1412
     CONNECT_POLL=6
     CONNECT_TIMEOUT=60
     DEFROUTE=yes
     SYNCHRONOUS=no
     ETH=eth2 (对应新线路的网卡)
     PROVIDER=DSLppp1
     USER=isp提供的新用户名字2
     PEERDNS=no

方法二:

(1)直接拷贝

cp /etc/sysconfig/network-scripts/ifcfg-ppp0 /etc/sysconfig/network-scripts/ifcfg-ppp1

(2)修改其中的ppp0为ppp1,

修改PIDFILE=/var/run/pppoe-adsl.pid 为PIDFILE=/var/run/pppoe-adsl1.pid

修改采用的新接线路的网卡,例如:改ETH=eth1 为ETH=eth2

(3)增加新线路的帐号和密码. 帐号密码一般是保存在/etc/ppp/chap-secrets 和pap-secrets

我们只需要在最后增加新的用户名2和密码2即可.

/etc/ppp/chap-secrets文件一般是这样的:

# Secrets for authentication using CHAP

# client server secret IP addresses

“用户名1″ * “密码1″

“用户名2″ * “密码2″

“用户名n” * “密码n”

3.启动ppp接口

因为adsl-start 命令缺省只能启动第一的ppp接口。所以要启动两个接口,必须指定配置文件。

可以用:方法一:

ifup ppp0

ifup ppp1

(ifup pppn…)

或方法二:

adsl-start /etc/sysconfig/network-scripts/ifcfg-ppp0

adsl-start /etc/sysconfig/network-scripts/ifcfg-ppp1

(adsl-start /etc/sysconfig/network-scripts/ifcfg-pppn)

4.查看新设置是否启动:

方法一:执行: ifconfig

出现ppp0 和ppp1 ,并且均得到ip了.说明成功了,结果象这样:

ppp0   Link encap:Point-to-Point Protocol 
          inet addr:218.114.37.137  P-t-P:61.142.110.30  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1492  Metric:1
          RX packets:165721 errors:0 dropped:0 overruns:0 frame:0
          TX packets:123673 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:157324193 (150.0 Mb)  TX bytes:14068892 (13.4 Mb)

   ppp1   Link encap:Point-to-Point Protocol 
          inet addr:218.114.35.62  P-t-P:61.142.110.30  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1492  Metric:1
          RX packets:560055 errors:0 dropped:0 overruns:0 frame:0
          TX packets:439711 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:530025378 (505.4 Mb)  TX bytes:80895162 (77.1 Mb)

方法二: 执行ip高级命令: ip link ls

也可以检验ppp0和ppp1,结果象这样:

1: lo: mtu 16436 qdisc noqueue
            link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
         2: eth0: mtu 1500 qdisc pfifo_fast qlen 100
            link/ether 00:50:bf:07:4e:32 brd ff:ff:ff:ff:ff:ff
         3: eth1: mtu 1500 qdisc pfifo_fast qlen 100
            link/ether 00:01:02:9a:31:b9 brd ff:ff:ff:ff:ff:ff
         4: eth2: mtu 1500 qdisc pfifo_fast qlen 100
            link/ether 00:e0:4c:79:44:71 brd ff:ff:ff:ff:ff:ff
       268: ppp1: mtu 1492 qdisc pfifo_fast qlen 3
            link/ppp
       273: ppp0: mtu 1492 qdisc cbq qlen 3
            link/ppp

方法三: 用命令测试反应

ping -I ppp0 202.96.134.133 (测试ppp0的线路状况)

ping -I ppp1 202.96.134.133 (测试ppp1的线路状况)

ping -I ppp2 ……n (如果还有多条的话)

5.如果单单是要实现链路负载平衡,让ppp0和ppp1分担负载,那么我们象以下设置一下配置:

(1)启动路由,允许转发echo 1 > /proc/sys/net/ipv4/ip_forward

(2)进行伪装:

iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -o ppp0 -j MASQUERADE

iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -o ppp1 -j MASQUERADE

iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -o pppn -j MASQUERADE (如果还有多条的话)

(3)修改网关

ip route replace default scope global nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1

说明: 用replace的原因是当原来没有网关时会增加这个网关,当已经存在网关时,会修改原来的网关.

用add 也可以,但是当已经存在默认网关时就要先delete再add.

两个weight 1的意思是这两条链路的权值是相等的,两条链路承担的网络流量是相等的。

有一点是需要说明的,因为路由表是基于缓存的,所以在实际中两条链路并不能100%的平分流量

nexthop NEXTHOP 设置多路径路由的下一跳地址。NEXTHOP比较复杂,它的语法和以下高层参数类似:

via ADDRESS–表示下一跳路由器;

dev NAME–表示输出设备;

weight NUMBER–在多路由路径中,这个元素的权重。表示相对带宽或者服务质量。

如果你有多条线路,那么继续在其中增加 nexthop dev ppp2(ppp3……)即可

(4) 刷新路由

ip route flush cache

6.即时检查数据的方向

用tcpdump 分别监察两条线路,用下面命令:

tcpdump -i ppp0
  tcpdump -i ppp1
  tcpdump -i ppp2……n (有多条的话)

7.为了每次启动时自动启动,把命令写在启动脚本里面,在 /etc/rc.local 后面增加:

ifup ppp0
  ifup ppp1
  echo 1 > /proc/sys/net/ipv4/ip_forward
  iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -o ppp0 -j MASQUERADE
  iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -o ppp1 -j MASQUERADE
  ip route replace default scope global nexthop dev ppp0 weight 1 nexthop dev ppp1 weight 1
  ip route flush cache

结果:运行数月,效果稳定良好。

上网会明显加快,为何呢,因为两条线路分摊了负载.例如,内网80人同时上网, 如果网络是一条2M的adsl,那么整条线路都拥挤,挤得死死的,每人分得的带宽可能只有30k.大家都会明显感觉上网很慢。

如果增加多一条线路,那么两条2M共同分摊拥挤程度,这样,可能每人分得的带宽就会变成60K,是原来的加倍,以这种形式来加快上网速度,增加了并发连接的个数。

Amfphp简介

Wednesday, May 14th, 2008

Amfphp 是PHP的RPC工具,它可以使PHP与下述技术无缝通信:

  • Flash 和 Flex Remoting
  • JavaScript JSON 和 Ajax JSON
  • XML 和XML-RPC

一、什么是RPC?

远端程序调用(RPC, Remote Procedure Call) 是一种客户端与服务器端交换数据方式。我们可以调用本地对象带对各种参数方法 ,设置回调并接受调用结果。我们不用关心发送和接收数据的实现细节。实现细节通常是抽象的, 就像我们在调用本地方法一样.

二、工作原理

客户端(Flash)与服务器端(php), 使用相同的方式描述方法调用和复杂数据。客户端序列化请求并将它发送到网关Amfphp。Amfphp再执行:

  • 反序列化请求
  • 找到相应的远程服务类
  • 实例化类
  • 执行安全检查
  • (使用指定参数)调用服务器端方法
  • 序列化返回的数据

Amfphp 可以正确地序列化、反序列化复杂类型数据。除了对象和数组,它还支持resources 数据连接资源,这就意味着我们可以通过调用远程方法简单返回mysql_query,amfphp 会处理这一切。 如果平台支持 (目前来说,Flash Remoting 和Flex Remoting), phpamf还可以处理循环引用和自定义数据。 它也支持简单的远程调试。还有amfphp 附带一个服务浏览器,它可以在创建客户端代码前测试远程服务。amfphp 1.0.1还添加了模板,可以自动生成客户端代码。Amfphp 1.9 beta更是新增了对AMF3的支持。详见http://www.riafan.com/article.asp?id=31

三、教学资源

1. 英文

2. 中文

centos 5.1 xen实例

Wednesday, May 14th, 2008

建立一个文件,使其成为guest所使用的磁盘:
mkdir /xen
dd if=/dev/zero of=/xen/hdcentos.img bs=1M count=6000

这样就在/xen中建立了一个6G左右的文件。

newfs下这个文件:

mke2fs -F -j /xen/hdcentos.img

将对应的文件挂到系统中来:

mount -o loop /xen/hdcentos.img /mnt/vm

为vm建立对应的设置

# mkdir /mnt/vm/dev
# /sbin/MAKEDEV -d /mnt/vm/dev -x console
# /sbin/MAKEDEV -d /mnt/vm/dev -x null
# /sbin/MAKEDEV -d /mnt/vm/dev -x zero

为vm准备对应的fstab文件

/dev/sda1 / ext3 defaults 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0

加臷proc

# mkdir /mnt/vm/proc
# mount -t proc none /mnt/vm/proc

安装操作系统:

yum –installroot=/mnt/vm -y groupinstall Base

配置网卡(/etc/sysconfig/network-scripts/ifcfg-eth0):

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no

配置网络在启动时开始工作(/etc/sysconfig/network):

NETWORKING=yes

做一个在dom0上引导domU的initrd:

mkinitrd -v -f –fstab /mnt/vm/etc/fstab –with xenblk –with xennet –preload xenblk –preload xennet initrd-2.6.18-53.el5xen.vbird.img `uname -r`

最后,写上xen的配置文件(/etc/xen/hdcentos):
name = “hdcentos”
maxmem = 2048
memory = 2048
vcpus = 1
kernel = “/boot/vmlinuz-2.6.18-53.el5xen”
ramdisk = “/boot/initrd-2.6.18-53.el5xen.vbird.img”
on_poweroff = “destroy”
on_reboot = “restart”
on_crash = “restart”
vfb = [ “type=vnc,vncunused=1,keymap=en-us” ]
disk = [ “tap:aio:/xen/hdcentos.img,sda1,w” ]
vif = [ “bridge=xenbr0″ ]
root = “/dev/sda1 ro ”

下面你就可以启动你自己的虚拟domU了。

如何保护自己编写的shell程序

Monday, April 28th, 2008

要保护自己编写的shell脚本程序,方法有很多,最简单的方法有两种:1、加密 2、设定过期时间,下面以shc工具为例说明:
一、安装shc工具
shc是一个加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件.
 
shc的下载地址:             
http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz

安装:
tar zxvf shc-3.8.tgz
cd shc-3.8
make test
make
make test
make strings
make install  这一步需要root权限
 
二、加密方法:
shc -r -f scrīpt-name  注意:要有-r选项, -f 后跟要加密的脚本名.
运行后会生成两个文件,scrīpt-name.x 和 scrīpt-name.x.c
scrīpt-name.x是加密后的可执行的二进制文件.
./scrīpt-name    即可运行.
scrīpt-name.x.c是生成scrīpt-name.x的原文件(c语言)

三、设定期限:
首先使用shc转化为二进制,并加上过期时间,如
./shc -e 12/06/2006 -m “please contact yazjiyao@yahoo.com.cn” -r -f flushvpn.sh
 
 我一般在程序中加入自动更新系统时间 的命令,防止用户更改系统时间。

$(( )) 與 $( ) 還有${ } 差在哪?

Saturday, April 26th, 2008

我們上一章介紹了 ( ) 與 { } 的不同,這次讓我們擴展一下,看看更多的變化:$( ) 與 ${ } 又是啥玩意兒呢?

在 bash shell 中,$( ) 與 ` ` (反引號) 都是用來做命令替換用(command substitution)的。
所謂的命令替換與我們第五章學過的變量替換差不多,都是用來重組命令行:
* 完成引號裡的命令行,然後將其結果替換出來,再重組命令行。
例如:
[code]$ echo the last sunday is $(date -d “last sunday” +%Y-%m-%d)[/code]
如此便可方便得到上一星期天的日期了… ^_^

在操作上,用 $( ) 或 ` ` 都無所謂,只是我”個人”比較喜歡用 $( ) ,理由是:

1,  ` ` 很容易與 ‘ ‘ ( 單引號)搞混亂,尤其對初學者來說。
有時在一些奇怪的字形顯示中,兩種符號是一模一樣的(直豎兩點)。
當然了,有經驗的朋友還是一眼就能分辯兩者。只是,若能更好的避免混亂,又何樂不為呢? ^_^

2, 在多層次的復合替換中,` ` 須要額外的跳脫( \` )處理,而 $( ) 則比較直觀。例如:
這是錯的:
[code]command1 `command2 `command3` `[/code]
原本的意圖是要在 command2 `command3` 先將 command3 提換出來給 command 2 處理,
然後再將結果傳給 command1 `command2 …` 來處理。
然而,真正的結果在命令行中卻是分成了 `command2 ` 與 “ 兩段。
正確的輸入應該如下:
[code]command1 `command2 \`command3\` `[/code]

要不然,換成 $( ) 就沒問題了:
[code]command1 $(command2 $(command3))[/code]
只要你喜歡,做多少層的替換都沒問題啦~~~  ^_^

不過,$( ) 並不是沒有斃端的…
首先,` ` 基本上可用在全部的 unix shell 中使用,若寫成 shell script ,其移植性比較高。
而 $( ) 並不見的每一種 shell 都能使用,我只能跟你說,若你用 bash2 的話,肯定沒問題…  ^_^

接下來,再讓我們看 ${ } 吧… 它其實就是用來作變量替換用的啦。
一般情況下,$var 與 ${var} 並沒有啥不一樣。
但是用 ${ } 會比較精確的界定變量名稱的範圍,比方說:
[code]$ A=B
$ echo $AB

[/code]
原本是打算先將 $A 的結果替換出來,然後再補一個 B 字母於其後,
但在命令行上,真正的結果卻是只會提換變量名稱為 AB 的值出來…
若使用 ${ } 就沒問題了:
[code]$ echo ${A}B
BB[/code]

不過,假如你只看到 ${ } 只能用來界定變量名稱的話,那你就實在太小看 bash 了﹗
有興趣的話,你可先參考一下 cu 本版的精華文章:
http://www.chinaunix.net/forum/viewtopic.php?t=201843

為了完整起見,我這裡再用一些例子加以說明 ${ } 的一些特異功能:
假設我們定義了一個變量為:
file=/dir1/dir2/dir3/my.file.txt
我們可以用 ${ } 分別替換獲得不同的值:
${file#*/}:拿掉第一條 / 及其左邊的字串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最後一條 / 及其左邊的字串:my.file.txt
${file#*.}:拿掉第一個 .  及其左邊的字串:file.txt
${file##*.}:拿掉最後一個 .  及其左邊的字串:txt
${file%/*}:拿掉最後條 / 及其右邊的字串:/dir1/dir2/dir3
${file%%/*}:拿掉第一條 / 及其右邊的字串:(空值)
${file%.*}:拿掉最後一個 .  及其右邊的字串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一個 .  及其右邊的字串:/dir1/dir2/dir3/my
記憶的方法為:
[list]# 是去掉左邊(在鑑盤上 # 在 $ 之左邊)
% 是去掉右邊(在鑑盤上 % 在 $ 之右邊)
單一符號是最小匹配﹔兩個符號是最大匹配。[/list]
${file:0:5}:提取最左邊的 5 個字節:/dir1
${file:5:5}:提取第 5 個字節右邊的連續 5 個字節:/dir2

我們也可以對變量值裡的字串作替換:
${file/dir/path}:將第一個 dir 提換為 path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:將全部 dir 提換為 path:/path1/path2/path3/my.file.txt

利用 ${ } 還可針對不同的變數狀態賦值(沒設定、空值、非空值):
${file-my.file.txt} :假如 $file 沒有設定,則使用 my.file.txt 作傳回值。(空值及非空值時不作處理)
${file:-my.file.txt} :假如 $file 沒有設定或為空值,則使用 my.file.txt 作傳回值。 (非空值時不作處理)
${file+my.file.txt} :假如 $file 設為空值或非空值,均使用 my.file.txt 作傳回值。(沒設定時不作處理)
${file:+my.file.txt} :若 $file 為非空值,則使用 my.file.txt 作傳回值。 (沒設定及空值時不作處理)
${file=my.file.txt} :若 $file 沒設定,則使用 my.file.txt 作傳回值,同時將 $file 賦值為 my.file.txt 。 (空值及非空值時不作處理)
${file:=my.file.txt} :若 $file 沒設定或為空值,則使用 my.file.txt 作傳回值,同時將 $file 賦值為 my.file.txt 。 (非空值時不作處理)
${file?my.file.txt} :若 $file 沒設定,則將 my.file.txt 輸出至 STDERR。 (空值及非空值時不作處理)
${file:?my.file.txt} :若 $file 沒設定或為空值,則將 my.file.txt 輸出至 STDERR。 (非空值時不作處理)

tips:
以上的理解在於, 你一定要分清楚 unset 與 null 及 non-null 這三種賦值狀態.
一般而言, : 與 null 有關, 若不帶 : 的話, null 不受影響, 若帶 : 則連 null 也受影響.

還有哦,${#var} 可計算出變量值的長度:
${#file} 可得到 27 ,因為 /dir1/dir2/dir3/my.file.txt 剛好是 27 個字節…

接下來,再為大家介稍一下 bash 的組數(array)處理方法。
一般而言,A=”a b c def” 這樣的變量只是將 $A 替換為一個單一的字串,
但是改為 A=(a b c def) ,則是將 $A 定義為組數…
bash 的組數替換方法可參考如下方法:
${A[@]} 或 ${A[*]} 可得到 a b c def (全部組數)
${A[0]} 可得到 a (第一個組數),${A[1]} 則為第二個組數…
${#A[@]} 或 ${#A[*]} 可得到 4 (全部組數數量)
${#A[0]} 可得到 1 (即第一個組數(a)的長度),${#A[3]} 可得到 3 (第四個組數(def)的長度)
A[3]=xyz 則是將第四個組數重新定義為 xyz …

諸如此類的….
能夠善用 bash 的 $( ) 與 ${ } 可大大提高及簡化 shell 在變量上的處理能力哦~~~  ^_^

好了,最後為大家介紹 $(( )) 的用途吧:它是用來作整數運算的。
在 bash 中,$(( )) 的整數運算符號大致有這些:
+ - * / :分別為 “加、減、乘、除”。
% :餘數運算
& | ^ !:分別為 “AND、OR、XOR、NOT” 運算。

例:
[code]$ a=5; b=7; c=2
$ echo $(( a+b*c ))
19
$ echo $(( (a+b)/c ))
6
$ echo $(( (a*b)%c))
1[/code]

在 $(( )) 中的變量名稱,可於其前面加 $ 符號來替換,也可以不用,如:
$(( $a + $b * $c)) 也可得到 19 的結果

此外,$(( )) 還可作不同進位(如二進位、八進位、十六進位)作運算呢,只是,輸出結果皆為十進位而已:
echo $((16#2a)) 結果為 42 (16進位轉十進位)
以一個實用的例子來看看吧:
假如當前的  umask 是 022 ,那麼新建文件的權限即為:
[code]$ umask 022
$ echo “obase=8;$(( 8#666 & (8#777 ^ 8#$(umask)) ))” | bc
644[/code]

事實上,單純用 (( )) 也可重定義變量值,或作 testing:
a=5; ((a++)) 可將 $a 重定義為 6
a=5; ((a–)) 則為 a=4
a=5; b=7; ((a < b)) 會得到  0 (true) 的返回值。
常見的用於 (( )) 的測試符號有如下這些:
[list]<:小於
>:大於
<=:小於或等於
>=:大於或等於
==:等於
!=:不等於[/list]
不過,使用 (( )) 作整數測試時,請不要跟 [ ] 的整數測試搞混亂了。(更多的測試我將於第十章為大家介紹)

怎樣?好玩吧..  ^_^  okay,這次暫時說這麼多…
上面的介紹,並沒有詳列每一種可用的狀態,更多的,就請讀者參考手冊文件囉…

如何实现在RHEL下将多个网卡bonding为一个单一通道?

Wednesday, April 9th, 2008

Linux Channel Bonding可以支持把多个网络适配器集合在一起, 当作一个网络适配器来使用。在 Linux 下,网卡的高可用性是通过 MII 或者 ETHTOOL 的状态监测来实现的,所以,需要检查系统中的网络适配器是否支持 MII 或者ETHTOOL 的连状态监测。可以用命令 “ethtool eth0″ 来检查,如果显示的 “Link detected:” 信息与实现的连接状态一致,就没有问题。如果系统中的网络适配器不支持 MII 或者 ETHTOOL 状态监测,当连接失效时,系统就不能检测到,同时,在 bonding 驱动加载时,会记录一条不支持 MII 和 ETHTOOL 的警告信息。

环境: Red Hat Enterprise Linux 4 Update1  

 

解决方法:

RHEL支持在内核级实现将多个物理网卡帮定为一个逻辑bonding设备,通过把多个物理网卡帮定为一个逻辑设备,可以实现增加带宽吞吐量,提供冗余。

为了创建一个bonding设备,在/etc/sysconfig/network-scripts/下创建文件ifcfg-bond<N>,N是一个数字,例如0。

该文件内容对不同类型网卡的帮定都是一样的,例如以太设备。唯一的区别是DEVICE=应该是bond<N>,下面是一个示例文件:

DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
NETWORK=10.0.1.0
NETMASK=255.255.255.0
IPADDR=10.0.1.27
USERCTL=no

一旦创建了bonding接口,bonding设备包含的网卡设备必须进行配置,在配置文件中添加到MASTER=和SLAVE=,所有网卡的配置文件都是类似的,例如一个bonding设备帮定了两个网卡,eth0和eth1的配置文件可能如下:

DEVICE=eth<N>
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no

在本例中,把<N>替换为网卡编号。

为了激活bonding设备,必须加载内核模块,在RHEL4和RHEL3下加载模块的过程稍有不同。

RHEL4

为了确保在bonding设备在启动时模块被加载,在/etc/modprobe.conf中添加如下内容:

install bond<N> /sbin/modprobe bonding -o bond<N> miimon=100 mode=0

替换<N>为设备编号,例如0。

miimon 是指多久时间检查网络一次,单位是ms(毫秒),其意义是假设其中有一条网络断线,会在0.1秒内自动备援

 mode 共有七种模式(0~6)
 mode=0:负载均衡模式,有自动备援,但需要”Switch”支持和设定。
 mode=1:自动备援模式,其中一条线若断线,其他线路将会自动备援。
 mode=6:负载均衡模式,有自动备援,不需要”Switch”支持和设定。

如果还要使用互备模式,需要加上参数primary=ethx

对于每个虚拟bonding设备,在/etc/modprobe.conf中都要有对应行。需要注意的是在RHEL4U2之前的版本,不支持多个bonding设备。如果需要配置多个bonding设备,则需要升级内核到 RHEL4U2。

一旦/etc/modprobe.conf被配置,bonding虚拟设备配置文件以及网卡配置文件配置完毕,ifup命令将用来启动bonding虚拟设备,或者通过重新起动网络。

linux下解决大量的TIME_WAIT

Wednesday, April 9th, 2008

 vi /etc/sysctl.conf
新增如下内容:
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies=1
使内核参数生效:
[root@web02 ~]# sysctl -p
readme:
net.ipv4.tcp_syncookies=1 打开TIME-WAIT套接字重用功能,对于存在大量连接的Web服务器非常有效。
net.ipv4.tcp_tw_recyle=1
net.ipv4.tcp_tw_reuse=1 减少处于FIN-WAIT-2连接状态的时间,使系统可以处理更多的连接。
net.ipv4.tcp_fin_timeout=30 减少TCP KeepAlive连接侦测的时间,使系统可以处理更多的连接。
net.ipv4.tcp_keepalive_time=1800 增加TCP SYN队列长度,使系统可以处理更多的并发连接。
net.ipv4.tcp_max_syn_backlog=8192

找到一篇sed强文:sed1line

Saturday, March 22nd, 2008

HANDY ONE-LINERS FOR SED (Unix stream editor)               Mar. 23, 2001
compiled by Eric Pement <pemente@northpark.edu>               version 5.1
Latest version of this file is usually at:
   http://www.student.northpark.edu/pemente/sed/sed1line.txt
   http://www.cornerstonemag.com/sed/sed1line.txt
This file is also available in Portuguese at:
   http://www.lrv.ufsc.br/wmaker/sed_ptBR.html

FILE SPACING:

 # double space a file
 sed G

 # double space a file which already has blank lines in it. Output file
 # should contain no more than one blank line between lines of text.
 sed ’/^$/d;G’

 # triple space a file
 sed ’G;G’

 # undo double-spacing (assumes even-numbered lines are always blank)
 sed ’n;d’

NUMBERING:

 # number each line of a file (simple left alignment). Using a tab (see
 # note on ’\t’ at end of file) instead of space will preserve margins.
 sed = filename | sed ’N;s/\n/\t/’

 # number each line of a file (number on left, right-aligned)
 sed = filename | sed ’N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /’

 # number each line of file, but only print numbers if line is not blank
 sed ’/./=’ filename | sed ’/./N; s/\n/ /’

 # count lines (emulates ”wc -l”)
 sed -n ’$=’

TEXT CONVERSION AND SUBSTITUTION:

 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
 sed ’s/.$//’               # assumes that all lines end with CR/LF
 sed ’s/^M$//’              # in bash/tcsh, press Ctrl-V then Ctrl-M
 sed ’s/\x0D$//’            # gsed 3.02.80, but top script is easier

 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
 sed ”s/$/`echo -e \\\r`/”            # command line under ksh
 sed ’s/$’”/`echo \\\r`/”             # command line under bash
 sed ”s/$/`echo \\\r`/”               # command line under zsh
 sed ’s/$/\r/’                        # gsed 3.02.80

 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
 sed ”s/$//”                          # method 1
 sed -n p                             # method 2

 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
 # Cannot be done with DOS versions of sed. Use ”tr” instead.
 tr -d \r <infile >outfile            # GNU tr version 1.22 or higher

 # delete leading whitespace (spaces, tabs) from front of each line
 # aligns all text flush left
 sed ’s/^[ \t]*//’                    # see note on ’\t’ at end of file

 # delete trailing whitespace (spaces, tabs) from end of each line
 sed ’s/[ \t]*$//’                    # see note on ’\t’ at end of file

 # delete BOTH leading and trailing whitespace from each line
 sed ’s/^[ \t]*//;s/[ \t]*$//’

 # insert 5 blank spaces at beginning of each line (make page offset)
 sed ’s/^/     /’

 # align all text flush right on a 79-column width
 sed -e :a -e ’s/^.\{1,78\}$/ &/;ta’  # set at 78 plus 1 space

 # center all text in the middle of 79-column width. In method 1,
 # spaces at the beginning of the line are significant, and trailing
 # spaces are appended at the end of the line. In method 2, spaces at
 # the beginning of the line are discarded in centering the line, and
 # no trailing spaces appear at the end of lines.
 sed  -e :a -e ’s/^.\{1,77\}$/ & /;ta’                     # method 1
 sed  -e :a -e ’s/^.\{1,77\}$/ &/;ta’ -e ’s/\( *\)\1/\1/’  # method 2

 # substitute (find and replace) ”foo” with ”bar” on each line
 sed ’s/foo/bar/’             # replaces only 1st instance in a line
 sed ’s/foo/bar/4′            # replaces only 4th instance in a line
 sed ’s/foo/bar/g’            # replaces ALL instances in a line
 sed ’s/\(.*\)foo\(.*foo\)/\1bar\2/’ # replace the next-to-last case
 sed ’s/\(.*\)foo/\1bar/’            # replace only the last case

 # substitute ”foo” with ”bar” ONLY for lines which contain ”baz”
 sed ’/baz/s/foo/bar/g’

 # substitute ”foo” with ”bar” EXCEPT for lines which contain ”baz”
 sed ’/baz/!s/foo/bar/g’

 # change ”scarlet” or ”ruby” or ”puce” to ”red”
 sed ’s/scarlet/red/g;s/ruby/red/g;s/puce/red/g’   # most seds
 gsed ’s/scarlet\|ruby\|puce/red/g’                # GNU sed only

 # reverse order of lines (emulates ”tac”)
 # bug/feature in HHsed v1.5 causes blank lines to be deleted
 sed ’1!G;h;$!d’               # method 1
 sed -n ’1!G;h;$p’             # method 2

 # reverse each character on the line (emulates ”rev”)
 sed ’/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//’

 # join pairs of lines side-by-side (like ”paste”)
 sed ’$!N;s/\n/ /’

 # if a line ends with a backslash, append the next line to it
 sed -e :a -e ’/\\$/N; s/\\\n//; ta’

 # if a line begins with an equal sign, append it to the previous line
 # and replace the ”=” with a single space
 sed -e :a -e ’$!N;s/\n=/ /;ta’ -e ’P;D’

 # add commas to numeric strings, changing ”1234567″ to ”1,234,567″
 gsed ’:a;s/\B[0-9]\{3\}\>/,&/;ta’                     # GNU sed
 sed -e :a -e ’s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta’  # other seds

 # add commas to numbers with decimal points and minus signs (GNU sed)
 gsed ’:a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta’

 # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
 gsed ’0~5G’                  # GNU sed only
 sed ’n;n;n;n;G;’             # other seds

SELECTIVE PRINTING OF CERTAIN LINES:

 # print first 10 lines of file (emulates behavior of ”head”)
 sed 10q

 # print first line of file (emulates ”head -1″)
 sed q

 # print the last 10 lines of a file (emulates ”tail”)
 sed -e :a -e ’$q;N;11,$D;ba’

 # print the last 2 lines of a file (emulates ”tail -2″)
 sed ’$!N;$!D’

 # print the last line of a file (emulates ”tail -1″)
 sed ’$!d’                    # method 1
 sed -n ’$p’                  # method 2

 # print only lines which match regular expression (emulates ”grep”)
 sed -n ’/regexp/p’           # method 1
 sed ’/regexp/!d’             # method 2

 # print only lines which do NOT match regexp (emulates ”grep -v”)
 sed -n ’/regexp/!p’          # method 1, corresponds to above
 sed ’/regexp/d’              # method 2, simpler syntax

 # print the line immediately before a regexp, but not the line
 # containing the regexp
 sed -n ’/regexp/{g;1!p;};h’

 # print the line immediately after a regexp, but not the line
 # containing the regexp
 sed -n ’/regexp/{n;p;}’

 # print 1 line of context before and after regexp, with line number
 # indicating where the regexp occurred (similar to ”grep -A1 -B1″)
 sed -n -e ’/regexp/{=;x;1!p;g;$!N;p;D;}’ -e h

 # grep for AAA and BBB and CCC (in any order)
 sed ’/AAA/!d; /BBB/!d; /CCC/!d’

 # grep for AAA and BBB and CCC (in that order)
 sed ’/AAA.*BBB.*CCC/!d’

 # grep for AAA or BBB or CCC (emulates ”egrep”)
 sed -e ’/AAA/b’ -e ’/BBB/b’ -e ’/CCC/b’ -e d    # most seds
 gsed ’/AAA\|BBB\|CCC/!d’                        # GNU sed only

 # print paragraph if it contains AAA (blank lines separate paragraphs)
 # HHsed v1.5 must insert a ’G;’ after ’x;’ in the next 3 scripts below
 sed -e ’/./{H;$!d;}’ -e ’x;/AAA/!d;’

 # print paragraph if it contains AAA and BBB and CCC (in any order)
 sed -e ’/./{H;$!d;}’ -e ’x;/AAA/!d;/BBB/!d;/CCC/!d’

 # print paragraph if it contains AAA or BBB or CCC
 sed -e ’/./{H;$!d;}’ -e ’x;/AAA/b’ -e ’/BBB/b’ -e ’/CCC/b’ -e d
 gsed ’/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d’         # GNU sed only

 # print only lines of 65 characters or longer
 sed -n ’/^.\{65\}/p’

 # print only lines of less than 65 characters
 sed -n ’/^.\{65\}/!p’        # method 1, corresponds to above
 sed ’/^.\{65\}/d’            # method 2, simpler syntax

 # print section of file from regular expression to end of file
 sed -n ’/regexp/,$p’

 # print section of file based on line numbers (lines 8-12, inclusive)
 sed -n ’8,12p’               # method 1
 sed ’8,12!d’                 # method 2

 # print line number 52
 sed -n ’52p’                 # method 1
 sed ’52!d’                   # method 2
 sed ’52q;d’                  # method 3, efficient on large files

 # beginning at line 3, print every 7th line
 gsed -n ’3~7p’               # GNU sed only
 sed -n ’3,${p;n;n;n;n;n;n;}’ # other seds

 # print section of file between two regular expressions (inclusive)
 sed -n ’/Iowa/,/Montana/p’             # case sensitive

SELECTIVE DELETION OF CERTAIN LINES:

 # print all of file EXCEPT section between 2 regular expressions
 sed ’/Iowa/,/Montana/d’

 # delete duplicate, consecutive lines from a file (emulates ”uniq”).
 # First line in a set of duplicate lines is kept, rest are deleted.
 sed ’$!N; /^\(.*\)\n\1$/!P; D’

 # delete duplicate, nonconsecutive lines from a file. Beware not to
 # overflow the buffer size of the hold space, or else use GNU sed.
 sed -n ’G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P’

 # delete the first 10 lines of a file
 sed ’1,10d’

 # delete the last line of a file
 sed ’$d’

 # delete the last 2 lines of a file
 sed ’N;$!P;$!D;$d’

 # delete the last 10 lines of a file
 sed -e :a -e ’$d;N;2,10ba’ -e ’P;D’   # method 1
 sed -n -e :a -e ’1,10!{P;N;D;};N;ba’  # method 2

 # delete every 8th line
 gsed ’0~8d’                           # GNU sed only
 sed ’n;n;n;n;n;n;n;d;’                # other seds

 # delete ALL blank lines from a file (same as ”grep ’.' ”)
 sed ’/^$/d’                           # method 1
 sed ’/./!d’                           # method 2

 # delete all CONSECUTIVE blank lines from file except the first; also
 # deletes all blank lines from top and end of file (emulates ”cat -s”)
 sed ’/./,/^$/!d’          # method 1, allows 0 blanks at top, 1 at EOF
 sed ’/^$/N;/\n$/D’        # method 2, allows 1 blank at top, 0 at EOF

 # delete all CONSECUTIVE blank lines from file except the first 2:
 sed ’/^$/N;/\n$/N;//D’

 # delete all leading blank lines at top of file
 sed ’/./,$!d’

 # delete all trailing blank lines at end of file
 sed -e :a -e ’/^\n*$/{$d;N;ba’ -e ’}'  # works on all seds
 sed -e :a -e ’/^\n*$/N;/\n$/ba’        # ditto, except for gsed 3.02*

 # delete the last line of each paragraph
 sed -n ’/^$/{p;h;};/./{x;/./p;}’

SPECIAL APPLICATIONS:

 # remove nroff overstrikes (char, backspace) from man pages. The ’echo’
 # command may need an -e switch if you use Unix System V or bash shell.
 sed ”s/.`echo \\\b`//g”    # double quotes required for Unix environment
 sed ’s/.^H//g’             # in bash/tcsh, press Ctrl-V and then Ctrl-H
 sed ’s/.\x08//g’           # hex expression for sed v1.5

 # get Usenet/e-mail message header
 sed ’/^$/q’                # deletes everything after first blank line

 # get Usenet/e-mail message body
 sed ’1,/^$/d’              # deletes everything up to first blank line

 # get Subject header, but remove initial ”Subject: ” portion
 sed ’/^Subject: */!d; s///;q’

 # get return address header
 sed ’/^Reply-To:/q; /^From:/h; /./d;g;q’

 # parse out the address proper. Pulls out the e-mail address by itself
 # from the 1-line return address header (see preceding script)
 sed ’s/ *(.*)//; s/>.*//; s/.*[:<] *//’

 # add a leading angle bracket and space to each line (quote a message)
 sed ’s/^/> /’

 # delete leading angle bracket & space from each line (unquote a message)
 sed ’s/^> //’

 # remove most HTML tags (accommodates multiple-line tags)
 sed -e :a -e ’s/<[^>]*>//g;/</N;//ba’

 # extract multi-part uuencoded binaries, removing extraneous header
 # info, so that only the uuencoded portion remains. Files passed to
 # sed must be passed in the proper order. Version 1 can be entered
 # from the command line; version 2 can be made into an executable
 # Unix shell script. (Modified from a script by Rahul Dhesi.)
 sed ’/^end/,/^begin/d’ file1 file2 … fileX | uudecode   # vers. 1
 sed ’/^end/,/^begin/d’ ”$@” | uudecode                    # vers. 2

 # zip up each .TXT file individually, deleting the source file and
 # setting the name of each .ZIP file to the basename of the .TXT file
 # (under DOS: the ”dir /b” switch returns bare filenames in all caps).
 echo @echo off >zipup.bat
 dir /b *.txt | sed ”s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/” >>zipup.bat

TYPICAL USE: Sed takes one or more editing commands and applies all of
them, in sequence, to each line of input. After all the commands have
been applied to the first input line, that line is output and a second
input line is taken for processing, and the cycle repeats. The
preceding examples assume that input comes from the standard input
device (i.e, the console, normally this will be piped input). One or
more filenames can be appended to the command line if the input does
not come from stdin. Output is sent to stdout (the screen). Thus:

 cat filename | sed ’10q’        # uses piped input
 sed ’10q’ filename              # same effect, avoids a useless ”cat”
 sed ’10q’ filename > newfile    # redirects output to disk

For additional syntax instructions, including the way to apply editing
commands from a disk file instead of the command line, consult ”sed &
awk, 2nd Edition,” by Dale Dougherty and Arnold Robbins (O’Reilly,
1997; http://www.ora.com), ”UNIX Text Processing,” by Dale Dougherty
and Tim O’Reilly (Hayden Books, 1987) or the tutorials by Mike Arst
distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power
of sed, one must understand ”regular expressions.” For this, see
“Mastering Regular Expressions” by Jeffrey Friedl (O’Reilly, 1997).
The manual (”man”) pages on Unix systems may be helpful (try ”man
sed”, ”man regexp”, or the subsection on regular expressions in ”man
ed”), but man pages are notoriously difficult. They are not written to
teach sed use or regexps to first-time users, but as a reference text
for those already acquainted with these tools.

QUOTING SYNTAX: The preceding examples use single quotes (’…’)
instead of double quotes (”…”) to enclose editing commands, since
sed is typically used on a Unix platform. Single quotes prevent the
Unix shell from intrepreting the dollar sign ($) and backquotes
(`…`), which are expanded by the shell if they are enclosed in
double quotes. Users of the ”csh” shell and derivatives will also need
to quote the exclamation mark (!) with the backslash (i.e., \!) to
properly run the examples listed above, even within single quotes.
Versions of sed written for DOS invariably require double quotes
(”…”) instead of single quotes to enclose editing commands.

USE OF ’\t’ IN SED SCRIPTS: For clarity in documentation, we have used
the expression ’\t’ to indicate a tab character (0×09) in the scripts.
However, most versions of sed do not recognize the ’\t’ abbreviation,
so when typing these scripts from the command line, you should press
the TAB key instead. ’\t’ is supported as a regular expression
metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.

VERSIONS OF SED: Versions of sed do differ, and some slight syntax
variation is to be expected. In particular, most do not support the
use of labels (:name) or branch instructions (b,t) within editing
commands, except at the end of those commands. We have used the syntax
which will be portable to most users of sed, even though the popular
GNU versions of sed allow a more succinct syntax. When the reader sees
a fairly long command such as this:

   sed -e ’/AAA/b’ -e ’/BBB/b’ -e ’/CCC/b’ -e d

it is heartening to know that GNU sed will let you reduce it to:

   sed ’/AAA/b;/BBB/b;/CCC/b;d’      # or even
   sed ’/AAA\|BBB\|CCC/b;d’

In addition, remember that while many versions of sed accept a command
like ”/one/ s/RE1/RE2/”, some do NOT allow ”/one/! s/RE1/RE2/”, which
contains space before the ’s’. Omit the space when typing the command.

OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to
large input files or slow processors or hard disks), substitution will
be executed more quickly if the ”find” expression is specified before
giving the ”s/…/…/” instruction. Thus:

   sed ’s/foo/bar/g’ filename         # standard replace command
   sed ’/foo/ s/foo/bar/g’ filename   # executes more quickly
   sed ’/foo/ s//bar/g’ filename      # shorthand sed syntax

On line selection or deletion in which you only need to output lines
from the first part of the file, a ”quit” command (q) in the script
will drastically reduce processing time for large files. Thus:

   sed -n ’45,50p’ filename           # print line nos. 45-50 of a file
   sed -n ’51q;45,50p’ filename       # same, but executes much faster

If you have any additional scripts to contribute or if you find errors
in this document, please send e-mail to the compiler. Indicate the
version of sed you used, the operating system it was compiled for, and
the nature of the problem. Various scripts in this file were written
or contributed by:

 Al Aab <af137@freenet.toronto.on.ca>   # ”seders” list moderator
 Edgar Allen <era@sky.net>              # various
 Yiorgos Adamopoulos <adamo@softlab.ece.ntua.gr>
 Dale Dougherty <dale@songline.com>     # author of ”sed & awk”
 Carlos Duarte <cdua@algos.inesc.pt>    # author of ”do it with sed”
 Eric Pement <pemente@northpark.edu>    # author of this document
 Ken Pizzini <ken@halcyon.com>          # author of GNU sed v3.02
 S.G. Ravenhall <stew.ravenhall@totalise.co.uk> # great de-html script
 Greg Ubben <gsu@romulus.ncsc.mil>      # many contributions & much help

Mysql备份脚本 带压缩 显示压缩率 日志

Friday, March 21st, 2008

#!/bin/sh
#
# Copyright (C), 2007 bug All Rights Reserved

# Title       : Database Backup Script
# Author      : BUG
# File        : db_bak.sh
# Version     : 1.1.0
# Date        : 2008-03-21
# Email       : efbbb27115@homtail.com
# License     : General Public License (GPL) v2
# Description : Database Backup Script
#

initial() {

    echo -en “\33[2J”

    echo -en “\33[0;0H”

    echo -en “\33[32m”

    echo “————————————————————”

    echo “—                Database Backup Script                —”

    echo “————————————————————”

    echo -en “\33[37m”

    stty -echo

    umask 077

    mysqln=`echo ’show databases;’ | /usr/local/mysql/bin/mysql -psjldafhkg | sed -n ‘3,$p’`

    mysqlu=“root”

    mysqlp=“********”

    binpath=“/usr/local/mysql/bin”

    dstpath=“/pri/bak/sql_bak/”

    runuser=`ps aux | grep $$ | sed -n “1p” | awk ‘{ print $1 }’`

    mkdir -p $dstpath/`date +%y%m%d`

    exec 3>>$dstpath/`date +%y%m%d`/db_bak.log

}initialecho -e “\n[`date +%y/%m/%d\ %H:%M:%S`] Database Backup Script start by $runuser PID:$$” >&3if [ -f “$dstpath/`date +%y%m%d`/lock” ]

then

    echo -e “\33[33mIt’s has completed by `cat $dstpath/\`date +%y%m%d\`/lock`!!!\33[37m”

    echo “[`date +%y/%m/%d\ %H:%M:%S`] It’s has completed by `cat $dstpath/\`date +%y%m%d\`/lock`!!!” >&3

else

    echo -e “Database\tExport\tCompress\tSql\tTgz\tRate”

    echo -e “\33[32m————————————————————\33[37m”

    cd $dstpath/`date +%y%m%d`

    for db in $mysqln

    do

    echo -n “$db”

    $binpath/mysqldump --opt -u$mysqlu -p$mysqlp $db > $dstpath/`date +%y%m%d`/$db.sql 2> /dev/null

    RETVAL1=$?

    if [ $RETVAL1 -eq 0 ]

    then

    sqlsize=`du -k $db.sql | cut -f1`

    echo -n “[`date +%y/%m/%d\ %H:%M:%S`]” >&3

    echo -n “$db” | awk ‘{printf ” %-17s”, $1}’ >&3

    echo -n “OK  ” >&3

    echo -en “\r\t\t  \33[32mOK\33[37m\t”

    tar -czvf $db.tgz $db.sql > /dev/null 2>&1

        RETVAL2=$?

        if [ $RETVAL2 -eq 0 ]

        then

        tgzsize=`du -k $db.tgz | cut -f1`

        echo “OK” >&3

        rm -f $dstpath/`date +%y%m%d`/$db.sql

        echo -en “   \33[32mOK\33[37m”

        echo -en “\33[33m”

        echo -en “$sqlsize” | awk ‘{printf “%12dKB”, $1}’

        echo -en “$tgzsize” | awk ‘{printf “%6sKB”, $1}’

        echo “$sqlsize $tgzsize” | awk ‘{printf “%8.0f%”,$2/$1*100}’

        echo -e “\33[37m”

        else

        echo “Compress fail return $RETVAL2″ >&3

        echo -e “  \33[31mfail\33[37m”

        fi

    else

    echo -n “[`date +%y/%m/%d\ %H:%M:%S`]” >&3

    echo -n “$db” | awk ‘{printf ” %-17s “, $1}’ >&3

    echo “Export fail return $RETVAL1″ >&3

    echo -en “\r\t\t   \33[31mfail\33[37m”

    fi

    done

    echo “`date +%H:%M:%S`”>$dstpath/`date +%y%m%d`/lock

fi