2005年04月26日

oracle数据库的备份和恢复


++++++++++++++++++++++++++++++++++++++++++++++++++++++
备份: export.sh
++++++++++++++++++++++++++++++++++++++++++++++++++++++

#!/bin/sh

BASH_ENV=$HOME/.bashrc; export BASH_ENV;
ORAENV_ASK=NO; export ORAENV_ASK;
ORACLE_SID=ora; export ORACLE_SID;
ORACLE_HOME=/home/oracle/9.0; export ORACLE_HOME;
PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin; export PATH;
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib; export LD_LIBRARY_PATH;
LANG=ZH_en; export LANG;
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK; export NLS_LANG;

BAKUP_PATH=/u/oradata/bak/
BAKUP_LOG_PATH=/u/oradata/bak/log/
TODAY=`date +%m`
BAKUP_DATA_FILE=$BAKUP_PATH$TODAY.dmp
[ -f $BAKUP_DATA_FILE ] && rm -f $BAKUP_DATA_FILE
BAKUP_LOG_FILE=$BAKUP_LOG_PATH$TODAY.log
[ -f $BAKUP_LOG_FILE ] && rm -f $BAKUP_LOG_FILE


USER=test
PASS=test
SERID=ORA

exp $USER/$PASS@$SERID file=$BAKUP_DATA_FILE owner=radius log=$BAKUP_LOG_FILE


++++++++++++++++++++++++++++++++++++++++++++++++++++++
恢复: import.sh
++++++++++++++++++++++++++++++++++++++++++++++++++++++


#!/bin/sh

BASH_ENV=$HOME/.bashrc;
export BASH_ENV;

ORAENV_ASK=NO;
export ORAENV_ASK;

ORACLE_SID=ora;
export ORACLE_SID;

ORACLE_HOME=/home/oracle/9.0;
export ORACLE_HOME;

PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin;
export PATH;

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib;
export LD_LIBRARY_PATH;

LANG=ZH_en;
export LANG;

NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;
export NLS_LANG;

USER=test
PASS=test
SERID=ORA

imp $USER/$PASS@$SERID  FULL=Y FILE=/tmp/20050401044200.dmp

2005年03月26日

cho2 是个好东西。尤其对于我们这种不喜欢做过多的界面开发,但又希望有较强的rich client的表现力.

可以研究一把,下面是整理的相关资源,感兴趣的朋友可以去看一下。

project: echo2

官方站点:

http://www.nextapp.com/products/echo2

介绍:
  http://www.theserverside.com/news/thread.tss?thread_id=32834


demo link:
  http://demo.nextapp.com/InteractiveTest/ia?debug
  http://www.nextapp.com/products/echo/demo/

api download:
    at here ,it includes source code,documents and any testes,examples
 
  http://www.nextapp.com/products/echo2/download/

2005年03月23日

夜阑风声,宿雨新停,休而不眠,起而后作。

雨后无痕疑空水,
只影对孤灯,
冰壶冷彻,
红尘俱静,
万象清虚。

明年何地,
他乡客里,
不逾归期,
婵娟千里,
天涯处处,
皆是吾庐。

2005年03月22日

教训一;代码审查

    现在才知道代码审查的重要性,以前工作太随意了。尤其对于一个管理者来说,必须具有原则性。
中庸之道并不适合于工程管理之中。不能将软件质量寄希望与个人的能力和职业道德之上。

CASE:
    去年十二月份。由于项目紧张,未详细考察,让一位新来的仁兄负责一个地区的代码开发工作,
    由于是远程开发,未及时将所开发代码提交至cvs 库中。所以也没有注意这位仁兄的水平如何。
    年前项目完成,代码提交时发现所写的代码质量实在太差了。由于新年来到。时间紧张,
    当时和他指出,希望能进行改正。当时也答应了。可是新年过后,这位仁兄已逃之夭夭。
    新来的同事叫苦不迭,影响颇大。
   

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   
教训二:分离通用部发和个性化需求

    随着项目的逐步前进。个性化需求会越来越多,尤其是象我们这种和工程相关的程序来说,尤其如此。
    这种预期原来就想到,但一直未做具体操作。
   
CASE:

    随着我们程序施工地区不段增多,个性化越来越强,cvs版本控制难度增加,针对不同地区的个性化,
