#!/bin/bash
# Paranoid Backup User Interface  version 1.0

# If you define PB_config as '', then you will be prompted for which
# configuration to use. If you define PB_config as anything else, you
# will not be prompted for which configuration to use, and you will not
# be able to use any other configuration than the one defined here. If
# you have only one configuration, then you should set it here, and then
# you will not have to deal with the which configuration prompt. If you
# have more than one configuration, you should define PB_config as ''.
PB_config=''
#PB_config='/root/Paranoidbackup/all'

# the next line may help if ls and perl do not agree on the correct sort
# order.
#export LC_COLLATE=POSIX

SingleQuote=\'
DoubleQuote=\"
BackSlash=\\
DollarSign=\$

# this if block is a subscript which does the dirty work of
# the kludge described below
if test "$1" = "--sort_output_of_pb_find"
then ControlM='
'
     while read REPLY
     do echo -n " $SingleQuote"'                 '"$REPLY"'          '"$SingleQuote"
        read REPLY
        echo -n " ${SingleQuote}${ControlM}${REPLY}${SingleQuote} off"
     done
     exit
fi


function undo_service_shutdown  {
  umount /namelessp2/dos
  umount /namelessp2/var
  umount /namelessp2/root
  if cat /proc/mounts | grep --quiet -e namelessp2
  then echo error unmounting nfs. are there any process using the nfs drives?
       exit 1
  fi
  mount /dos
  mount /windows
  mount /os2
  mount /slackware-2.3.0
  . /etc/gpm
  lpd
  inetd
  portmap
  rpc.mountd
  rpc.nfsd
  mount /namelessp2/root
  mount /namelessp2/var
  mount /namelessp2/usr
  mount /namelessp2/dos
  return
  }


if ! cd /root/Paranoidbackup
then echo pbui: unable to cd to /root/Paranoidbackup
     exit 1
fi


clear
cat << end_of_message
do you want to shut down unused services and unmount unused drives, to free
up memory and cpu time for Paranoid Backup; and verify that nfs is working?
press y or Y and then enter to do these things, or press enter to not do
these things.
end_of_message
read REPLY
if test "$REPLY" = "Y" -o "$REPLY" = "y" ; then
  PB_service_shutdown=yes
  umount /namelessp2/windows
  umount /namelessp2/dos
  umount /namelessp2/usr
  umount /namelessp2/var
  umount /namelessp2/root
  if cat /proc/mounts | grep --quiet -e namelessp2
  then echo error unmounting nfs. are there any process using the nfs drives?
       exit 1
  fi
  umount /afloppy
  umount /bfloppy
  umount /cdrom
  umount /dos
  umount /windows
  umount /os2
  umount /slackware-2.3.0
  killall rpc.nfsd
  killall rpc.mountd
  killall portmap
  killall inetd
  killall lpd
  gpm -k
  mount -t nfs namelessp2:/ /namelessp2/root -o rw,intr
  mount -t nfs namelessp2:/var /namelessp2/var -o rw,intr
  mount -t nfs namelessp2:/dos /namelessp2/dos -o rw,intr
  if ! test -d /namelessp2/root/lib/modules
  then echo no access to root partition of namelessp2
  exit 1
  elif ! test -f /namelessp2/root/root/.bashrc
  then echo no access to root partition of namelessp2
  exit 1
  elif ! test -d /namelessp2/var/home/k/Web_page/Notes+Scripts
  then echo no access to /var partition of namelessp2
  exit 1
  elif ! test -f /namelessp2/dos/k/rbcomm/telnets/im.mac
  then echo no access to dos partition of namelessp2
  exit 1
  fi
fi


clear
cat << end_of_message
Paranoid Backup: To choose an action, press a letter and then enter
A  check configuration
B  backup
C  compare
D  compare last backup
E  exclude newly installed files
F  find some files
G  compare selected files  (this will prompt you for what files to compare)
H  help
I  find and compare some files
J  find and restore some files
L  list
M  make virtual restore list
P  prepare for installing new files
Q  quit Paranoid Backup
R  restore  (rebuild tape index before doing this)
S  restore selected files  (this will prompt you for what files to restore)
T  rebuild tape index  (restore configuration files before doing this)
U  restore configuration files
V  version
W  backup configuration files
X  desperate restore 1a
Y  desperate restore 1b
Z  desperate restore 2
end_of_message

PB_action=
PB_second_action=
PB_action_description=
while test "$PB_action" = "" ; do
read REPLY
if test "$REPLY" = "A" -o "$REPLY" = "a" ; then
  PB_action=--check-configuration
elif test "$REPLY" = "B" -o "$REPLY" = "b" ; then
  PB_action=--backup
elif test "$REPLY" = "C" -o "$REPLY" = "c" ; then
  PB_action=--compare
elif test "$REPLY" = "D" -o "$REPLY" = "d" ; then
  PB_action=--compare-last-backup
elif test "$REPLY" = "E" -o "$REPLY" = "e" ; then
  PB_action=--exclude-new-files
elif test "$REPLY" = "F" -o "$REPLY" = "f" ; then
  PB_action=--find
elif test "$REPLY" = "G" -o "$REPLY" = "g" ; then
  PB_action=--compare-selected-files
  PB_action_description=compare
elif test "$REPLY" = "H" -o "$REPLY" = "h" ; then
  exec paranoidbackup.pl --help
elif test "$REPLY" = "I" -o "$REPLY" = "i" ; then
  PB_action=--find
  PB_action_description=compare
  PB_second_action=--compare-selected-files
elif test "$REPLY" = "J" -o "$REPLY" = "j" ; then
  PB_action=--find
  PB_action_description=restore
  PB_second_action=--restore-selected-files
elif test "$REPLY" = "L" -o "$REPLY" = "l" ; then
  PB_action=--list
elif test "$REPLY" = "M" -o "$REPLY" = "m" ; then
  PB_action=--make-virtual-restore-list
elif test "$REPLY" = "P" -o "$REPLY" = "p" ; then
  PB_action=--prepare-for-new-files
elif test "$REPLY" = "Q" -o "$REPLY" = "q" ; then
  if test "$PB_service_shutdown" = "yes" ; then undo_service_shutdown; fi
  exit
elif test "$REPLY" = "R" -o "$REPLY" = "r" ; then
  PB_action=--restore
elif test "$REPLY" = "S" -o "$REPLY" = "s" ; then
  PB_action=--restore-selected-files
  PB_action_description=restore
elif test "$REPLY" = "T" -o "$REPLY" = "t" ; then
  PB_action=rebuild_tape_index
elif test "$REPLY" = "U" -o "$REPLY" = "u" ; then
  PB_action=restore_config_files
elif test "$REPLY" = "V" -o "$REPLY" = "v" ; then
  exec paranoidbackup.pl --version
elif test "$REPLY" = "W" -o "$REPLY" = "w" ; then
  exec /root/Paranoidbackup/backup_config_files
elif test "$REPLY" = "X" -o "$REPLY" = "x" ; then
  PB_action=--desperate-restore-1a
elif test "$REPLY" = "Y" -o "$REPLY" = "y" ; then
  PB_action=--desperate-restore-1b
elif test "$REPLY" = "Z" -o "$REPLY" = "z" ; then
  PB_action=--desperate-restore-2
else
  echo \'$REPLY\' is not one of the choices
fi
done



