#!/bin/sh
#
# Run a simple backup  then do a
#   Virtual Full backup to another device.
#
# This script uses the disk autochanger
#
TestName="virtual-backup-test"
JobName=Vbackup
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer
change_jobname NightlySave $JobName

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/flat-c.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

#
# Note, we first backup into Pool Default,
#          then Migrate into Pool Full.
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# Write out bconsole commands
cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
@#setdebug level=100 storage=File
label storage=File volume=FileVolume001 Pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Full drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Full drive=0
@# run several jobs
@exec "sh -c 'date > ${BackupDirectory}/date'"
run job=$JobName level=Full yes
wait
messages
list jobs
@exec "sh -c 'touch ${BackupDirectory}/*.c'"
run job=$JobName level=Incremental yes
wait
messages
list jobs
@exec "sh -c 'touch ${BackupDirectory}/*.c'"
run job=$JobName level=Differential yes
wait
messages
list jobs
@exec "sh -c 'date > ${BackupDirectory}/date'"
@exec "sh -c 'touch ${BackupDirectory}/*.c'"
run job=$JobName level=Incremental yes
wait
messages
list jobs
list volumes
@#
@# now do a normal restore of normal backups
@#
setdebug level=10 dir
restore where=${tmp}/bareos-restores select
unmark *
mark *
done
yes
list volumes
@# should Consolidate Full, Differential and Incremental
setdebug level=150 storage=DiskChanger
setdebug level=100 dir
run job=$JobName level=VirtualFull yes
wait
messages
list volumes
list jobs
@#
@# now do a restore of the consolidated Full
@#
restore where=${tmp}/bareos-restores select storage=DiskChanger
unmark *
mark *
done
yes
wait
list volumes
list jobs
messages
quit
END_OF_DATA


run_bareos
check_for_zombie_jobs storage=File
stop_bareos

#
# We only used one log so copy it to the second log
#  so that any restore errors will be picked up
#
cp -f ${tmp}/log1.out ${tmp}/log2.out
check_two_logs
check_restore_diff

# remove restored data as we want to restore it again after bscan run
rm -Rf ${tmp}/bareos-restores


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log3.out
mount storage=DiskChanger volume=ChangerVolume002

purge volume=FileVolume001
purge volume=ChangerVolume001
purge volume=ChangerVolume002

delete volume=FileVolume001 yes
delete volume=ChangerVolume001 yes
delete volume=ChangerVolume002 yes
messages
list jobs
quit
END_OF_DATA

run_bareos
stop_bareos

# prepare BSR file

#echo "volume=ChangerVolume001" >tmp/bscan.bsr
echo "volume=ChangerVolume002" >>${tmp}/bscan.bsr
# If the database has a password pass it to bscan
if test "x${db_password}" = "x"; then
  PASSWD=
else
  PASSWD="-P ${db_password}"
fi



BSCAN_OUT="write_stdin_to_file ${tmp}/log-bscan.out"
if is_debug; then
   BSCAN_OUT="tee ${tmp}/log-bscan.out"
fi
$bin/bscan -c ${conf} $BSCANLIBDBI -B ${DBTYPE} -n ${db_name} -u ${db_user} $PASSWD -m -s -v -b ${tmp}/bscan.bsr Drive-0 2>&1 | $BSCAN_OUT


# do a restore of the bscan-ed content
cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log4.out
@#setdebug level=100 storage=File
@#
@# now do a restore of the consolidated Full
@#
restore where=${tmp}/bareos-restores select storage=DiskChanger
unmark *
mark *
done
yes
wait
list volumes
list jobs
messages
quit
END_OF_DATA


run_bareos
check_for_zombie_jobs storage=File
stop_bareos

#
# We only used one log so copy it to the second log
#  so that any restore errors will be picked up
#
cp -f ${tmp}/log1.out ${tmp}/log2.out
check_two_logs
check_restore_diff

end_test
