ccidnet????

出版日期:1997-06-16 总期号:662 本年期号:22

本期导读
综合消息
网络通信
市场商情
国际文摘
专题评测
编程维修
一个实用的unix备份与安装辅助程序

邓振兴

  在unix系统上,我们有时会遇到这种情况:在某台unix系统机上安装好一个几百m的应用系统(如一个数据库系统)后,若在一个局域网内安装另一台机器,只需使用rcp把相应目录及文件拷贝即可,而毋需再安装一次;但当另一台机器不在一个局域网内,只是一个低速的广域网,怎么利用已安装好的机器来快速安装呢?显然使用rcp已不现实,这时我们可以把已安装好的unix系统上的相应目录合并成一个文件,然后拷贝到笔记本机上;携带笔记本至需待装机处,把合并后的文件拷贝到待装机上,再把文件还原成目录,这样便完成了安装。有兴趣的可修改该程序,编写一个unix上的arj实用工具。

  笔者在维护下属单位的系统中常使用这种方法,速度快,方便。

  该程序不仅适应于安装,而且也适应于用dos系统备份unix系统上的应用。

  附源程序:(已调试通过,并多次实现使用)

  #includeΙsys/types.hΛ

  #includeΙsys/stat.hΛ

  #includeΙstdio.hΛ

  #includeΙftw.hΛ

  #definear″ ″

  file farj;intlist();

  voidarx();

  main(argc,argv)

  intargc;char argv[]

  {charfname1[100],opt[1];

  if(argcΙ4){printf(″ιnusage:myara xarj—filepathdestpathιn″);exit(1);}

  strcpy(opt,argv[1]);

  if(opt[0]ΚΚ′a′){strcpy(fname1,argv[2]);

  if((farjΚfopen(fname1,″wb″))ΚΚnull)

  {printf(″ιnerrorinopenfile%sιn″,fname1);exit(1);}

  elseftw(argv[3],list,1);}

  elseif(opt[0]ΚΚ′x′)arx(argv[2],argv[3]);

  else{printf(″ιnusage:myara xarj—filepathdestpathιn″);exit(1);}

  fclose(farj);}

  

  intlist(name,status,type)

  char name;structstat status;inttype;

  {intsize,left,bbΚ80;charbuf[80],buf1[80];file ft;

  charfname[100];if(typeΚΚftw—ns)return(0);

  if(typeΚΚftw—f){

  sprintf(buf,″%s%3o%d%dιn″,name,status-Λst—mode&0777,status-Λst—reserved1,status-Λst—reserved2,status-Λst—size);

  / 若unix是sco—unix可把″st—reserved1″改成″st—uid″把″st—reserved2″改成″st—gid″ /

  fwrite(buf,strlen(buf),1,farj);printf(″ιnbeingadd%s″,name);

  strcpy(fname,name);ftΚfopen(fname,″rb″);sizeΚstatus-Λst—size;

  if(ftΚΚnull){printf(″ιnerrorinopen%s″,fname);

  exit(1);}

  while(fread(buf1,bb,1,ft)Λ0)fwrite(buf1,bb,1,farj);leftΚsize%bb;

  if(leftΛ0){fread(buf1,left,1,ft);fwrite(buf1,left,1,farj);}

  fclose(ft);}

  else

  fprintf(farj,″ %s%3o%d%dιn″,name,status-Λst—mode&0777,status-Λst—reserved1,status-Λst—reserved2);

  return(0);

  }

  

  / ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ /

  / thefollowfunctionforextract /

  voidarx(filepath,destpath)

  charfilepath[100],destpath[100];

  {file ft;

  intacc,owner,grp,size,s1Κ80,s2,i,len,p,sp,sl;

  charfname[70],fnamet[70],fname[70],path[70],str[80],buf[80];

  strcpy(fname1,filepath);

  if((farjΚfopen(fnam1,″rb″)Κnull){perror(″farj″);exit(1);}

  while(!feof(farj)){

  fgets(buf,s1,farj);

  accΚ0;ownerΚ0;grpΚ0;sizeΚ0;

  sscanf(buf,″%s%3o%d%d%d″,fnamet,&acc,&owner,&grp,&size);

  if(fnamet[0]ΚΚ′ ′){lenΚstrlen(fnamet);

  for(iΚ1;iΙlen;i++)path[i-1]Κfnamet[i];

  path[len-1]Κ′ι0′;

  strcpy(str,″mkdir″);

  strcat(str,destpath);

  if(path[0]ΚΚ′/′);

  elsestrcat(str,″/″);

  strcat(str,path);

  printf(″ιnbeing%s″,str);

  system(str);

  continue;}

  else{printf(″ιnbeingextractedfromιn%s″,fnamet);

  if(fnamet[0]ΚΚ′/′)

  sprintf(fname,″%s/%s″,destpath,fnamet);

  else

  sprintf(fname,″%s/%s″,destpath,fnamet);

  if((ftΚfopen(fname,″ab″))ΚΚnull){

  perror(″ιnopenftΚ″);

  printf(″%sιn″,fname);

  continue;}

  spΚ0;

  while(spΙsize){

  if(size-spΛΚs1){fread(buf,s1,1,farj);fwrite(buf,s1,1,ft);

  spΚsp+s1;}

  else{fread(buf,size-sp,1,farj);

  fwrite(buf,size-sp,1,ft);

  spΚsize;

  break;}}

  fclose(ft);

  if(chmod(fname,acc)Ι0)printf(″ιnerrorinchmod:%s″,fname);

  if(chown(fname,owner,grp)ΚΚ-1)printf(″ιnerrorinchown:%s″,fname);

  }

  }

  }