if test "$PB_action" = "restore_config_files"
then echo
     echo type your floppy device and press enter.
     echo if you press enter without typing anything,
     echo /dev/fd0 will be assumed
     read REPLY
     if test "$REPLY" != ""
     then floppy_device=$REPLY
     fi
     echo
     echo type the name of your floppy mount point and press enter.
     echo if you press enter without typing anything,
     echo /mnt/floppy will be assumed
     echo insert your config files floppy before pressing enter
     read REPLY
     if test "$REPLY" != ""
     then floppy_mount_point=$REPLY
     fi
     if ! test -d /mnt/floppy
     then mkdir --parents /mnt/floppy || exit 1
     fi
     mount -t auto /dev/fd0 /mnt/floppy  || exit 1
     cd /mnt/floppy
     echo
     echo files on this floppy:
     ls
     echo type the name of the archive of your config files and press
     echo enter, or press enter without typing anything to abort
     read REPLY
     cd /
     if test "$REPLY" != ""
     then tar -xzf /mnt/floppy/$REPLY
          if test $? != 0
          then echo press enter to continue
               read REPLY
          fi
     fi
     umount /mnt/floppy
     exec $0 $*
     exit
fi






if test "$PB_config" = "" ; then
clear
cat << end_of_message
Which Paranoid Backup configuration do you want to use? Press a key and then
press enter. If you only have one configuration, you might want to edit the
shell script '$0' and remove this menu.

A  all
2  all2
R  files in the root filesystem, excluding /home and /usr
H  files in /home
J  junk486 set 1
K  junk486 set 2
N  namelessp2 set 1
M  namelessp2 set 2
U  files in /usr

Q  quit Paranoid Backup

end_of_message
while test "$PB_config" = "" ; do
read REPLY
if test "$REPLY" = "A" -o "$REPLY" = "a" ; then
  PB_config=/root/Paranoidbackup/all
elif test "$REPLY" = "2" ; then
  PB_config=/root/Paranoidbackup/all2
elif test "$REPLY" = "H" -o "$REPLY" = "h" ; then
  PB_config=/root/Paranoidbackup/home_files
elif test "$REPLY" = "J" -o "$REPLY" = "j" ; then
  PB_config=/root/Paranoidbackup/junk486_1
elif test "$REPLY" = "K" -o "$REPLY" = "k" ; then
  PB_config=/root/Paranoidbackup/junk486_2
elif test "$REPLY" = "N" -o "$REPLY" = "n" ; then
  PB_config=/root/Paranoidbackup/namelessp2_1
elif test "$REPLY" = "M" -o "$REPLY" = "m" ; then
  PB_config=/root/Paranoidbackup/namelessp2_2
elif test "$REPLY" = "Q" -o "$REPLY" = "q" ; then
  if test "$PB_service_shutdown" = "yes" ; then undo_service_shutdown; fi
  exit
elif test "$REPLY" = "R" -o "$REPLY" = "r" ; then
  PB_config=/root/Paranoidbackup/root_files
elif test "$REPLY" = "U" -o "$REPLY" = "u" ; then
  PB_config=/root/Paranoidbackup/usr_files
else
  echo \'$REPLY\' is not one of the choices
fi
done
fi



if test "$PB_action" = "rebuild_tape_index"
then $PB_config/mdti/start --rebuild-index
     exec $0 $*
fi




dialog --checklist "select any options you want to use" 25 80 19 \
  --force '' off \
  --include-corrupted-files '' off \
  --check-control-file '' off \
  --verify-backup '' off \
  2> /tmp/pbui$$.tmp
PB_options=`cat /tmp/pbui$$.tmp | sed -e "s/$DoubleQuote//g"`
rm /tmp/pbui$$.tmp





PB_pattern=
if test "$PB_action" = "--find" ; then
clear
cat << 'end_of_message'
What file or files do you want to find? Type a pattern and press enter, and
Paranoid Backup will display all the files which match the pattern. For
example, the pattern '^tmp\\/' will match all files in directory tmp, and
pattern '\\.tmp$' will match all *.tmp files. Usually you have to enclose the
pattern in single quotes. You must put two backslashes wherever you want one
backslash because bash will interpret backslashes while it reads whatever
you type. If you want to give more than one pattern, try
something like:   '\\.tmp$' --pattern='\\.bak$' --pattern='\\.swp$'              

