📄 backup.c
字号:
log("Creating final archive"); Report("Final archive:\n"); Report("--------------\n\n"); unlink("sitback.tmpout"); unlink("sitback.tmperr"); if(conf__mode!=DAEMON) pthread_create(&tracker,NULL,UI__TrackStdout,NULL); /*track output*/ else tracker=0; UI__SetProgressText(""); rc=__make_archive_cd(); sleep(1); if(!pthread_kill(tracker,0)) /*stop tracking output */ { pthread_cancel(tracker); pthread_join(tracker,NULL); } UI__SetProgressText(""); if(rc) { /* Get the error messages (if possible) */ debug("archive cd creation failed..\n"); Report("Error (Please check the logfile)\n"); log("Cd creation failed, check report file for details"); if((err_in=fopen("sitback.tmperr","r"))==NULL) Report("Error message not available\n"); else { while(!feof(err_in)) { ch=getc(err_in); if(feof(err_in)) break; ch2strn[0]=ch; /* Oh boy... what a hack.. :) */ ch2strn[1]='\0'; Report(ch2strn); } fclose(err_in); } /* stop */ errcode=96; return; } Report("Success\n\n\n\n"); log("Final archive created"); } /*we would only reach this point of the backup is okay. Make/Update the database entry and set the status to OK. Ignore errors since the archive is ok.. Just report the errors to the log*/ if(DataBaseInsert(conf__archive_id)==-1) log("Error reported when creating or updating database entry"); if(DataBaseSetStatus(conf__archive_id,"OK")==-1) log("Error reported when setting status of database entry"); /*close the report file and tell the logfile that we are done*/ log("Backup operation complete"); UI__Message("Backup operation complete\n");}/******************************************************************** void *__check_for_new_volume() Check for tar asking the user to prepare the next volume. Used when creating multiple volumes arguments: word: Argument value ass passed to pthread_create() return: none. Thread is terminated, either on internal error or as a result of an external call to pthread_cancel********************************************************************/void *__check_for_new_volume(void *word){ FILE *err,*redir; char strn[256]; unsigned int count=2; char *buffer; unsigned long size; char *p; int oldcnt,oldlimit=0; char *msg=(char*) word; char newname[4096]; char archive[256]; int pos; /*wait a little while until we are sure that tar has created the tmperr file*/ sleep(2); pthread_testcancel(); /*check for 'prepare new volume for...' messages*/ while(1) { /*open the error-file*/ if((err=fopen("sitback.tmperr","r"))==NULL) { while(1) { /*hmm.. maybe a synchronization problem*/ sleep(1); pthread_testcancel(); /*try to solve this problem*/ if((err=fopen("sitback.tmperr","r"))!=NULL) break; } } /*get the length of the file as of now*/ fseek(err,0,SEEK_END); size=ftell(err); rewind(err); /*check the content*/ if((buffer=(char*) malloc(size+1))!=NULL) { /*get the current content*/ fread(buffer,size,1,err); buffer[size]='\0'; for(pos=0;pos<size;pos++) { if(buffer[pos]=='\0') buffer[pos]='_'; } /*check if a new media should be inserted*/ sprintf(strn,"Prepare volume #%u for ",count); if(strstr(buffer,strn)!=NULL) { /* Here is a special situation. If the user has requested multiple volumes to be created on a cd, we must burn a cd now, and then ask for a new disc to be inserted */ if(conf__type==TARCDR || conf__type==TARCDRW) { /* Write a cd */ strcpy(archive,conf__temp); if(conf__compress && !strcmp(conf__compressapp,"gzip")) strcat(archive,"archive.tar.gz"); else if(conf__compress && !strcmp(conf__compressapp,"bzip2")) strcat(archive,"archive.tar.bz2"); else if(conf__compress && !strcmp(conf__compressapp,"compress")) strcat(archive,"archive.tar.Z"); else strcat(archive,"archive.tar"); __execute_iso_backup(archive); /* Remove the existing archive */ unlink(archive); } /* Another special situation, if we are creating multiple tar iso's, rename the current */ if(conf__type==TARISO) { /* Create an iso */ strcpy(archive,conf__temp); if(conf__compress && !strcmp(conf__compressapp,"gzip")) strcat(archive,"archive.tar.gz"); else if(conf__compress && !strcmp(conf__compressapp,"bzip2")) strcat(archive,"archive.tar.bz2"); else if(conf__compress && !strcmp(conf__compressapp,"compress")) strcat(archive,"archive.tar.Z"); else strcat(archive,"archive.tar"); __execute_iso_backup(archive); /* Move the archive to a final file */ snprintf(newname,4095,"%s#%u",conf__archive,count-2); debug("Renaming %s to %s\n",conf__archive,newname); rename(conf__archive,newname); /* Remove the existing archive */ unlink(archive); } /*okay, got this.. wait for next volume*/ count++; conf__archive_count=count-2; /*try to beeb the bell*/ UI__Beep(); /*message to the user*/ sprintf(strn,"Insert new media for volume %u.",count-1); UI__OkDlg(strn); UI__Message(msg); if((redir=fopen("sitback.tmpin","a"))!=NULL) { fprintf(redir,"\n"); fflush(redir); fclose(redir); } else UI__Warning("Unable to open redirected stdin for backup process\n"); } else { /*check if the user pressed a key, but did not change the media*/ sprintf(strn,"Prepare volume #%u for ",count-1); if((p=strstr(buffer,strn))!=NULL) { oldcnt=0; do { p++; p=strstr(p,strn); oldcnt++; if(p!=NULL && oldcnt>oldlimit) { /*try to beeb the bell*/ UI__Beep(); /*message to the user*/ sprintf(strn,"Insert new media for volume %u. Press any key to continue\n",count-1); UI__OkDlg(strn); UI__Message(msg); if((redir=fopen("sitback.tmpin","a"))!=NULL) { fprintf(redir,"\n"); fflush(redir); fclose(redir); } else UI__Warning("Unable to open redirected stdin for backup process\n"); oldlimit=oldcnt; } } while(p!=NULL); } } /*release the buffer-memory and clear any errors*/ free(buffer); } /*close the error-file*/ fclose(err); /*test if we should cancel, else sleep a second*/ pthread_testcancel(); sleep(1); } /*keep the compiler happy*/ return NULL;}/******************************************************************** void __daemon_killed(int signal) Catch SIGINT and SIGTERM to do a gracefull shutdown when sitback in daemon mode is killed. arguments: signal: signal received return: none********************************************************************/void __daemon_killed(int signal){ debug("Received signal '%d'. Gracefull shutdown\n",signal); /*make a log-entry saying that we were killed*/ switch(signal) { case SIGINT: log("Received SIGINT. Halting operation"); break; case SIGTERM: log("Received SIGTERM. Halting operation"); break; default: log("Unexpected signal %d received. Halting operation",signal); break; } /*Sitback shutdown*/ log("Sitback shutdown"); UI__Message("Caught signal, waiting for archive to finish\n"); /*release dynamic allocated resources, such as the filelist etc.*/ debug("Doing Shutdown()\n"); Shutdown(); /*exit nicely*/ debug("Ready to exit\n"); exit(0);}/******************************************************************** void __caught_sigpipe(int signal) Catch SIGPIPE arguments: signal: signal received return: none********************************************************************/void __caught_sigpipe(int signal){ debug("Received signal '%d'. Ignoring\n",signal);}/******************************************************************** void __backup_cleanup() Cleanup /tmp after a backup arguments: none return: none, errors are not reported********************************************************************/void __backup_cleanup(){ char location[512]; /*go ahead and try to remove the files that are a result of a backup operation. Some of them may not exists, f.ex. if the backup or verify encountered errors. We can safely ignore any error-messages.*/ if(!enable_dbg) { remove("sitback.tmperr"); remove("sitback.tmpout"); remove("sitback.tmpin"); remove("sitback.archivelist"); remove("sitback.archivename"); remove("sitback.archiveid"); remove("sitback.sh"); remove("sitback.report"); remove("sitback.excludelist"); remove("sitback.debug"); remove("sitback.config-debug"); sprintf(location,"%ssitback.iso",conf__temp); remove(location); sprintf(location,"rm -f %sarchive.*",conf__temp); system(location); system("rm -f *.chk"); system("rm -f *.timestamp"); }}/******************************************************************** int __execute_backup() Execute the appropriate backup command arguments: none return: return code on success -1 on error (unable to exec)********************************************************************/int __execute_backup(){ /* Creating a tar archive */ if(conf__type==TAR || conf__type==TARCDR || conf__type==TARCDRW || conf__type==TARISO) return __execute_tar_backup(); /* File copy */ else if(conf__type==FILECOPY) return __execute_filecopy_backup(); /* Cdrom backup */ else if(conf__type==CDR || conf__type==CDRW || conf__type==ISO) return __execute_iso_backup(NULL); /* ZIP backup */ else if(conf__type==ZIP || conf__type==ZIPCDR || conf__type==ZIPCDRW || conf__type==ZIPISO) return __execute_zip_backup(NULL); /* Unknown type */ else { debug("Unknown type of backup %d\n",conf__type); UI__Warning("Unknown type of backup\n"); return -1; } /* To keep the compiler happy */ return -1;}/******************************************************************** int __execute_tar_backup() Execute a tar backup arguments: none return: return code on success -1 on error (unable to exec)********************************************************************/int __execute_tar_backup(){ int pid; int status=0; char cmd[4096]; int len; /* If this is a compressed tar backup to a local or remote device, tar will not (for some versions of tar) bring back exit status, f.ex. if a tape is missing, so we must do the check.. */ if(conf__type==TAR && conf__compress) { debug("Checking backup device\n"); UI__Message("- Checking backup device\n"); system("echo 123 > sitback.tmpdata"); sprintf(cmd,"%s -c -f %s sitback.tmpdata",conf__tar,conf__archive); pid=Execute(cmd); waitpid(pid,&status,0); unlink("sitback.tmpdata"); debug("Status is %d\n",status); if(status) { UI__Warning("Unable to write to backup device.\nPerhaps missing media\n"); log("Unable to write to backup device. Perhaps missing media\n"); Report("Unable to write to backup device. Perhaps missing media\n"); return -1; } /* Rewind */ if(RewindDevice()) { Report("Unable to rewind the tape\n"); return -1; } } /* initial command line */ if(conf__type==TAR)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -