#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Run a simple backup of the Bacula build directory 
#   to a tape where the maximum tape file size is set to 1M
#   then restore a few files from it.  Note, by setting the maximum 
#   file size to 1M, it runs very slow. There are about 64 files that
#   are created during each of the two backups.
#
TestName="restore-by-file-tape"
JobName=restorebyfile
. scripts/functions

require_tape_drive

scripts/copy-tape-confs
scripts/cleanup-tape

echo "${cwd}/build" >${cwd}/tmp/file-list
sed s%\^%${cwd}% ${cwd}/scripts/flist | sort | uniq >${cwd}/tmp/restore2-list

cp ${cwd}/bin/bacula-sd.conf ${cwd}/tmp/1
sed "s%# Maximum File Size%  Maximum File Size%" ${cwd}/tmp/1 >${cwd}/bin/bacula-sd.conf

change_jobname NightlySave $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out   ${cwd}/tmp/log1.out
label storage=tape volume=TestVolume001 slot=0 pool=Default
run job=$JobName level=Full yes
wait
messages
@# 
@# now do a restore
@#
@$out   ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores storage=tape file=<${cwd}/tmp/restore2-list
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=tape
stop_bacula

dstat=0
#
# We need to stop and start Bacula to
#  test appending to a previously written tape
#
for i in `cat ${cwd}/tmp/restore2-list`; do
   diff $i ${cwd}/tmp/bacula-restores$i
   if [ $? != 0 ] ; then
      dstat=1
   fi
done

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out   ${cwd}/tmp/log1.out
run job=$JobName  level=Full yes
wait
messages
@#
@# now do a restore
@#
@$out   ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores
7
<${cwd}/tmp/restore2-list
                                                                                        
yes
wait
messages
quit
END_OF_DATA

#
# Bacula was stopped, but we must restart it to
#  test appending to a previously written tape
#
run_bacula
check_for_zombie_jobs storage=tape
stop_bacula

check_two_logs

for i in `cat ${cwd}/tmp/restore2-list`; do
   diff $i ${cwd}/tmp/bacula-restores$i
   if [ $? != 0 ] ; then
      dstat=1
   fi
done

end_test
