home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Utilities / TarChive / UserBack < prev   
Text File  |  1992-08-24  |  8KB  |  241 lines

  1. #! /usr/local/bin/perl
  2. # Object:
  3. #     Users should be able to back up their own files.  Package to do so needs
  4. #     to do the following:
  5. #     1.    Label the tape.  This label controls the access to the tape.
  6. #         E.g. if the tape label is not marked for group to read,  other 
  7. #         users cannot read the tape.  (Writing & reading 
  8. #        currently limited to owners only.)
  9. #     2.    For each user, keep a record what's on that tape.
  10. #     3.    Enable users to append information onto a tape.
  11. #     4.    Keep track of the how much space remains on the tape.  
  12. #    (Not yet implemented)
  13. #     5.    Make it reasonable for users to recover items from a tape.  
  14. #    (Separate Script)(Not yet implemented)
  15. # Algolrithms & methods
  16. #    The backup machine has a registry of tapes, 
  17. #    Users can label a blank tape with the 
  18. #    /usr/local/bin/LabelTape program. 
  19. #    See that script for details.
  20. #
  21. #    Each user has a directory ~/backups, that belongs to root.
  22. #    In that directory, every time a tar file is put on tape, 
  23. #    the package creates a log file with a list of the contents 
  24. #    of the tar file. This makes it easy for users to keep track 
  25. #    of what they have. 
  26.  
  27. #    
  28. #     TapeLibrary sets up the drive, and does a bunch of initialization.
  29.  
  30. require("TapeLibrary.perl");
  31. $BackupType="Private ";
  32.  
  33.  
  34. # Define labels, strings, and so on.
  35.  
  36. @DirList = @ARGV;
  37. if ($DirList[0] eq "-V") {
  38.     print "VERIFY PASS ONLY  NO BACKUPS WILL BE MADE \n\n";
  39.     $VERIFYONLY = true;
  40.     shift @DirList;
  41.     }
  42. $udir = $ENV{HOME};
  43. if ($udir eq "") { print "No home directory.\n"; &Quit;}
  44. $backdir = $udir."/".backups;
  45. $pwd = `pwd`;
  46. chop($pwd);
  47.  
  48. # Check that the user has a directory for log files
  49.     if ( ! -d $backdir ) {
  50.         print "You do not have a directory for logging backup results.\n";
  51.         print "        See your System Administrator.\n";
  52.         &Quit;
  53.         }    
  54.  
  55. #     Check that all the directories asked for actually exist.
  56. #    Will check as an absolute path, relative to current path, 
  57. #   then relative to home directory.
  58.  
  59.     $ok = true;
  60.     print " \n Checking to see that everything you want to  \n";
  61.     print " backup actually exists.  (Humans are such rotten typists...)\n\n";
  62.     foreach $DirList ( @DirList ){
  63.         #print $DirList, "\n";        
  64.         if ( substr($DirList,0,1) eq "/") {
  65.             #print "$DirList is an absolute path \n";
  66.             next;
  67.             }    
  68.         if (-e $DirList ) {
  69.             #print "$DirList is relative to current directory  \n";
  70.             $DirList = $pwd."/".$DirList;
  71.             next;
  72.             }    
  73.         if (-e $udir."/".$DirList){
  74.             #print "$DirList is relative to home directory  \n";
  75.             $DirList = $udir."/".$DirList;
  76.             next;
  77.             }
  78.         else {
  79.             print "Humble Tape Servant is unable to find $DirList\n";
  80.             print "Perhaps the great one has made a tiny typing error.\n\n";
  81.             print "The learned master is reminded that directories \n" ;
  82.             print "must be relative to his home directory,\n" ;
  83.             print "the current directory, or must be absolute paths.\n\n";
  84.             &Quit;
  85.             }
  86.         }    #End while -- All DirList now converted to absolute path names.
  87.  
  88.     #    Now we check that the user owns the directories in question.
  89.     #    He's bumped if he doesn't own the directory.
  90.     
  91.     foreach $DirList ( @DirList ){
  92.         if (! -o $DirList) {
  93.             print "Least worthy servant has discovered that the \n";
  94.             print "directory $DirList exists, but you do not own it.\n";
  95.             print "Must humbly decline making this copy.\n\n";
  96.             &Quit;
  97.             }
  98.         else {
  99.             print "Verified existence & ownership of ", $DirList, "\n";
  100.             }
  101.         }
  102.     print "\n Hit y,Y, or <CR> to proceed, anything else to abort:  ";
  103.     $answer = getc;
  104.     $answer =~ tr/yY\n/Y/;
  105.     if ($answer ne "Y") { &Quit; }
  106.  
  107. #
  108. # =========================
  109. #
  110. #     Read Volume label
  111. #
  112.     $tapelabel=&CheckVolumeLabel($Code);
  113.     print "Tape is labeled $tapelabel\n";
  114. #
  115. #    Check the label
  116.  
  117. #    A series of checks are made.  CheckVolumeLabel tries to read 
  118. #    the first file on the tape.  If that returns blank, or more than a 
  119. #    single line, or a single line that doesn't match with the name of tape, 
  120. #    or a tape name that is not owned by the user, then the user is bumped. 
  121.  
  122.     if ($Code == 100 ) {
  123.         print "Verified that this tape is registered on this server.\n";
  124.         print "Verified that $user owns this tape. \n";
  125.         }
  126.     if ($Code == 101 ) {
  127.           print "Begging forgiveness for rude interuption but this tape has no label.\n";
  128.         print "Humble tar slave is not permitted to write to an unlabeled tape.\n\n";
  129.         &Quit(102);
  130.         }
  131.     if ( ! -e "/usr/local/tapelabels/$tapelabel" ) {
  132.           print "Excuse Interuption, but this label is not registered\n";
  133.         print "Cannot write to unregistered tape.  \n";
  134.         &Quit(101);
  135.         }
  136.     if ( ! -o "/usr/local/tapelabels/$tapelabel" ) {
  137.           print "Most Humble Apologies, but you do appear to own this tape.\n";
  138.         print "Only the Owner can write on a tape.\n";
  139.         &Quit(102);
  140.         }
  141.  
  142. #    Check that the user has a backups directory.  This should be owned
  143. #    by root to prevent users from deleting the log files, which is how
  144. #    we keep track of how many files are on the tape.
  145.  
  146.     chdir $udir; 
  147.     if ( ! -d  $backdir ) {
  148.         print "You don\'t have a backup logging directory";
  149.         print "Logging will fail, and everyone will get confused";
  150.         print "See Y.F.N.S.A. for assistance.";
  151.         &Quit;
  152.         }
  153.  
  154. #        Position the tape to the current end of tape.
  155.  
  156. #        Since Next doesn't implement an mt eom command (shame!)
  157. #    we instead count the number of log files that already refer 
  158. #    to this tape, and space forward appropriately.  This can 
  159. #    cause trouble if the script is aborted during the Verify pass.
  160.  
  161.     $FilesOnTape=`ls -1 ${backdir}/${tapelabel}-* | wc -l`;
  162.     chop($FilesOnTape);    
  163.     $tarnum = $FilesOnTape;
  164.     if     ( $tarnum == "0" ) {
  165.         print "\nNo previous data is registered on this tape.\n";
  166.         print "       Proceeding with backup. \n";
  167.         }
  168.     else{
  169.         print "This tape has $tarnum file(s) on it already.\n"; 
  170.         print "     Moving past these files...\n\n";
  171.         }
  172. #
  173. #    So we move forward the number of backups on the tape + 1 for the tapelabel
  174. #    But Verify does one, so we do tarnum now, and the other one after
  175. #    the VerifyOnly branch
  176.  
  177. #label.  
  178.     &ForwardSpaceTape( $tarnum );
  179.     if ($ErrorCode != 200) {
  180.         print "Failed to move past existing files on tape.\n";
  181.         print "Error Code: $ErrorCode.\n";
  182.         &ErrorTranslate($ErrorCode);
  183.         &Quit;
  184.         }
  185.  
  186.     if ($VERIFYONLY) {goto VERIFY;}
  187.     &ForwardSpaceTape(1);
  188.  
  189. #
  190. # and then back to the front end of the tapemark.
  191. # We erase, because this may be a restarted script.
  192. #
  193.     &BackSpaceTape(1);
  194.     &EraseRestOfTape;
  195.         
  196. # Ok, tape is positioned, now make the backups.
  197. # Each backup is made from the parent directory of it,
  198. # to make it easier to place recovered files in arbitrary places.
  199. #
  200.     foreach $direct ( @DirList ){
  201.         print "\n\nStarting backup of directory $direct. \n";
  202.         # $DirList has been converted to absolute pathnames.
  203.         &TarDir($direct);
  204.     }    # End Foreach
  205.  
  206. #     =========================================================    #
  207. #                Reposition tape and compare
  208. #     =========================================================    #
  209. print "Backing up to today's starting point.\n";
  210. $BErrorCode = &BackSpaceTape($tarnum - $FilesOnTape +1 );
  211.  
  212. #+1? Yeah.  After writing, the tape is just after a tape mark. 
  213. # So it takes 1 to get at the front of that mark, then one for each
  214. # of the tars added (tarnum - filesontape)
  215.  
  216. $tarnum =  $FilesOnTape;
  217. # Tarnum should always be the number of marks to move, from the beginning
  218. #of the tape to get to the mark immediately before the current working
  219. #location. 
  220. #At this point we should be at the BOT side of the tape of the last tar
  221. #previously written on the tape.  Remember that verify starts with an fsf.
  222.  
  223.         
  224. VERIFY:{    
  225.     foreach $direct ( @DirList ){
  226.         &VerifyDir($backdir,$direct);
  227.         }    # End Foreach
  228.     }    # End VERIFY
  229.  
  230. # =========================
  231. #
  232. #  Housekeeping:
  233. # Take tape offline.
  234. #
  235.     print "Ok, all done.  Please pick up your tape.\n";
  236.     &Quit(0);
  237. #
  238.  
  239.