home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / backups / backup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  8.9 KB  |  458 lines

  1. #ifdef lint
  2. static char rcsid[] = "$Header: backup.c,v 1.9 86/10/20 18:37:34 scooter Exp $";
  3. #endif lint
  4. /*
  5.  * backup - a program to control daily dumps
  6.  *
  7.  * This program has been written to control the daily dumping
  8.  * of all of the system's files.  Backup reads from the file
  9.  * /etc/backup_dates to get the days and times that a filesystem
  10.  * should be backed up.  The format of backup_dates is:
  11.  *
  12.  * Day_of_week    Week_of_month    Month    Level    Filesystem    Message
  13.  *
  14.  * where:
  15.  *    Day_of_week is one of {Mon,Tue,Wed,Thu,Fri,Sat,Sun}
  16.  *    Week_of_month is 1-5
  17.  *    Month is one of {Jan,Feb,...,Dec}
  18.  *    Level is 0-9
  19.  *    Filesystem is the NAME of the filesystem
  20.  *    Message is a message to be printed to the operator upon
  21.  *            completion of the dump
  22.  *
  23.  * For example, backup_dates might look like:
  24.  *
  25.  * # Backup schedule for Genie - Genentech, Inc.
  26.  * # Daily level 9's
  27.  * Tue-Fri    *    *    9    /va    "/va dump complete"
  28.  * # Monday dumps - level 1's
  29.  * Mon    2-5    *    1    /    "root dump complete"
  30.  * Mon    2,3,4,5    *    1    /va    "/va dump complete"
  31.  * # Monthly level 0's
  32.  * Mon    1    *    0    /    "root dump complete"
  33.  * Mon    1    *    0    /va    "/va dump complete"
  34.  *
  35.  * Note that the "#" character is used as a comment, and that ranges
  36.  * as well as the "*" may be used.  Obviously ranges and wildcards are
  37.  * only allowed in the Day_of_week and Week_of_month fields.
  38.  *
  39.  * Usage:
  40.  *    backup [-b blocksize] [-s tapelength] [-t device] [-f file ] [-m] [-w n]
  41.  *             [-d density] [date]
  42.  *
  43.  * Diagnostics:
  44.  *    Backup will complain if the input file is garbaged or if the
  45.  *    filesystem is mounted.  The return status from dump is monitored
  46.  *    to track the progress of the dump and to determine if dump
  47.  *    fails.  If it fails, the operator will be prompted to determine
  48.  *    if they want to retry this filesystem.
  49.  *
  50.  * Author: Scooter Morris, Genentech
  51.  * Date: March, 1985
  52.  *
  53.  */
  54.  
  55. #include <stdio.h>
  56. #include <sys/time.h>
  57. #include <fstab.h>
  58. #include <signal.h>
  59. #include <errno.h>
  60. #include <sys/reboot.h>
  61. #include "backup.h"
  62.  
  63. struct tm    *localtime();
  64. struct tm    *convdate();
  65. struct fstab    *fsent;
  66. char         line[180];
  67. extern int    errno;
  68. int        x_opt = NO;
  69.  
  70. char        *day_names[7] =
  71. { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" };
  72.  
  73. char        *month_names[12] =
  74. { "January","February","March","April","May","June","July","August",
  75.   "September","October","November","December" };
  76.  
  77. main(argc, argv)
  78. int argc; char *argv[];
  79. {
  80.     FILE    *file;
  81.     char    *ptr;
  82.     char    fname[BUFSIZ];    /* file name */
  83.     char    tname[BUFSIZ];    /* tape name */
  84.     char    fsname[BUFSIZ];
  85.     char    dump_comm[BUFSIZ];
  86.     char    *tmp,*rindex();
  87.     int    f_opt = NO;
  88.     int    t_opt = NO;
  89.     int    m_opt = NO;
  90.     int    date_opt = 0;
  91.     int    ret = 0;
  92.     int    minutes = 5;
  93.     int    tape_block = BLOCK_SIZE;
  94.     int    tape_size = TAPE_LENGTH;
  95.     int    tape_density = TAPE_DENSITY;
  96.     long    tmptime;    /* place to put time */
  97.     struct    tm *curtim;    /* current time structure */
  98.  
  99.     int    week_day,month_week,month,day,year;
  100.  
  101.     argc--, argv++;
  102.     while (argc > 0) {
  103.         ptr = *argv;
  104.         while (*ptr) switch (*ptr++) {
  105.  
  106.         case '-':
  107.             break;
  108.  
  109.         case 'm':
  110.         case 'M':
  111.             m_opt = YES;
  112.             break;
  113.  
  114.         case 'f':
  115.         case 'F':
  116.             f_opt = YES;
  117.             if (*ptr == 0) {
  118.                 argv++;
  119.                 argc--;
  120.                 if (*argv == 0) {
  121.                 fprintf(stderr,
  122.                     "backup: no file given with '-f'.\n");
  123.                 exit(1);
  124.                 }
  125.                 strcpy(fname,*argv);
  126.             }
  127.             else {
  128.                 strcpy(fname,ptr);
  129.                 *ptr = 0;
  130.             }
  131.             break;
  132.  
  133.         case 't':
  134.         case 'T':
  135.             t_opt = YES;
  136.             if (*ptr == 0) {
  137.                 argv++;
  138.                 argc--;
  139.                 if (*argv == 0) {
  140.                 fprintf(stderr,
  141.                     "backup: no device given with '-t'.\n");
  142.                 exit(1);
  143.                 }
  144.                 strcpy(tname,*argv);
  145.             }
  146.             else {
  147.                 strcpy(fname,ptr);
  148.                 *ptr = 0;
  149.             }
  150.             break;
  151.  
  152.         case 'b':
  153.         case 'B':
  154.             if (*ptr == 0) {
  155.                 argv++;
  156.                 argc--;
  157.                 if (*argv == 0) {
  158.                 fprintf(stderr,
  159.             "backup: no blocking factor given with '-b'.\n");
  160.                 exit(1);
  161.                 }
  162.                 tape_block = atoi(*argv);
  163.             }
  164.             else {
  165.                 tape_block = atoi(ptr);
  166.                 *ptr = 0;
  167.             }
  168.             break;
  169.             
  170.  
  171.         case 'd':
  172.         case 'D':
  173.             if (*ptr == 0) {
  174.                 argv++;
  175.                 argc--;
  176.                 if (*argv == 0) {
  177.                 fprintf(stderr,
  178.             "backup: no density given with '-d'.\n");
  179.                 exit(1);
  180.                 }
  181.                 tape_density = atoi(*argv);
  182.             }
  183.             else {
  184.                 tape_density = atoi(ptr);
  185.                 *ptr = 0;
  186.             }
  187.             break;
  188.  
  189.         case 's':
  190.         case 'S':
  191.             if (*ptr == 0) {
  192.                 argv++;
  193.                 argc--;
  194.                 if (*argv == 0) {
  195.                 fprintf(stderr,
  196.                     "backup: no size given with '-s'.\n");
  197.                 exit(1);
  198.                 }
  199.                 tape_size = atoi(*argv);
  200.             }
  201.             else {
  202.                 tape_size = atoi(ptr);
  203.                 *ptr = 0;
  204.             }
  205.             break;
  206.         case 'w':
  207.         case 'W':
  208.             if (*ptr == 0) {
  209.                 argv++;
  210.                 argc--;
  211.                 if (*argv == 0) {
  212.                 fprintf(stderr,
  213.             "backup: warning time in minutes not given with '-w'.\n");
  214.                 exit(1);
  215.                 }
  216.                 minutes = atoi(*argv);
  217.             }
  218.             else {
  219.                 minutes = atoi(ptr);
  220.                 *ptr = 0;
  221.             }
  222.             break;
  223.         case 'X':
  224.         case 'x':
  225.             x_opt = YES;
  226.             printf("*** Debugging ***\n");
  227.             break;
  228.  
  229.         case '0':
  230.         case '1':
  231.         case '2':
  232.         case '3':
  233.         case '4':
  234.         case '5':
  235.         case '6':
  236.         case '7':
  237.         case '8':
  238.         case '9':
  239.             month = day = year = 0;
  240.             date_opt = sscanf(--ptr,"%d/%d/%d",&month,&day,&year);
  241.             if (date_opt < 2)
  242.             {
  243.                 fprintf(stderr,
  244.                 "backup: illegal date specification\n");
  245.                 exit(1);
  246.             }
  247.             *ptr = 0;
  248.             break;
  249.  
  250.         default:
  251.             fprintf(stderr,
  252.             "Unknown option '%c' - ignored\n",ptr[-1]);
  253.         }
  254.         argc--, argv++;
  255.         *ptr = 0;
  256.     }
  257.  
  258.     quiet = 0;
  259.  
  260.     /* Ignore impatience */
  261.  
  262.     signal(SIGTERM,SIG_IGN);
  263.     signal(SIGHUP,SIG_IGN);
  264.     signal(SIGQUIT,SIG_IGN);
  265.     signal(SIGTSTP,SIG_IGN);
  266.  
  267.     /* get current date */
  268.  
  269.     if (date_opt)
  270.     {
  271.         curtim = convdate(month,day,year);
  272.     } else {
  273.         time(&tmptime);
  274.         curtim = localtime(&tmptime);
  275.     }
  276.  
  277.     week_day = curtim -> tm_wday;
  278.     month_week = ( (curtim -> tm_mday - 1) / 7 ) + 1;
  279.     month = curtim -> tm_mon;
  280.  
  281.     /* get dates file name */
  282.  
  283.     if (! f_opt) sprintf(fname,"%s",BACKUPFILE);
  284.     if (! t_opt) sprintf(tname,"%s",DEVICE);
  285.  
  286. /*
  287.  * Open up the input file
  288.  */
  289.  
  290.     file = fopen(fname,"r");
  291.     if (file == NULL)
  292.     {
  293.         perror(file);
  294.         exit(errno);
  295.     }
  296.  
  297. /*
  298.  * Give introduction
  299.  */
  300.  
  301.     printf("backup: Performing backup for %s %s %d, %d\n",
  302.         day_names[week_day],month_names[month],curtim->tm_mday,
  303.         curtim->tm_year+1900);
  304.  
  305.     line_count = 0;
  306.     while (fgets(line,180,file) != NULL)
  307.     {
  308.         line_count++;
  309.         lineptr = 0;
  310.         if(yyparse() || ignore == YES)continue;
  311.  
  312.         if (x_opt)
  313.         {
  314.             printf("Today: %dw %dd %dm\n",month_week,week_day,month);
  315.             printres("Weeks: ",weeks,week_lst,week_ptr);
  316.             printres("Days: ",days,day_lst,day_ptr);
  317.             printres("Months: ",months,month_lst,month_ptr);
  318.         }
  319.  
  320.         if (check_per(month_week,weeks,week_lst,week_ptr))continue;
  321.  
  322.         if (check_per(week_day,days,day_lst,day_ptr))continue;
  323.  
  324.         if (check_per(month,months,month_lst,month_ptr))continue;
  325.  
  326.         fsent = getfsfile(filesys);
  327.         if (fsent == NULL)
  328.         {
  329.             fprintf(stderr,"*** No filesystem %s\n",filesys);
  330.             continue;
  331.         }
  332.  
  333.         if (m_opt == NO)
  334.         {
  335.         /*
  336.          * Single user - if we already haven't quietize the system
  337.          */
  338.  
  339.             if (!quiet) 
  340.             {
  341.                 fprintf(stderr,"%s\n%s",
  342.     "*** You are about ready to kick off all users!!! ***",
  343.     "*** Are you SURE you want to do this? ");
  344.                 gets(dump_comm);
  345.                 if (dump_comm[0] != 'y' &&
  346.                     dump_comm[0] != 'Y') abort();
  347.                 fprintf(stderr,
  348.     "*** Are you POSITIVE??? ");
  349.                 gets(dump_comm);
  350.                 if (dump_comm[0] != 'y' &&
  351.                     dump_comm[0] != 'Y') abort();
  352.  
  353.                 fprintf(stderr,
  354.     "*** Beginning system shutdown....");
  355.                 quietize(minutes);
  356.                 quiet++;
  357.             }
  358.         }
  359.  
  360.         tmp = rindex(fsent->fs_spec,'/');
  361.         fsname[0] = '\0';
  362.         strncpy(fsname,fsent->fs_spec,(int)tmp-(int)(fsent->fs_spec)+1);
  363.         fsname[(int)tmp-(int)(fsent->fs_spec)+1] = '\0';
  364.         strncat(fsname,"r",1);
  365.         strcat(fsname,++tmp);
  366.  
  367.         printf("To perform level %d dump of %s\n",level,filesys);
  368.         dump_comm[0] = '0';
  369. retry:        while (*dump_comm != 'r' && *dump_comm != 'q')
  370.         {
  371. printf("mount first tape and press 'r' RETURN when ready: ");
  372.             gets(dump_comm);
  373.         }
  374.  
  375.         if (*dump_comm == 'q') continue;
  376.  
  377.         sprintf(dump_comm,"%s %1dnufsdb %s %d %d %d %s\n",
  378.                    DUMPCOM,level,tname,
  379.                    tape_size,tape_density,tape_block,
  380.                    fsname);
  381.  
  382.         ret = system(dump_comm);
  383.  
  384.         sprintf(line,"mt -t %s offl\n",tname);
  385.         system(line);
  386.         printf("*** %s ***\n",string);
  387.     }
  388.  
  389.     printf("Backup is now complete!\n");
  390.     if (m_opt == NO)
  391.     {
  392.         restart();    /* Restart the System */
  393.     }
  394. }
  395.  
  396. char
  397. getnextchar()
  398. {
  399.     return((char)line[lineptr++]);
  400. }
  401.  
  402. check_per(today,target,list,count)
  403. int today,target,count;
  404. int list[];
  405. {
  406.     int i;
  407.  
  408.     if (target == ALL)
  409.         return(0);
  410.     else if (target == LIST)
  411.     {
  412.         for (i = 0 ; i < count ; i++)
  413.         {
  414.             if(list[i] == today)return(0);
  415.         }
  416.     }
  417.     else if (target == RANGE && today >= list[0] && today <= list[1])
  418.         return(0);
  419.  
  420.     return(1);
  421. }
  422.  
  423. printres(string,today,list,count)
  424. char    *string;
  425. int    today;
  426. int    list[];
  427. int    count;
  428. {
  429.     int i;
  430.  
  431.     printf("%s ",string);
  432.     switch(today)
  433.     {
  434.     case ALL:
  435.         printf("ALL ");
  436.         break;
  437.     case LIST:
  438.         printf("LIST ");
  439.         break;
  440.     case RANGE:
  441.         printf("RANGE ");
  442.         break;
  443.     }
  444.  
  445.     printf("(%d)",count);
  446.     for (i = 0 ; i < count ; i++)
  447.     {
  448.         printf(" %d,",list[i]);
  449.     }
  450.     printf("\n");
  451. }
  452.  
  453. abort()
  454. {
  455.     fprintf(stderr,"*** Backup aborted ***\n");
  456.     exit(1);
  457. }
  458.