home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s053 / 5.ddi / usr / bin / .backup next >
Encoding:
Text File  |  1990-12-08  |  9.6 KB  |  424 lines

  1. #!/sbin/sh
  2. #    Copyright (c) 1990 UNIX System Laboratories, Inc.
  3. #    Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
  4. #      All Rights Reserved
  5.  
  6. #    THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
  7. #    UNIX System Laboratories, Inc.
  8. #    The copyright notice above does not evidence any
  9. #    actual or intended publication of such source code.
  10.  
  11. #ident    "@(#)/usr/bin/.backup.sl 1.1 4.0 12/08/90 11862 AT&T-USL"
  12.  
  13. #    Backup script 
  14. #    Options:
  15. #    c - complete backup
  16. #    p - incremental ("partial") backup
  17. #    h - backup history
  18. #    u "user1 [user2]" - backup users (use "all" to backup all users)
  19. #    t - backup done to tape; only used if backing up to tape
  20. #    d - special device name; defaults to /dev/rdsk/f0q15d
  21. #    f "<files>" - backup by file name (argument must be in quotes)
  22.  
  23. USAGE="backup -d "device" [ -t ] [ -c | -p | -u [user] | -f <files> ]\n backup -h"
  24. MEDIA="floppy disk"
  25. CPIO=cpio
  26. CPIOFLGS="-ocB"
  27. DEV=""
  28. BTYPE=""
  29. BACKUP=/etc/Backup
  30. IGNORE=/etc/Ignore
  31. DIR=
  32. trap "cd /; rm -f /tmp/FFILE$$ /tmp/FILE$$ /tmp/VFILE$$ /tmp/flp_index; echo \"\nYou have \
  33. cancelled the Backup\n\"; exit 1 " 1 2 3 9 15
  34.  
  35. dofind() {
  36.     find $1 $2 -print 2>/dev/null | while read file
  37.     do
  38.     if [ -f $file ]
  39.         then du -a $file
  40.     elif [ -d $file ]
  41.         then echo "D\t$file"
  42.     fi
  43. done >> /tmp/FILE$$
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. while getopts tcphu:f:d: c
  51. do
  52.     case $c in
  53.     c)    type=c
  54.         BTYPE="Complete System"
  55.         ;;
  56.     p)    type=p
  57.         BTYPE="Incremental System"
  58.         ;;
  59.     d)    DEV="$OPTARG"
  60.         ;;
  61.     t)    MEDIA="cartridge tape"
  62.         CPIOFLGS="-oc -C 10240 "
  63.         scsi=`devattr $DEV scsi`
  64.         if [ "$scsi" = "true" ]
  65.         then
  66.             CPIOFLGS="-oc -C 65536 "
  67.         fi
  68.         ;;
  69.     h)    type=h;
  70.         ;;
  71.     u)    type=u
  72.         user="$OPTARG"
  73.         if [ -n "$FILES" ]
  74.         then
  75.             echo "Error: -u and -f options can't be used together"
  76.             exit 1
  77.         fi
  78.         BTYPE="User"
  79.         ;;
  80.     f)    type=f
  81.         FILES="$OPTARG"
  82.         if [ -n "$user" ]
  83.         then
  84.             echo "Error: -u and -f options can't be used together"
  85.             exit 1
  86.         fi
  87.         BTYPE="Selective"
  88.         ;;
  89.     \?)    echo "backup: $USAGE"
  90.         exit 0
  91.         ;;
  92.     *)    echo "backup: $USAGE"
  93.         exit 1
  94.         ;;
  95.     esac
  96. done
  97.  
  98. case "$type" in
  99.  
  100.     "")
  101.         echo "backup: $USAGE"
  102.         exit 1
  103.         ;;
  104.     h)
  105.         
  106.         if [ -s /etc/.lastbackup ]
  107.         then
  108.             echo "Last complete backup done on `cat /etc/.lastbackup`"
  109.         else
  110.             echo "No complete backup has been done."
  111.         fi
  112.         if [ -s /etc/.lastpartial ]
  113.         then
  114.             echo "Last incremental backup done on `cat /etc/.lastpartial`"
  115.         else
  116.             echo "No incremental backup has been done."
  117.         fi
  118.         exit 0
  119.         ;;
  120. esac
  121.  
  122.  
  123. shift `expr $OPTIND - 1`
  124.  
  125. if [ ! "$DEV" ]
  126. then
  127.     if [ "$MEDIA" = "cartridge tape" ]
  128.     then
  129.         DEV="/dev/rmt/c0s0"
  130.     elif [ -c "/dev/rdsk/f1"  ]
  131.     then
  132.         /sbin/flop_num
  133.         if [ $? = 1 ]
  134.         then
  135.             DEV="/dev/rdsk/f0"
  136.         else
  137.             while (true)
  138.             do
  139.                 echo "\nThe system has two floppy drives."
  140.                 echo "Strike ENTER to backup to drive 0"
  141.                 echo "or 1 to backup to drive 1.  \c"
  142.                 read ans
  143.                 if [ "x$ans" = "x0" -o "x$ans" = "x" ]
  144.                 then
  145.                     DEV="/dev/rdsk/f0"
  146.                     break
  147.                 elif [ "x$ans" = "x1" ]
  148.                 then
  149.                     DEV="/dev/rdsk/f1"
  150.                     break
  151.                 else
  152.                     continue
  153.                 fi
  154.             done
  155.         fi
  156.     else
  157.         DEV="/dev/rdsk/f0"
  158.     fi
  159. fi
  160.  
  161. MESSAGE="You may remove the $MEDIA. To exit, strike 'q' followed by ENTER.
  162. To continue, insert $MEDIA number %d and strike the ENTER key. "
  163.  
  164. UID=`id | cut -c5`
  165. if [ "$UID" != "0" ]
  166. then
  167.     RNAME=`id | cut -d\( -f2 | cut -d\) -f1`
  168.     if [ "$type" = "c" -o "$type" = "p" ]
  169.     then
  170.         echo "\"$RNAME\" may only select 'Backup History' \
  171. or 'Private Backup'."
  172.         exit 1
  173.     fi
  174.     if [ \( "$type" = u -a "$user" = all \) ]
  175.     then
  176.         echo "\"$RNAME\" may only select 'Backup History' \
  177. or 'Private Backup'."
  178.         exit 1
  179.     fi
  180. fi
  181.  
  182. case $type in
  183.     c)
  184. #    handle complete backup
  185.         TIR=/
  186.         file2=/etc/.lastbackup
  187.         ;;
  188.     p)
  189. #        set up for an incremental update
  190. #        assumption: date IN the file is the same as date OF the file
  191.         if [ ! -s /etc/.lastbackup ]
  192.         then
  193.             echo "A complete backup up must be done before the Incremental backup."
  194.             exit 1
  195.         fi
  196.         if [ ! -s /etc/.lastpartial ]
  197.         then 
  198.             NEWER="-newer /etc/.lastbackup"
  199.         else 
  200.             NEWER="-newer /etc/.lastpartial"
  201.         fi
  202.         file2=/etc/.lastpartial
  203.         TIR=/
  204.         ;;
  205.     f)
  206.         DIR="$FILES"
  207.         NEWER=
  208.         TIR=
  209.         ;;
  210.     u)
  211.         DIR=
  212.         if [ "$user" = all ]
  213.         then
  214.             user="`pwdmenu`"
  215.             if [ "Name=N O N E" = "$user" ]
  216.             then
  217.                echo "There are no users to backup"
  218.                exit 1
  219.             fi
  220.             for i in `pwdmenu`
  221.             do
  222.                 T=`echo $i | cut -d= -f2`
  223.                 T=`grep "^$T:" /etc/passwd | cut -d: -f6`
  224.                 DIR="$DIR $T"
  225.             done
  226.             TIR=/usr
  227.         else
  228.             for i in $user
  229.             do
  230.                 TDIR=`grep "^$i:" /etc/passwd | cut -d: -f6`
  231.                 if [ -z  "$TDIR" ]
  232.                 then
  233.                     echo "$i doesn't exist"
  234.                     continue
  235.                 else
  236.                     DIR="$DIR $TDIR"
  237.                 fi
  238.             done
  239.             if [ -z "$DIR" ]
  240.             then
  241.                 echo "No users to back up."
  242.                 exit 1
  243.             fi
  244.             TIR=/usr
  245.         fi
  246.         ;;
  247.     *)    # THIS CASE SHOULDN'T BE REACHED
  248.         echo "Invalid type $type"
  249.         echo "backup: $USAGE"
  250.         exit 1
  251.         ;;
  252. esac
  253.  
  254. message -d "Calculating approximate number of $MEDIA(s) required. Please wait."
  255.  
  256. #  Compute Blocks, number of floppies etc ..
  257. >/tmp/FILE$$
  258. if [ "$type" = "c" -o "$type" = "p" ]
  259. then
  260.     if [ -s "$BACKUP" ]
  261.     then
  262.         > /tmp/FFILE$$
  263.         for file in `cat $BACKUP`
  264.         do
  265.             if [ -f "$file" ]
  266.             then
  267.                 du -a $file >>/tmp/FFILE$$
  268.             else
  269.                 DIR="$file $DIR"
  270.             fi
  271.         done 
  272.     fi
  273.     if [ -z "$DIR"  -a ! -s /tmp/FFILE$$ ]
  274.     then
  275.         DIR=/
  276.     fi
  277. fi
  278.  
  279. dofind "$DIR" "$NEWER"
  280.  
  281. if [ "$type" = "c" -o "$type" = "p" ]
  282. then
  283.     if [ -s "$IGNORE" ]
  284.     then
  285.         cat $IGNORE | while read file
  286.         do
  287.             if [ -n "$file" ]
  288.             then
  289.                 grep -v "^[0-9D]*    $file" /tmp/FILE$$ >/tmp/VFILE$$
  290.                 mv /tmp/VFILE$$ /tmp/FILE$$
  291.             fi
  292.         done
  293.     fi
  294.     cat /tmp/FFILE$$ >>/tmp/FILE$$ 2>/dev/null
  295. fi
  296.  
  297. grep -v "^D" /tmp/FILE$$ > /tmp/VFILE$$
  298. cp /tmp/FILE$$ /tmp/flp_index
  299. if [ ! -s /tmp/VFILE$$ ]
  300. then
  301.     if [ "$type" != "u" -a "$type" != "f" ]
  302.     then
  303.         if [ ! -s /etc/.lastpartial ]
  304.         then 
  305.             set `cat -s /etc/.lastbackup`
  306.         else 
  307.             set `cat -s /etc/.lastpartial`
  308.         fi
  309.         message -d "There are no new files since \
  310. the last Incremental Backup on $2 $3.  Therefore no Backup will be done."
  311.     else
  312.         echo  "There are no files to be backed up."
  313.     fi
  314.     rm -f /tmp/FFILE$$ /tmp/FILE$$ /tmp/VFILE$$ /tmp/flp_index
  315.     exit 0
  316. fi
  317.  
  318. BLOCKS=`cut -f1 < /tmp/VFILE$$ | sed -e '2,$s/$/+/' -e '$s/$/p/'| dc`
  319.  
  320. # If there are only directories to back up, BLOCKS could be 0.  Make sure
  321. # the message tells the user at least 1 media is needed.
  322.  
  323. if [ $BLOCKS -eq 0 ]
  324. then
  325.     BLOCKS=1
  326. fi
  327. cut -f2 /tmp/FILE$$ > /tmp/VFILE$$
  328.  
  329. # Block maxs are:
  330. # 702 for 360K, 1422 for 720K, 2370 for 1.2MB and 2844 for 1.44MB floppies
  331. # 131072 for the 60MB cartridge tape
  332. # 257000 for the 125MB cartridge tape. This also allows for cpio overhead.
  333.  
  334. if [ "$MEDIA" = "cartridge tape" ]
  335. then
  336.     CTAPEA=`expr \( $BLOCKS / 131072 \) + \( 1 \& \( $BLOCKS % 131072 \) \)`
  337.     CTAPEB=`expr \( $BLOCKS / 257000 \) + \( 1 \& \( $BLOCKS % 257000 \) \)`
  338.     CTAPEC=`expr \( $BLOCKS / 514000 \) + \( 1 \& \( $BLOCKS % 514000 \) \)`
  339.     TIME=`expr ${CTAPEA} \* 30`
  340.     echo /tmp/flp_index > /tmp/FILE$$
  341.     cat /tmp/VFILE$$ >> /tmp/FILE$$ 2>/dev/null
  342.     echo "BLOCKS=$BLOCKS" >> /tmp/flp_index
  343.     rm -f /tmp/VFILE$$
  344.     message "The backup will need approximately:\n\
  345. $CTAPEA cartridge tape(s) for a 60 MB drive or \n\
  346. $CTAPEB cartridge tape(s) for a 125 MB drive \n\
  347. $CTAPEC cartridge tape(s) for a 320 MB drive \n\
  348. and will take no more than $TIME minute(s).\n\n\
  349. Please insert the first cartridge tape.  Be sure to number the cartridge tape(s) \
  350. consecutively in the order they will be inserted."
  351. else
  352.     FP5D9=`expr \( $BLOCKS / 690 \) + \( 1 \& \( $BLOCKS % 690 \) \)`
  353.     FP5H=`expr \( $BLOCKS / 2360 \) + \( 1 \& \( $BLOCKS % 2360 \) \)`
  354.     FP3H=`expr \( $BLOCKS / 2830 \) + \( 1 \& \( $BLOCKS % 2830 \) \)`
  355.     FP3Q=`expr \( $BLOCKS / 1410 \) + \( 1 \& \( $BLOCKS % 1410 \) \)`
  356.     TIME=`expr \( ${FP5D9}0 / 15 \) + \( 1 \& \( ${FP5D9}0 % 15 \) \)`
  357.     echo /tmp/flp_index > /tmp/FILE$$
  358.     cat /tmp/VFILE$$ >> /tmp/FILE$$ 2>/dev/null
  359.     echo "BLOCKS=$BLOCKS" >> /tmp/flp_index
  360.     rm -f /tmp/VFILE$$
  361.     message "The backup will need approximately:\n\
  362. $FP5H formatted 1.2MB 5.25\" floppy disk(s) or\n\
  363. $FP5D9 formatted 360KB 5.25\" floppy disk(s) or\n\
  364. $FP3H formatted 1.44MB 3.5\" floppy disk(s) or\n\
  365. $FP3Q formatted 720KB 3.5\" floppy disk(s).\n\
  366. and will take no more than $TIME minute(s).\n\n\
  367. Please insert the first floppy disk.  The floppy disk(s) you are using \
  368. for the backup MUST be formatted.  Be sure to number the floppy disks \
  369. consecutively in the order they will be inserted."
  370. fi
  371.  
  372. echo "\n$BTYPE backup in progress\n\n\n"
  373.  
  374. cat /tmp/FILE$$ | ${CPIO} ${CPIOFLGS} -M"$MESSAGE" -O $DEV
  375. err=$?
  376. if [ "$err" = "4" ] 
  377. then
  378.     message -d "You have cancelled the Backup to $MEDIA(s)."
  379. elif [ "$err" != "0" ] 
  380. then
  381.     if [ "$MEDIA" = "cartridge tape" ]
  382.     then
  383.         message -d "An error was encountered while writing to the ${MEDIA}. \
  384. Please be sure the $MEDIA is inserted properly, and wait for the notification \
  385. before removing it."
  386.     else
  387.         message -d "An error was encountered while writing to the ${MEDIA}. \
  388. Please be sure all your $MEDIA(s) are formatted, and wait for the notification \
  389. before removing them."
  390.     fi
  391. else
  392.     if [ $type = c ]
  393.     then
  394.         message -d "The Complete Backup is now finished."
  395.     elif [ $type = p ]
  396.     then
  397.         set `cat -s /etc/.lastbackup`
  398.         eval M=$2 D=$3
  399.         if [ "$NEWER" = "-newer /etc/.lastbackup" ]
  400.         then 
  401.             set `cat -s  /etc/.lastbackup`
  402.         else 
  403.             set `cat -s  /etc/.lastpartial`
  404.         fi
  405.         message -d  "The incremental backup of the files that have changed \
  406. since the last backup, on $2 $3, is now finished.\n\n\
  407. In order to restore the system totally, first restore the last \
  408. complete backup dated $M $D, and then restore the incremental backup(s) that \
  409. you have performed since $M $D starting with earliest one and \
  410. ending with the one you have just finished."
  411.     else
  412.         message -d "Backup is now completed."
  413.     fi
  414.     if [ "$TIR" = "/" ]
  415.     then
  416.         if [ "$file2" = "/etc/.lastbackup" ]
  417.         then 
  418.             rm -f /etc/.lastpartial
  419.         fi
  420.         date > $file2
  421.     fi
  422. fi
  423. rm -f /tmp/FFILE$$ /tmp/FILE$$ /tmp/VFILE$$ /tmp/flp_index
  424.