必须重新定义开发过程:
  1. 要将一些通用的部分加以提升,作为全局代码,不再现场开发。
     这部分代码应该可读不可写,如果需要进行调整,现场开发人员可以提出申请,论证后进行调整。
  2. 个性化较强的部分抽出来,由现场开发人员自己开发。

2005年03月19日

“一扇幸福之门向你关闭的同时,另一扇幸福之门却在你面前敞开了;但是我们总是为关上的门的恋恋不舍,却对另一个开启的门视而不见“
   —————— 海伦.凯勒

节自《CMM实施与软件过程改进》第二章:不段改进组织文化


挺有意思的言论,文章中引用的意思可能是说对软件过程的改进的期望结果和现在的潜在意识往往存在着差异性。

但我觉得前一句倒是更有哲学思辨色彩。门闭从另一个方向理解即是门开。也就失即是得的意思了。
但我们常人会不会这样理解呢。现实的功利欲望往往会蒙蔽我们的心灵。

想起一个故事:
 

惠子对庄子说:“你的言论没有用处。”庄子说:“懂得没有用处方才能够跟他谈论有用。大地不能不说是既广且大了,人所用的只是脚能踩踏的一小块罢了。既然如此,那么只留下脚踩踏的一小块其余全都挖掉,一直挖到黄泉,大地对人来说还有用吗?”惠子说:“当然没有用处。”庄子说:“如此说来,没有用处的用处也就很明白了

2005年03月18日

用easymock来解耦测试中的依赖关系

分析:

DAO层:
interface : CustomerDAO
implements: CustomerDAOHibernate extends BaseDAOHibernate

service层
interface : CustomerManager
implements: CustomerManagerImpl

问题域:

 CustomerManagerImpl 里包含一个对CustomerDAO的引用。即它依赖于CustomerDAO的行为,
也就是说这两者发生耦合,导致在在单元测试里无法单一测试这个类的代码正确。

解决方法参考自:

 http://www-106.ibm.com/developerworks/cn/java/j-mocktest/
 

代码:

[CustomerDAO ]

package com.ts.persistence;

import com.ts.model.Customer;
import com.ts.persistence.DAO;
import java.util.List;

public interface CustomerDAO extends DAO{

    public List getCustomeres( ) throws DAOException ;
    public Customer getCustomer(String name ) throws DAOException ;
    public void saveCustomer(Customer customer) throws DAOException ;
    public void removeCustomer(Long id ) throws DAOException ;
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

[CustomerDAOHibernate]

package com.ts.persistence.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.ts.persistence.hibernate.BaseDAOHibernate;
import com.ts.model.Customer;
import com.ts.persistence.CustomerDAO;
import com.ts.persistence.DAOException;
import java.util.List;

public class CustomerDAOHibernate extends BaseDAOHibernate implements CustomerDAO{

    public List getCustomeres( ) throws DAOException {
        return getHibernateTemplate().find("from Customer");
    }

    public Customer getCustomer(String name) throws DAOException {
        Customer customer = null;
        List customeres = getHibernateTemplate().find(" from Customer C where C.name=?",name);
         if((customeres !=null) &&(customeres.size() > 0)){
            customer = (Customer)customeres.get(0);
        }

         if(customer == null) {
             log.warn("uh oh,Customer object not found …");
             throw new DAOException("Customer:"+ name + " not found in database!");
        }

        return customer;

    }

    public void saveCustomer( Customer customer) throws DAOException  {
          getHibernateTemplate().saveOrUpdate(customer);
    }

    public void removeCustomer(Long id ){
        Object customer = getHibernateTemplate().load(Customer.class, id);
          getHibernateTemplate().delete(customer);
    }

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[CustomerManager]

package com.ts.service;

import java.util.List;

import com.ts.model.Customer;

/**
 * Business Delegate (Proxy) Interface to handle communication between web and
 * persistence layer.
 *
 * <p>
 * <a href="CustomerManager.java.html"><i>View _fcksavedurl=""CustomerManager.java.html"><i>View" Source</i></a>
 * </p>
 *
 * @author <a href="mailto:clxnj@vip.sina.com">clxnj</a>
 * @version $Revision: 1.6 $ $Date: 2004/05/25 06:27:20 $
 */
public interface CustomerManager {
    //~ Methods ================================================================

    /**
     * Retrieves a Customer by name.  An exception is thrown if now Customer
     * is not found.
     *
     * @param name
     * @return customer
     * @throws ServiceException
     */
    public Customer getCustomer(String name) throws ServiceException;

    /**
     * Retrieves a list of Customers
     * @return List
     * @throws ServiceException
     */
    public List getCustomeres() throws ServiceException;

    /**
     * Saves a Customer’s information
     *
     * @param Customer the user’s information
     * @return updated Customer information
     * @throws ServiceException
     */
    public void saveCustomer(Customer customer) throws ServiceException;

    /**
     * Removes a Customer from the database by their name
     *
     * @param id the Customer’s id
     * @throws ServiceException
     */
    public void removeCustomer(Long id) throws ServiceException;

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[CustomerManagerImpl]

package com.ts.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ts.model.Customer;
import com.ts.persistence.DAOException;
import com.ts.persistence.CustomerDAO;

//import com.sun.rsasign.d;

/**
 * Implementation of CustomerManager interface.  This basically transforms POJOs ->
 * Forms and back again.
 * <p/>
 * <p/>
 * <a href="CustomerManagerImpl.java.html"><i>View _fcksavedurl=""CustomerManagerImpl.java.html"><i>View" Source</i></a>
 * </p>
 *
 * @author <a href="mailto:clxnj@vip.sina.com">chen.l.x</a>
 * @version $Revision: 1.8 $ $Date: 2004/05/25 06:27:20 $
 */
public class CustomerManagerImpl extends BaseManager implements CustomerManager {
    private Log log = LogFactory.getLog(CustomerManagerImpl.class);
    private CustomerDAO dao;

   /*
    public CustomerManagerImpl(CustomerDAO dao) {
        this.dao = dao;
    }
    */

    /**
     * Set the DAO for communication with the data layer.
     * @param dao
     */
    public void setCustomerDAO(CustomerDAO dao) {
        this.dao = dao;
    }

    /**
     * @see org.appfuse.service.CustomerManager#getCustomer(java.lang.String)
     */
    public Customer getCustomer(String name) throws ServiceException {
        try {
         return dao.getCustomer(name);
        } catch (DAOException d) {
         throw new ServiceException(d.getMessage(), d);
        }
    }

    /**
     * @see org.appfuse.service.CustomerManager#getCustomers()
     */
    public List getCustomeres() throws ServiceException{
      try {
        return dao.getCustomeres();
      } catch (DAOException d) {
                throw new ServiceException(d.getMessage(), d);
        }
    }

    /**
     * @see org.appfuse.service.CustomerManager#saveCustomer(java.lang.Customer)
     */
    public void saveCustomer(Customer customer) throws ServiceException {
        try {
         dao.saveCustomer(customer);
               // customer = dao.getCustomer(customer.getName());
               // return customer;
        } catch (DAOException d) {
         throw new ServiceException(d.getMessage(), d);
        }
    }

