#! /usr/local/bin/perl 
#Copyright 1992	Sherwood Botsford

####################################################################
#                         Full User Back                           #
#                        Sherwood Botsford                         #
#                   sherwood@space.ualberta.ca                     #
#                            CAUTION:                              #
#              NO WARRANTY IS EXPRESSED OR IMPLIED.                #
#              SUITABILITY FOR TASK NOT GUARANTEED.                #
#                      USE AT YOUR OWN RISK.                       #
####################################################################
#         Feel free to modify this script for local use.           #
#          If you come up with something really clever,            #
#              let me know. Ditto if you find a bug.               #
#                                                                  #
#       If you want to be kept up to date on new versions,         #
#                      let me know that too.                       #
####################################################################


####################################################################
#                                                                  #
#               Define labels, strings, and so on.                 #
#                                                                  #
####################################################################
#	So that I can time the script, and see how/when it dies
print `date`;

#	This library also does some setup.
#   Worth looking at before you go on.  
#	Located in  /usr/local/lib/perl

require("TapeLibrary.perl");

#	The tape library can do various kinds of backups.  The type of backup
#	is recorded in the the log file created during the verify pass.
#	See also UserBack for private user backups.
#	Possible other values for this string would be System Backup, Private
#	Backup. etc.
#	A similar module could be used for incremental backups.  This would
#	require modification of the the subroutines TarDir, or the creation
#	of a new subroutine, as TarDir currently doesn't have provision for 
#	making incremental backups.

$BackupType="Full User ";

#	Customize
#	You may wish to change ypcat to cat (for local users only) or 
#   nidump (NeXT)
#
#	You will certainly need to change the names of the hosts that serve
#	user directories.

@userlist=split(' ',`(ypcat passwd | egrep "arafel|fenris" |awk -f: '\{print(\$1)\}' | sort )`);

&Say( @userlist);
$ErrorCode=0;	#	Used for reporting back errors from subroutines.

#Uncomment next line for testing
#(Of course you will provide your own list of small users)
#@userlist=(barry, beth, cliff, guy, karen, greg, myriam );	


####################################################################
#            TapeLibrary has already rewound the tape,             #
#                and has set the ioctl and drive.                  #
#               Now we will check the Volume label,                #
#                       and clear the tape.                        #
#        Log files with this tape's name are now worthless         #
#                        So we toast them.                         #
#                                                                  #
####################################################################

#	CheckVolumeLabel returns the error number if something is wrong 
#	and the tapelabel if all is ok.  So $ErrorCode has to be checked. 


$tapelabel = &CheckVolumeLabel;
print "Tape is labeled: $tapelabel\n";
if ($LabelErrorCode != 100){
	&ErrorTranslate($LabelErrorCode);
	&Quit(ErrorCode);
	}

#
#		We have now established that the tape is legitimate, and that
#	root has appropriate permission to write on the tape.  Now
#	to actualy do the job.  (Permission is required as the backup 
#	script usually runs in the middle of the night.  If someone else
#	left a tape in the drive it would get clobbered.)
#


print "Deleting log files refering to this tape \n";

#	Customize:
#	Put in the file systems where user home directories live.
#	NOTE:  The command below assumes that these directories 
#	contain ONLY user home directories.  

foreach $UserFileSystem ("/u/arafel", "/u/fenris"){
	system("rm -f $UserFileSystem/*/backups/$tapelabel*");
	}

#	Script can be called with -V for a verify only pass.
# 	so skip making the tar files if thats the case.

if ($ARGV[0] eq "-V") {goto VERIFY;}

# At this point the tape is positioned at the end of the 
# tape label.  We will write an Logical End of Tape mark to
# overwrite tape.

&EraseRestOfTape;

# 	=========================================================	#
#  		Create a tar file for each user on this disk.
# 	=========================================================	#


#	Customize. 
#	This program puts log files in a directory called 
#	backups.  If it doesn't exist it is created, with 
#	root being the owner. Read and execute are set so that 
#	the user can read the directory and files in it but
#	not fiddle with the stuff in it. 


foreach $u ( @userlist )	{
	$Label = $tapelabel."-".$DateString."-".$u;
	$udir = (getpwnam($u))[7];
	print "\n Writing $u\'s home directory $udir to tape.\n";
	if ( ! -d "${udir}/backups" ) {
		print "Creating backup log file directory for $u. \n";
		mkdir($udir."/backups",655);
		}
	&TarDir( $udir);
	}	# End Foreach



# 	=========================================================	#
#				Reposition tape and compare
# 	=========================================================	#
&RewindTape;
VERIFY:{
	print "STARTING VERIFY PASS";
	$tarnum = 0;
	# Remember that VerifyDir updates tarnum.
	# Remember that VerifyDir expects to fsf before doing the read
	foreach $loop ( @userlist )	{
		$udir = (getpwnam($loop))[7];
		$tarlabel=$udir."/backups/".$tapelabel."-".$DateString."-".$loop;
		print "\nUser directory: ",$udir, "\ntarlabel: ",$tarlabel,"\n";
				
		&VerifyDir(${udir}."/backups",$udir);
		}	# End Foreach
	}	#End VERIFY
print `date`;
#	End Program
  &Offline;


# gnutar return errors.
#define EX_SUCCESS      0               /* success! */
#define EX_ARGSBAD      1               /* invalid args */
#define EX_BADFILE      2               /* invalid filename */
#define EX_BADARCH      3               /* bad archive */
#define EX_SYSTEM       4               /* system gave unexpected error */
#define EX_BADVOL       5               /* Special error code means
#                                          Tape volume doesn't match the one
#                                          specified on the command line */

