#!/bin/sh
#
# Copyright (C) 2000-2020 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then create some           
#   new files, do an Incremental onto a different medium 
#   and then restore everything.
#
# This script uses the two different tape drives.
#
TestName="incremental-2media-tape"
JobName=Inc2mediaTape
. scripts/functions

require_tape_drive
require_second_drive

scripts/cleanup
# Get conf files
/bin/cp -f scripts/bacula-dir-2d.conf bin/bacula-dir.conf
/bin/cp -f scripts/bacula-sd-2d.conf bin/bacula-sd.conf
/bin/cp -f scripts/bacula-fd-2d.conf bin/bacula-fd.conf
/bin/cp -f scripts/bconsole-2d.conf bin/bconsole.conf

# get proper SD tape definitions
cp -f scripts/linux_tape_options bin/tape_options
if test x`uname` = xFreeBSD ; then
   cp -f scripts/freebsd_tape_options bin/tape_options
fi

mt -f /dev/nst0 status | grep ONLINE >/dev/null
stat=$?
if [ $stat -eq 0 ] ; then
  mt -f /dev/nst0 rewind
  mt -f /dev/nst0 weof
else
  mtx -f $AUTOCHANGER load 1 0
fi
mt -f /dev/nst1 status | grep ONLINE >/dev/null
stat=$?
if [ $stat -eq 0 ] ; then
  mt -f /dev/nst1 rewind
  mt -f /dev/nst1 weof
else
  mtx -f $AUTOCHANGER load 2 1
  mtx -f $AUTOCHANGER status
fi

echo "${cwd}/build" >${cwd}/tmp/file-list

change_jobname $JobName
start_test

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=tape volume=TestVolume001 slot=$SLOT1 Pool=Default drive=$DRIVE1
label storage=DLT80 volume=TestVolume002 Pool=Default
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula

echo "Backup 1 done"
# make some files for the incremental to pick up
touch ${cwd}/build/src/dird/*.c ${cwd}/build/src/dird/*.o
touch ${cwd}/build/src/lib/*.c ${cwd}/build/src/lib/*.o

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# Force Incremental on the second Volume
update volume=TestVolume001 VolStatus=Used
setdebug level=51 storage=DLT80
run level=Incremental job=$JobName storage=DLT80 yes
wait
list volumes
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=51 storage=tape
restore where=${cwd}/tmp/bacula-restores select all done 
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=DLT80
stop_bacula

check_two_logs
check_restore_diff

end_test