    /**
     * @see org.appfuse.service.ustomerManager#removeustomer(java.lang.String)
     */
    public void removeCustomer(Long id) throws ServiceException {

        if (log.isDebugEnabled()) {
            log.debug("removing customer: " + id);
        }

        try {
         dao.removeCustomer(id);
        } catch (DAOException d) {
            throw new ServiceException(d.getMessage(), d);
        }
    }

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//用mock object 来测试

[CustomerManagerImplTest]

package com.ts.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ts.Constants;
import com.ts.model.Customer;
import com.ts.persistence.CustomerDAO;

import org.easymock.MockControl;
import java.util.List;
import java.util.LinkedList;

public class CustomerManagerImplTest
    extends BaseManagerTestCase {
  //~ Instance fields ========================================================

  private CustomerManagerImpl mgr ;
  private Log log = LogFactory.getLog(CustomerManagerImplTest.class);
  private Customer customer;
  private MockControl control;
  private CustomerDAO customerDAO;

  //~ Methods ================================================================

  protected void setUp() throws Exception {
    //now i used mock Object
    control = MockControl.createControl(CustomerDAO.class);
    customerDAO = (CustomerDAO) control.getMock();

    mgr = new CustomerManagerImpl();
    mgr.setCustomerDAO(customerDAO);
  }

  protected void tearDown() {
    mgr = null;
  }

  public void testSaveCustomer() throws Exception {
    Customer customer = new Customer();
    customer.setName("test");

    mgr.saveCustomer(customer);
    control.setVoidCallable(); //void ,not return anything,so call this

    control.replay();
    mgr.saveCustomer(customer);
    control.verify();
  }

  public void testGetCustomer() throws Exception {

    //init mock paramter and return value
    String name = "clxnj";
    Customer customer = new Customer();
    customer.setName(name);
    customer.setAddress("tssoft");

    //set mock paramter and return value
    mgr.getCustomer(name);
    control.setReturnValue(customer);

    //now starting test …
    control.replay();
    Customer customerNew = mgr.getCustomer(name);
    log.info(customerNew);
    assertEquals(customer.getName(), customerNew.getName());
    assertEquals(customer.getName(), customerNew.getName());
    control.verify();
  }

  public void testGetCustomeres() throws Exception {

    //init mock paramter and return value
    List list = new LinkedList();

    Customer customerOne = new Customer();
    customerOne.setName("one");
    customerOne.setAddress("tssoft1");

    Customer customerTwo = new Customer();
    customerTwo.setName("two");
    customerTwo.setAddress("tssoft12");

    list.add(customerOne);
    list.add(customerTwo);

    //set mock paramter and return value
    mgr.getCustomeres();
    control.setReturnValue(list);

    //now starting test …
    control.replay();
    List returnList = mgr.getCustomeres();

    assertEquals(2, returnList.size());
    assertEquals("one", ( (Customer) list.get(0)).getName());
    assertEquals("tssoft1", ( (Customer) list.get(0)).getAddress());
    assertEquals("two", ( (Customer) list.get(1)).getName());
    assertEquals("tssoft2", ( (Customer) list.get(1)).getAddress());
    control.verify();
  }

  public void testRemoverCustomer() throws Exception {

    Customer customer = new Customer();
    customer.setName("test");
    customer.setId(new Long(100));

    mgr.removeCustomer(customer.getId());
    control.setVoidCallable(); //void ,not return anything,so call this

    control.replay();
    mgr.removeCustomer(customer.getId());
    control.verify();

  }

 }

 

 

 

 

 

2005年03月13日

  凤栖小楼中,
  把盏联吟烛泪红。
  借问孟光谁得匹,
  梁鸿。
  相座欣然主客同。
 
  杯酒且从容,
  旧事无多梦也慵。
  更待他年花更好,
  重逢。
  只待春风换北风。

用rdate与crontab来实现同步时间服务

[status]
时间服务器:192.168.0.1
被同步机器:192.168.0.2

[step]


1.在192.168.0.1中打开time服务
 修改/etc/xinetd.d/time
 把   disable = yes -> disable = no
 运行 pkill -HUP xinetd

 2.在192.168.0.2中加crontab
   0 0-23 * * * /usr/bin/rdate -s 192.168.0.1

1.建用户
 clx
 
 [root@cvssrv cvs]# groupadd adslcvs
 [root@cvssrv cvs]# groupadd bandcvs

2.建两个用户组
 adslcvs
 bandcvs
 
 [root@cvssrv cvs]# groupadd adslcvs
 [root@cvssrv cvs]# groupadd bandcvs
 
 
3.把用户加入用户组
 [root@cvssrv cvs]# usermod -G adslcvs,bandcvs  clx
 
 
4.创建cvs服务器
 
  (1)创建cvs目录
   /cvs/adsl
   /cvs/band

  (2)创建文件:
   /etc/xinetd.d/cvspserver

   service cvspserver
   {
     disable = no
     socket_type  = stream
     protocol  = tcp
     wait   = no
     user    = root
     #only_from =127.0.0.1
     server   = /usr/bin/cvs
     server_args  = -f –allow-root=/cvs/adsl/cvsroot –allow-root=/cvs/band/cvsroot pserver
  }

 (3)重启xinetd
    service xinetd restart

5. copy 文件
   /u/adsl/cvsroot   –>
   /u/cvsroot
   tar -zcvf bandcvs.tar.gz cvsroot
   tar -zxvf adsl.tar.gz


6.目录权限
  cd /cvs/band/
  chown -R clx:bandcvs cvsroot
 
  cd /cvs/adsl/
  chown -R clx:adslcvs cvsroot
 
7. check out  test


8. 创建新的用户,并且指定不同的模块


备份服务器的过程
  
  ip: 192.168.0.5
 
 服务: rsync(从cvs服务器(192.168.0.4)同步
 
 1.在cvs服务器(192.168.0.4)配置rsync
 
   (1)修改 /etc/xinetd.d/rsync ,将disable = no改成yes
   (2)编辑配置文件/etc/rsyncd.conf
  
    uid = 0                                 #用root,比较危险
  gid = nobody
  use chroot = no         # 不使用chroot
  max connections = 4         # 最大连接数为4
  pid file = /var/run/rsyncd.pid
  lock file = /var/run/rsync.lock
  log file = /var/log/rsyncd.log    # 日志记录文件
  
  #[inburst]            # 这里是认证的模块名,在client端需要指定
  #path = /home/inburst/python/    # 需要做镜像的目录
  #comment = BACKUP CLIENT IS SOLARIS 8 E250
  #ignore errors            # 可以忽略一些无关的IO错误
  #read only = yes            # 只读
  #list = no            # 不允许列文件
  #auth users = inburst        # 认证的用户名,如果没有这行,则表明是匿名
  #secrets file = /etc/inburst.pas    # 认证文件名
  
  [adslcvs]
  path = /cvs/adsl/cvsroot
  comment = cvs adsl
  
  [bandcvs]
  path = /cvs/band/cvsroot
  comment = cvs band
  
  (3) 重启rsync server
  service xinetd restart
  
  
2.在备份服务器上定时备份文件(192.168.0.5)
 
   1. mkdir /u/backup/
   2.创建脚本/u/backup.sh     
    BACKUPDIR=`date +%A`
    [ -d /u/backup/$BACKUPDIR ] || mkdir /u/backup/$BACKUPDIR
    rsync -vzrtopg –progress –delete 192.168.0.4::adslcvs /u/backup/$BACKUPDIR/adslcvs
    rsync -vzrtopg –progress –delete 192.168.0.4::bandcvs /u/backup/$BACKUPDIR/bandcvs
  
   3. crontab
     crontab -e
     #每天5点零一分做一次备份
     01 17 * * * /u/backup.sh
       
     
************************************************************************** 
 
 参考:
 (http://fanqiang.chinaunix.net/a6/b7/20010421/175728.html
 简介:rsync是个真正的镜像工具,它能相应地删除本地文件万一服务器上的文件被删除, 
     这一点比wget好,在镜像目录的同时也能排除某些目录及文件。速度非常快
 主页:rsync.samba.org
 环境: Redhat Linux 6.2 , rsync 2.4.3(下载源码编译最好),
两台linux服务器192.168.11.7(test)(rsync server) , 192.168.11.2(proxy)(rsync client)

步骤:

1. 下载最新的rsync 2.4.3源文件编译,看rsync.samba.org ,
    据我所知, 只有2.4.3才支持镜像时的排除选项 –include and –exclude, 
    编译后得到一个执行文件rsync,仅需这个执行文件即可

2. 编辑配置文件/etc/rsyncd.conf 如下:
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock

[test]
   path = /test
   comment = test folder
   uid = root
   ignore errors
   read only = yes
   list = no
   auth users = sun
   secrets file = /etc/test.scrt

注释: 定义了需要被镜像的目录为/test,需要密码连接,文件在/etc/test.scrt,
注意该文件的属性和权限  -rw——-   1 root     root       test.scrt

[root@test /etc]# more /etc/test.scrt
test:tset


3.启动服务器后台 rsync
# rsync –daemon 且加这一行到/etc/rc.d/rc.local

# echo ”rsync –daemon” >> /etc/rc.d/rc.local

4. 在服务器192.168.11.2上设定客户端cron job. 每晚自动作备份或者镜像
0 21 * * 1-5 /root/.test.sh
note:每晚九点周一至周五自动运行/root/.test.sh作备份

#more /root/.test.sh
#!/bin/sh
DATE=`date +%w`

rsync -tvzrp -e ssh –progress –delete test@192.168.11.7::test /shopu/test/$DATE –password
-file=/etc/test > /var/log/test/test.$DATE
mail -s ”test Backup is done”  myemailaddress < /var/log/test/test.$DATE

[root@proxy /root]# more /etc/test
tset
[root@proxy /root]# ls /etc/test -l
-rw——-   1 root     root            4 May 16 21:40 /etc/test


注意: -e ssh表示用SSH加密连接,需要服务器test支持SSH

5.可用于下列用途:
a. 从另一台服务器备份主服务器上的某个整个目录,且可排除某个子目录及文件
b. 镜像主WEB服务器的主页目录,适合于像像网站,用–delete可删除服务器上没有的本地文件
   是真正的像像,不同于wget,wget仅根据网站的URL链接下载链接指向的每个文件,本地文件并不
   删除