end_of_message
while test "$PB_pattern" = "" ; do
read PB_pattern
done
PB_pattern=--pattern=$PB_pattern
fi




PB_names=
if test "$PB_action" = "--restore-selected-files" -o \
        "$PB_action" = "--compare-selected-files"; then
clear
cat << end_of_message
What file or files do you want to $PB_action_description? Type a file name and press enter.
You must give the number of the backup which contains the file, then a
colon, then the complete file name. For example:
9:home/john/notes
If you want to give more than one file name, try something like:
9:home/john/notes --file-name=9:home/john/foo --file-name=9:home/john/bar

end_of_message
while test "$PB_names" = "" ; do
read PB_names
done
PB_names=--file-name=$PB_names
fi




PB_config="--config=$PB_config/config"




echo ===================================================== >>output
echo "running 'paranoidbackup.pl $PB_action $PB_config $PB_pattern $PB_names $PB_options'" 2>&1 | tee --append output
echo Paranoid Backup started at: >> output
date +%c >> output
if test "$PB_second_action" = "" ; then
  paranoidbackup.pl $PB_action $PB_config $PB_pattern $PB_names $PB_options 2>&1 | tee --append output
  echo exit code is $? >> output
  echo Paranoid Backup finished at: >> output
  date +%c >> output
  if test "$PB_service_shutdown" = "yes" ; then undo_service_shutdown; fi
  exit
fi
# if the action is find and if the pattern includes quotes, then we have
# to create a new bash process to handle the quotes. I think it is easier
# to create a new bash process always, rather than making the effort to test
# to see if this is the special case which requires a new bash process. That
# is why the next line begins with 'bash -c'
bash -c "paranoidbackup.pl $PB_action $PB_config $PB_pattern $PB_names $PB_options | tee /tmp/pbui$$.tmp" 2>&1 | tee --append output
echo exit code is $? >> output
echo Paranoid Backup finished at: >> output
date +%c >> output

# if /tmp/pbui$$.tmp is empty, then no files matched the pattern
if ! test -s /tmp/pbui$$.tmp
then echo "$0: file /tmp/pbui$$.tmp is empty; probably this means that"
     echo "there are no files which match    $PB_pattern"
     echo "$0: file /tmp/pbui$$.tmp is empty; probably this means that" >> output
     echo "there are no files which match    $PB_pattern" >> output
     rm /tmp/pbui$$.tmp
     if test "$PB_service_shutdown" = "yes" ; then undo_service_shutdown; fi
     exit
fi

# KLUDGE!
bash -c "dialog --separate-output --checklist ${SingleQuote}select the files you want to $PB_action_description$SingleQuote 25 80 18`$0 --sort_output_of_pb_find < /tmp/pbui$$.tmp`" 2> /tmp/pbui$$.2.tmp
# if the exit code is 255, that means there was an error in dialog; and the
# output of dialog is the error message
T=$?
if test "$T" = "255"
then echo ===================================================== >>output
     echo "running dialog --separate-output --checklist ${SingleQuote}select the files you want to $PB_action_description$SingleQuote 25 80 18`$0 --sort_output_of_pb_find < /tmp/pbui$$.tmp`" >>output
     cat /tmp/pbui$$.2.tmp >> output
     cat /tmp/pbui$$.2.tmp
     echo "exit code is $T" >> output
     echo "exit code is $T"
     rm /tmp/pbui$$.tmp
     rm /tmp/pbui$$.2.tmp
     if test "$PB_service_shutdown" = "yes" ; then undo_service_shutdown; fi
     exit $T
fi
# next line strips leading and trailing spaces
bash -c 'while read REPLY;do echo $REPLY;done' < /tmp/pbui$$.2.tmp > /tmp/pbui$$.tmp
rm /tmp/pbui$$.2.tmp
echo ===================================================== >>output
echo "running ${SingleQuote}paranoidbackup.pl $PB_second_action $PB_config --file-names-from=/tmp/pbui$$.tmp $PB_options$SingleQuote" 2>&1 | tee --append output
echo Paranoid Backup started at: >> output
date +%c >> output
paranoidbackup.pl $PB_second_action $PB_config --file-names-from=/tmp/pbui$$.tmp $PB_options 2>&1 | tee --append output
echo exit code is $? >> output
echo Paranoid Backup finished at: >> output
date +%c >> output
rm /tmp/pbui$$.tmp

if test "$PB_service_shutdown" = "yes" ; then undo_service_shutdown; fi
exit

The part where this converts the output of --find into a dialog box for you
to select which files to compare or restore is a kludge. Dialog does not do
what I want because it does not like checkbox items which are more than one
line. By trial and error I discovered that I could trick dialog into doing
something vaguely resembling what I wanted by inserting a control-m
(carriage return) into each checkbox item and by inserting spaces into each
checkbox tag. The result looks very strange: the current item spills over
into the next items, so you can read the current item, but you cannot read
the next item until you make it the current item; and the checkbox which
shows whether each file is selected or unselected is not visible until you
select or unselect it. But it does work, and you will get used to the
bizarre appearance and behavior. What this really needs is for someone to
fix dialog so that dialog will allow checkbox items to be more than one line
(and to respond to pageup, pagedown, home, etc keys; and to respond to the
mouse; and to have a search function for long lists). But I am far too lazy
to do that. If you have too much free time on your hands, why don't you try
to fix dialog?

Even though this works for me, I am worried that my kludge is not very
portable; it may not work with other architectures, other terminal types,
other versions of unix/linux, or other versions of dialog. My computer has
Intel x86 architecture; Linux 2.0.32; and my dialog says it is version 0.3
patched to version 0.4, although the dialog man page says it is version 0.6.

After you do something with pbui, pbui exits automatically. This is because
I think that a normal user is not going to do a backup, and then immediately
want to do another backup. The exceptions are restore configuration files,
and rebuild tape index. This is because I think that if a normal user does
restore configuration files, then the user will probably want to do rebuild
tape index next; and if a user does rebuild tape index, then the user will
probably want to do restore next (and after doing restore, the user will
probably be done).

The section for restoring configuration files needs to mount a floppy to
retrieve the configuration files. However, the only time you would want to
restore the configuration files is when recovering from a hard drive crash,
and all your configuration options would have been lost in the hard drive
crash. So the section for restoring configuration prompts for configuration
information, makes mount points, uses all mount options in case /etc/fstab
is not available, etc. But if pbui was also lost in the hard drive crash,
then you cannot use pbui to restore your configuration files; you will have
to manually restore your configuration files. The only time you would want
to use pbui to restore your configuration files is if your hard drive
crashed, and if you reinstalled linux, and if pbui is part of your
linux distribution.

we could save disk space by compressing the paranoid backup files and
uncompressing them as needed: at the begining of this script,
do something like this:
 gzip --uncompress --to-stdout PB.tgz | tar --extract --file=-
and at the end of this script do something like this:
 rm paranoidbackup.pl kxarc compare_file_lists.pl mdti.mdisks.pl mdti.tapes.pl





If this version of pbui is too complicated for you, the following is a simpler
version. If that is still too complicated for you, you can skip pbui and use
paranoidbackup.pl directly.
#!/bin/sh
cd /root/Paranoidbackup || exit
echo ===================================================== >>output
echo Paranoid Backup started at: >> output
date +%c >> output
paranoidbackup.pl --config=/root/Paranoidbackup/all/config $1 $2 $3 $4 $5 $6 $7 $8 $9 2>&1 | tee --append output
echo exit code is $? >> output
echo Paranoid Backup finished at: >> output
date +%c >> output
exit


