⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backup.c

📁 Linux系统备份源代码 可基于用户自定义策略实现系统、应用数据备份
💻 C
📖 第 1 页 / 共 5 页
字号:
  if(ArchiveId()==-1)  {    log("Unable to create archiveid file. Error %d",errcode);    Report("Error while creating the archiveid file. Please check the logfile\n");    return;  }  /*if we are using a magnetic tape and we is about to create a fresh archive,    erase and retenstion the tape, else just retension the tape*/  if(conf__mt_device)  {    if(conf__tar_action=='u')      UI__Message("Initializing magnetic tape (retension)\n");    else      UI__Message("Initializing magnetic tape (erase and retension)\n");    debug("Archive is magnetic tape. Using 'mt' to erase\n");    /*wait until the device is ready, ArchiveId has opened the device*/    if(ArchiveReady(3600))    {      errcode=50;      log("Archive or device no longer available");      Report("Archive or device no longer available\nERROR\n");      return;    }    /*try to erase the tape, but not if we are updating an archive*/    if(conf__tar_action!='u')    {      log("Erasing magnetic tape");      if((exec_pid=Execute("%s -f %s erase",conf__mt,conf__archive))<0)      {        Report("Unable to erase the tape\n");        log("In backup. Unable to erase the tape.");        errcode=13;        UI__Warning("Unable to erase the tape\n");        return;      }      waitpid(exec_pid,NULL,0);    }    /*wait until the device is ready, ArchiveId has opened the device*/    if(ArchiveReady(3600))    {      errcode=50;      log("Archive or device no longer available");      Report("Archive or device no longer available\nERROR\n");      return;    }    /*try to retension the tape*/    log("Retensioning magnetic tape");    if((exec_pid=Execute("%s -f %s retension",conf__mt,conf__archive))<0)    {      Report("Unable to retension the tape\n");      log("In backup. Unable to retension the tape.");      errcode=13;      UI__Warning("Unable to retension the tape\n");      return;    }    waitpid(exec_pid,NULL,0);  }  /*wait until the device is ready, ArchiveId has opened the device*/  if(ArchiveReady(3600))  {    errcode=50;    log("Archive or device no longer available");    Report("Archive or device no longer available\nERROR\n");    return;  }  /*make a named pipe so that we can get access to stdin in a subprocess*/  remove("sitback.tmpin");  #ifdef HAVE_MKFIFO  if(mkfifo("sitback.tmpin",S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)==-1)  #else  if(mknod("sitback.tmpin",S_IFIFO | 0666,0)==-1)  #endif  {    errcode=54;    log("Unable to create fifo for redirected stdin");    Report("Unable to redirect stdin form subprocesses\nERROR\n");    return;  }  /*create a unique start mark*/  GenerateStartMark();  /*from now on, until we complete the operation, the status of the    archive is unknown. Ignore any errors, the entry might not    even exist yet.*/  DataBaseSetStatus(conf__archive_id,"UNKNOWN");  errcode=0;  /*ignore any errors*/  /* execute the backup */  log("Backup in progress..");  UI__Message("Backup\n");  Report("Backup:  [%s]\n",conf__script_name);  Report("----------");  for(count=0;count<=strlen(conf__script_name);count++)    Report("-");  Report("\n\n");  if(conf__allow_multiple || conf__volume_length!=0)  {    /*since we are creating multiple volumes, and catching stderr,      someone need to check when it is time to insert another media*/    pthread_attr_init(&attr);    if(pthread_create(&vchecker,&attr,__check_for_new_volume,(void*) backup_new_volume_msg))    {      errcode=19;      UI__Warning("Unable to check for new volumes. Unable to continue\n");      log("pthread_create failed when creating vchecker. '%s'",(char*) strerror(errno));      Report("Unable to check for new volumes, operation terminated\n");      return;    }  }  if(conf__mode!=DAEMON)  {    unlink("sitback.tmpout");    unlink("sitback.tmperr");    if(conf__type==ISO)      pthread_create(&tracker,NULL,UI__TrackStdout,NULL); /*track tar verbose output*/    else      pthread_create(&tracker,NULL,UI__TrackStdout,NULL); /*track tar verbose output*/  }  UI__SetProgressText("File: ");  debug("Starting the backup\n");  rc=__execute_backup();  debug("rc=%d\n",rc);  sleep(1);  debug("Checking if backup is still running\n");  if(!pthread_kill(tracker,0))  /*stop tracking output from tar*/  {    debug("Yep, killing 'tracker' thread\n");    pthread_cancel(tracker);    pthread_join(tracker,NULL);  }  UI__SetProgressText("");  if( vchecker!=0 )  {    debug("Checking if the 'checker' thread is still running\n");    if(!pthread_kill(vchecker,0))    {      debug("cancel 'new volume'-checker\n");      pthread_cancel(vchecker);  /*no more use for this one*/      pthread_join(vchecker,NULL);    }  }  /*analyze return value from backup*/  debug("Analyzing return value from backup\n");  if(rc==127)  {    log("In backup. Unable to execute backup");    Report("Error during backup. Please check the logfile\n");    errcode=13;    UI__Warning("Unable to execute backup\n");    return;  }  else if(rc==-1)  {    log("In backup. Error while executing backup. '%s'",(char *) strerror(errno));    Report("Error during backup. Please check the logfile\n");    errcode=13;    UI__Warning("Error while while executing backup\n");    return;  }  else if(rc)  /*other errors, get stderr*/  {    /*open the error file*/    if((err_in=fopen("sitback.tmperr","r"))==NULL)    {      UI__Warning("Error message not available\n");    }    else    {      /*write the full error message to the report file*/      Report("Error during backup..  Error message follows:\n\n");      count=0;      while(!feof(err_in))      {        ch=getc(err_in);        if(!feof(err_in))        {          Report("%c",ch);          if(count<1024)          {            if(ch=='\n')              errbuffer[count]='|';            else              errbuffer[count]=ch;            count++;          }        }      }      fclose(err_in);      errbuffer[count]='\0';    }    /*write log message and return*/    log("In backup. Script reported an error. '%s'",errbuffer);    UI__Warning("Error reported during backup. Check logfile\n");    errcode=53;    return;  }  else /*rc is 0*/  {    /*else, there seems to be no errors*/    Report("Success\n\n\n\n");    log("Backup complete");    UI__Message("Backup complete\n");  }  while(waitpid(-1,NULL,WNOHANG | WUNTRACED)>0);  /*only verify archive if specific requested by the user or the script*/  if(conf__verify)  {    /*before continuing, wait for the backup device to be ready. A file is ready to      open right after creating the archive, but a tape will need some time      to empty buffers and rewind*/    if(ArchiveReady(3600))    {      errcode=50;      log("Archive or device no longer available");      Report("Archive or device no longer available\nERROR\n");      return;    }    /* Rewind */    if(RewindDevice())    {      Report("Unable to rewind the tape\n");      return;    }    /*execute tar*/    log("Verify in progress..");    UI__Message("Verify\n");    Report("Verify:  [%s]\n",conf__script_name);    Report("----------");    for(count=0;count<=strlen(conf__script_name);count++)      Report("-");    Report("\n\n");    vchecker=0;    remove("sitback.tmperr");    remove("sitback.tmpout");    if(conf__allow_multiple || conf__volume_length!=0)    {      /*since we are creating multiple volumes, and catching stderr,        someone need to check when it is time to insert another media*/      pthread_attr_init(&attr);      if(pthread_create(&vchecker,&attr,__check_for_new_volume,(void*) verify_new_volume_msg))      {        errcode=19;        UI__Warning("Unable to check for new volumes. Unable to continue\n");        log("pthread_create failed when creating vchecker. '%s'",(char*) strerror(errno));        Report("Unable to check for new volumes, operation terminated\n");        return;      }      UI__Beep();      UI__OkDlg("Insert volume 1 to begin verification of archive.");      UI__Message("Verify\n");    }    unlink("sitback.tmpout");    unlink("sitback.tmperr");    if(conf__mode!=DAEMON)      pthread_create(&tracker,NULL,UI__TrackStdout,NULL); /*track tar verbose output*/    else      tracker=0;    UI__SetProgressText("File: ");    rc=__execute_verify();    sleep(1);    if(!pthread_kill(tracker,0))  /*stop tracking output from tar*/    {      pthread_cancel(tracker);      pthread_join(tracker,NULL);    }    UI__SetProgressText("");    if( vchecker!=0 )    {      debug("Checking if the 'checker' thread is still running\n");      if(!pthread_kill(vchecker,0))      {        debug("cancel 'new volume'-checker\n");        pthread_cancel(vchecker);  /*no more use for this one*/        pthread_join(vchecker,NULL);      }    }    remove("sitback.tmpin");    /*analyze return value from the verification*/    if(rc==127)    {      log("In verify. Unable to execute verification");      Report("Error during verify. Please check the logfile\n");      errcode=13;      UI__Warning("Unable to verify archive\n");      return;    }    else if(rc==-1)    {      log("In verify. Error while executing verification. '%s'",(char*) strerror(errno));      Report("Error during verify. Please check the logfile\n");      errcode=13;      UI__Warning("Error while verifying archive\n");      UI__Warning(strerror(errno));      UI__Warning("\n");      return;    }    else if(rc)  /*other errors, get stderr*/    {      /*open the error file*/      if((err_in=fopen("sitback.tmperr","r"))==NULL)        Report("Error message not available\n");      else      {        /*see if this is the standard gzip or bzip2 complaint about trailing garbage, this is          not critical*/        if(!feof(err_in))          fgets(buffer,2,err_in);  /*newline*/        buffer[0]='\0';        if(!feof(err_in))          fgets(buffer,56,err_in);        if(!strcmp(buffer,"gzip: stdin: decompression OK, trailing garbage ignored") && conf__compressor==GZIP)        {          Report("Success\n\n\n\n");          log("Verify complete");          UI__Message("Verify complete\n");        }        else if(strstr(buffer,"bzip2: (stdin): trailing garbage after EOF ignored")!=NULL && conf__compressor==BZIP2)        {          Report("Success\n\n\n\n");          log("Verify complete");          UI__Message("Verify complete\n");        }        else        {          /* dump the messages to the log */          log("In verify. Script reported error. Message(s) follows..");          Report("Error verify backup..  Error message(s) follows:\n\n");          /*write the extended error message*/          rewind(err_in);          memset(errbuffer,'\0',1024);          count=0;          error_severity=0;          while(!feof(err_in))          {            ch=getc(err_in);            if(!feof(err_in))            {              error_severity=1;              Report("%c",ch);              if(count<1024)              {                if(ch=='\n')                {                  log(errbuffer);                  memset(errbuffer,'\0',1024);                  count=0;                }                else                  errbuffer[count++]=ch;              }            }          }          fclose(err_in);          /*also run through sitback.tmpout, here is the messages            if some content does not match the original files*/          memset(errbuffer,'\0',1024);          count=0;          msg_on=0;          if((err_in=fopen("sitback.tmpout","r"))!=NULL)          {            while(!feof(err_in))            {              ch=getc(err_in);              if(!feof(err_in))              {                if(ch==':')                  msg_on=1;                if(count<1024)                {                  if(ch=='\n')                  {                    if(msg_on)                    {                      Report("%s\n",errbuffer);                      log(errbuffer);                      msg_on=0;                    }                    memset(errbuffer,'\0',1024);                    count=0;                  }                  else                    errbuffer[count++]=ch;                }              }            }            fclose(err_in);          }          /* screen message */          if(conf__verify==2)          {            UI__Warning("Error reported during verify. Check logfile\n");            errcode=53;            return;          }          else if(conf__verify==1 || error_severity)            UI__Warning("Error reported during verify. Check logfile\n");          err_in=NULL;        }        if(err_in!=NULL)          fclose(err_in);      }    }    else    {      /*else, there seems to be no errors*/      Report("Success\n\n\n\n");      log("Verify complete");      UI__Message("Verify complete\n");    }    while(waitpid(-1,NULL,WNOHANG | WUNTRACED)>0);  }  else  {    debug("No verification due to configuration request\n");  }  /* wait for the device to empty buffers */  if(ArchiveReady(3600))  {    errcode=50;    log("Archive or device no longer available");    Report("Archive or device no longer available\nERROR\n");    return;  }  /* Rewind */  if(RewindDevice())  {    Report("Unable to rewind the tape\n");    return;  }  /* If we are creating a tarcdr or tarcdrw backup, now is the time  to move the finished archive to the cd */  if(conf__type==TARCDR || conf__type==TARCDRW || conf__type==TARISO || conf__type==ZIPCDR || conf__type==ZIPCDRW || conf__type==ZIPISO)  {    UI__Message("Final archive\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -