📄 backup.c
字号:
snprintf(cmd,4096,"%s %s-v speed=%s dev=%s %ssitback.iso",conf__cdrecord,options,conf__speed,conf__archive,conf__temp); else snprintf(cmd,4096,"%s %s-v speed=%s -nofix dev=%s %ssitback.iso",conf__cdrecord,options,conf__speed,conf__archive,conf__temp); } } } /* Execute */ if((pid=Execute(cmd))<0) return -1; waitpid(pid,&status,0); if(status!=0) status=1; /* cdrecord may output the exit code 255 which, for some reason, gives a status with the 8 least significant bits all 0. (65280) */ /* Done */ debug("Writing complete, status=%d\n",status); } /*no problems*/ return status;}/******************************************************************** int __execute_iso_backup_on_the_fly() Execute a iso/cdr/cdrw backup with 'on the fly' copy of data arguments: archive: If NULL, do a regular backup, else take the file indicated and write it to the cd return: return code on success -1 on error (unable to exec)********************************************************************/int __execute_iso_backup_on_the_fly(char *archive){ int pid; int status=0; char cmd[8192]; int len; char options[1024]=""; int tracksize; time_t currtime=time(NULL); struct tm *timeptr=localtime(&currtime); char timestamp[30]; FILE *file; /* Should the cd be blanked ?? */ if(conf__type==CDRW || conf__type==TARCDRW || conf__type==ZIPCDRW) { debug("Blanking cd-rw\n"); UI__Message("- Blanking cd-rw\n"); if(conf__speed[0]=='\0') snprintf(cmd,8192,"%s -v blank=fast -nofix dev=%s",conf__cdrecord,conf__archive); else snprintf(cmd,8192,"%s -v blank=fast -nofix speed=%s dev=%s",conf__cdrecord,conf__speed,conf__archive); if((pid=Execute(cmd))<0) return -1; waitpid(pid,&status,0); if(status!=0) return -1; /* Done */ debug("Blanking complete\n"); } /* Prepare the set of extra options for cdrecord (if any) */ len=0; if(conf__buffersize[0]!='\0') len+=snprintf(&options[len],1024,"fs=%s ",conf__buffersize); if(conf__eject) len+=snprintf(&options[len],1024,"-eject "); /* get the tracksize of this cd */ UI__Message("- Calculating size of archive\n"); tracksize=__tracksize(archive); if(tracksize==-1) return -1; /* Begin the mkisofs command */ len=snprintf(cmd,8192,"sh -c \"%s -allow-lowercase -allow-multidot -p sitback_%s -A %s ",conf__mkisofs,VERSION,conf__archive_name); if(conf__follow_symlinks) len+=snprintf(&cmd[len],8192-len,"-f "); len+=snprintf(&cmd[len],8192-len,"-l -J -L -r -relaxed-filenames -v -U -graft-points -D "); /* Add the file(s) to be included in the iso image */ if(archive==NULL) { /* Standard stuff */ if(conf__no_info==0) len+=snprintf(&cmd[len],8192-len,"sitback.archivelist sitback.excludelist sitback.archiveid sitback.archivename "); /* rewind the files list */ while(conf__filelist->prev!=NULL) conf__filelist=conf__filelist->prev; /* add the locations */ while(1) { if(conf__add_to_root==0) len+=snprintf(&cmd[len],4096-len,"\"%s/=%s\" ",&conf__filelist->target[1],conf__filelist->target); else len+=snprintf(&cmd[len],4096-len,"\"%s\" ",conf__filelist->target); /* next file location */ if(conf__filelist->next==NULL) break; else conf__filelist=conf__filelist->next; } /* Add timestamp */ sprintf(timestamp,"%04d%02d%02d%02d%02d%02d.timestamp",timeptr->tm_year+1900,timeptr->tm_mon,timeptr->tm_mday,timeptr->tm_hour,timeptr->tm_min,timeptr->tm_sec); if((file=fopen(timestamp,"w"))!=NULL) { fprintf(file,"%04d%02d%02d%02d%02d%02d",timeptr->tm_year+1900,timeptr->tm_mon,timeptr->tm_mday,timeptr->tm_hour,timeptr->tm_min,timeptr->tm_sec); fclose(file); } len+=snprintf(&cmd[len],4096-len,"%s",timestamp); } else { /* Include the specified file in the image */ debug("Including the file '%s' in the iso image\n",archive); len+=snprintf(&cmd[len],4096-len,"%s ",archive); /* Add timestamp */ sprintf(timestamp,"%04d%02d%02d%02d%02d%02d.timestamp",timeptr->tm_year+1900,timeptr->tm_mon,timeptr->tm_mday,timeptr->tm_hour,timeptr->tm_min,timeptr->tm_sec); if((file=fopen(timestamp,"w"))!=NULL) { fprintf(file,"%04d%02d%02d%02d%02d%02d",timeptr->tm_year+1900,timeptr->tm_mon,timeptr->tm_mday,timeptr->tm_hour,timeptr->tm_min,timeptr->tm_sec); fclose(file); } len+=snprintf(&cmd[len],4096-len,"%s",timestamp); } /* Add commands for writing the cd */ sleep(3); /* Allow the status output to flush */ if(conf__speed[0]=='\0') { if(conf__type==CDR || conf__type==TARCDR || conf__type==ZIPCDR || conf__fixate) len+=snprintf(&cmd[len],8192-len," | %s %s-v -waiti dev=%s",conf__cdrecord,options,conf__archive); else len+=snprintf(&cmd[len],8192-len," | %s %s-v -waiti -nofix dev=%s",conf__cdrecord,options,conf__archive); } else { if(conf__type==CDR || conf__type==TARCDR || conf__type==ZIPCDR || conf__fixate) len+=snprintf(&cmd[len],8192-len," | %s %s-v -waiti speed=%s dev=%s",conf__cdrecord,options,conf__speed,conf__archive); else len+=snprintf(&cmd[len],8192-len," | %s %s-v -waiti speed=%s -nofix dev=%s",conf__cdrecord,options,conf__speed,conf__archive); } /* Add tsize */ if(tracksize!=0) { debug("Adding tracksize info\n"); len+=snprintf(&cmd[len],8192-len," tsize=%d",tracksize*2048); } /* End the command */ len+=snprintf(&cmd[len],8192-len," -\""); /* Make sure we do not have a trailing blankspace */ if(cmd[strlen(cmd)-1]==' ') cmd[strlen(cmd)-1]='\0'; /* debug garbage */ debug("ready to execute..:\n"); debug(" '%s'\n",cmd); /*execute and wait for the process to finish*/ UI__Message("- Writing cd (on-the-fly)\n"); if((pid=Execute(cmd))<0) return -1; waitpid(pid,&status,0); debug("Iso creating has finished.. status is %d\n",status); /* Return result */ return status;}/******************************************************************** int __exec_verify() Execute the appropriate command to verify arguments: none return: return code on success -1 on error (unable to exec)********************************************************************/int __execute_verify(){ /* Verifying a tar archive */ if(conf__type==TAR || conf__type==TARCDR || conf__type==TARCDRW || conf__type==TARISO) return __execute_tar_verify(); /* File copy */ else if(conf__type==FILECOPY) return __execute_filecopy_verify(); /* Unknown type */ else { debug("Unknown type of verify opeation %d\n",conf__type); UI__Warning("Unknown type of verify operation\n"); return -1; } /* To keep the compiler happy */ return -1;}/******************************************************************** int __exec_tar_verify() Execute the appropriate command to verify a tar archive arguments: none return: return code on success -1 on error (unable to exec)********************************************************************/int __execute_tar_verify(){ int pid; int status=0; char cmd[2048]=""; int len; struct stat statbuf; /* Because some versions of tar combined with some versions of compressors do not return proper error codes, check if the archive is missing at this point */ if(conf__type==TAR) snprintf(cmd,1024,"%s",conf__archive); else { snprintf(cmd,1024,"%sarchive.tar",conf__temp); if(conf__compress) { if(strstr(conf__compressapp,"gzip")!=NULL) strcat(cmd,".gz"); else if(strstr(conf__compressapp,"bzip2")!=NULL) strcat(cmd,".bz2"); else if(strstr(conf__compressapp,"zip")!=NULL) strcat(cmd,".zip"); else if(strstr(conf__compressapp,"compress")!=NULL) strcat(cmd,".Z"); } } debug("Checking for archive '%s'\n",cmd); if(stat(cmd,&statbuf)) { UI__Warning("Archive not found.. Aborting\n"); log("Archive not found before verifying.. Aborting"); Report("Archive not found before verifying. Aborting\n"); return -1; } /* initial part of command line */ if(conf__type==TAR) len=snprintf(cmd,1024,"%s -d -v -f %s",conf__tar,conf__archive); else { len=snprintf(cmd,1024,"%s -d -v -f %sarchive.tar",conf__tar,conf__temp); if(conf__compress) { if(strstr(conf__compressapp,"gzip")!=NULL) strcat(cmd,"gz"); else if(strstr(conf__compressapp,"bzip2")!=NULL) strcat(cmd,"bz2"); else if(strstr(conf__compressapp,"zip")!=NULL) strcat(cmd,"zip"); else if(strstr(conf__compressapp,"compress")!=NULL) strcat(cmd,"Z"); } } /* compression ?? */ if(conf__compress) len+=snprintf(&cmd[len],1024-len," --use-compress-program %s",conf__compressapp); /* remote device ?? */ if(conf__rhost[0]!='\0') len+=snprintf(&cmd[len],4096-len," --rsh-command=%s",conf__rsh); /*Multiple or fixed length volumes*/ if(conf__volume_length!=0) len+=snprintf(&cmd[len],1024-len," -L %d",conf__volume_length); else if(conf__allow_multiple) len+=snprintf(&cmd[len],1024-len," -M"); /*start mark, if updating. This is necessary to avoid verify errors*/ if(conf__tar_action=='u') len+=snprintf(&cmd[len],1024-len," -K %s",conf__start_mark); /* what did we compose ?? */ debug("ready to execute..:\n"); debug(" '%s'\n",cmd); /*execute and wait for the process to finish*/ if((pid=Execute(cmd))<0) return -1; waitpid(pid,&status,0); debug("tar has finished.. status is %d\n",status); /*no problems*/ return status&0xFF; /* Result is lowest 8 bits */}/******************************************************************** int __exec_filecopy_verify() Execute the appropriate command to verify a filecopy archive arguments: none return: return code on success -1 on error (unable to exec)********************************************************************/int __execute_filecopy_verify(){ char cmd[1024]=""; FILE *archivelist; char local_file[4096]; char stored_file[4096]; int result=0; char *p; int rc; unsigned size; FILE *in,*out; struct stat statbuf; unsigned count; FILE *file; /* First open the archivelist */ snprintf(cmd,1024,"%s/sitback.archivelist",conf__homedir); if((archivelist=fopen(cmd,"r"))==NULL) { UI__Warning("Unable to open the archivelist\n"); log("Unable to open the archivelist\n"); return -1; } /* Go through the list */ while(1) { /* Get a line */ NEXT: fgets(local_file,4096,archivelist); if(feof(archivelist)) { debug("At end of the filelist\n"); fclose(archivelist); break; } if((p=strchr(local_file,','))!=NULL) *p='\0'; debug("Got local file '%s'\n",local_file); snprintf(cmd,1024,"%s/sitback.tmpout",conf__homedir); if((file=fopen(cmd,"a"))!=NULL) { fprintf(file,"%s\n",local_file); fclose(file); } /* Compose path to the local file */ while(conf__filelist->prev!=NULL) conf__filelist=conf__filelist->prev; while(1) { /* Check agains the stored file path */ debug("Checking target='%s' and local file\n",conf__filelist->target); if(!memcmp(local_file,conf__filelist->target,strlen(conf__filelist->target))) { debug("Match\n"); break; } /* Next */ if(conf__filelist->next==NULL) break; else conf__filelist=conf__filelist->next; } snprintf(stored_file,4096,"%s%s",conf__archive,local_file); debug("Got stored file '%s'\n",stored_file); /* If local_file is a directory, just make sure it exists in the backup */ if(conf__follow_symlinks) rc=stat(local_file,&statbuf); else rc=lstat(local_file,&statbuf); if(rc) { log("Unable to stat() local file or directory '%s'\n",local_file); fclose(archivelist); return -1; } if(S_ISDIR(statbuf.st_mode)) { debug("local_file is directory\n"); if(stat(stored_file,&statbuf)) { /* action depends on verify mode */ UI__Warning("Directory '%s' missing in backup\n",local_file); log("Directory '%s' missing in backup\n",local_file); snprintf(cmd,1024,"%s/sitback.tmperr",conf__homedir); if((file=fopen(cmd,"a"))!=NULL) { fprintf(file,"Directory '%s' missing in backup\n",local_file); fclose(file); } fclose(archivelist); return 1; } } else { /* Compare */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -