#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then do a 
#   Virtual Full backup to another device.
#
# This script uses the disk autochanger
#
TestName="virtual-jobid-test"
JobName=Vbackup
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
echo "${cwd}/build" >${cwd}/tmp/file-list

rm -f $cwd/build/inc1 $cwd/build/inc2 $cwd/build/diff1

change_jobname NightlySave $JobName
start_test

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

# +-------+---------+-------+----------+----------+-----------+
# | JobId | Name    | Level | JobFiles | JobBytes | JobStatus |
# +-------+---------+-------+----------+----------+-----------+
# | 1     | Vbackup | F     | 1754     | 50118554 | T         |
# | 2     | Vbackup | I     | 1        | 0        | T         |
# | 3     | Vbackup | D     | 2        | 0        | T         |
# | 4     | Vbackup | I     | 1        | 0        | T         |
# | 5     | Save    | F     | 1754     | 50118554 | T         |
# +-------+---------+-------+----------+----------+-----------+

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/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
@# JobId 1
@exec "sh -c 'date > ${cwd}/build/date'"
run job=$JobName level=Full yes
wait
messages
list jobs
@exec "sh -c 'touch ${cwd}/build/inc1'"
@# JobId 2
run job=$JobName level=Incremental yes
wait
messages
list jobs
@exec "sh -c 'touch ${cwd}/build/diff1'"
@# JobId 3
run job=$JobName level=Differential yes
wait
messages
list jobs
@exec "sh -c 'touch ${cwd}/build/inc2'"
@# JobId 4
run job=$JobName level=Incremental yes
wait
messages
list jobs
@# JobId 5
run job=Save level=Full yes
wait
messages
list jobs
@# should Consolidate Full, Incremental
@$out $tmp/log5.out
setdebug level=100 dir trace=1
@# JobId 6 -- should consolidate JobIds 1,3,4
run job=$JobName jobid=4 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log6.out
@# JobId 7
run job=$JobName jobid=3 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log7.out
@# JobId 8  - Should consolidate JobIds 1,2
run job=$JobName jobid=2 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log8.out
@# JobId 9
run job=$JobName jobid=1,2 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log9.out
@# JobId 10
run job=$JobName jobid=1,3 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log10.out
@# JobId 11
run job=$JobName jobid=1,3,4 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log11.out
@# JobId 12 -- should consolidate JobIds 1,2,3,4
run job=$JobName jobid=1-5 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log12.out
@# JobId 13 -- should consolidate JobIds 2,4
run job=$JobName jobid=2-5 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log13.out
@# JobId 14
run job=$JobName jobid=2,4 level=VirtualFull yes
wait
messages
list jobs
@$out $tmp/log14.out
@# JobId 15 -- should consolidate 1-5 (note alljobid)
run job=$JobName alljobid=1-5 level=VirtualFull yes
wait
messages
list jobs
@# 
@# now do a restore of the consolidated Full
@#
@$out $tmp/log2.out
restore jobid=8 where=${cwd}/tmp/restore1
m *
done
yes
wait
messages
restore jobid=7 where=${cwd}/tmp/restore2
m *
done
yes
wait
messages
restore jobid=6 where=${cwd}/tmp/bacula-restores
m *
done
yes
wait
messages
@$out $tmp/log3.out
.bvfs_lsfiles jobid=1 path=$cwd/build/ pattern=README$
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File

fileid=`awk '/README/ { print $2 }' $tmp/log3.out`

cat <<EOF > $tmp/bconcmds
@$out $tmp/log4.out
.bvfs_delete_fileid fileid=$fileid jobid=1
run job=$JobName jobid=1 level=VirtualFull yes
wait
messages
restore jobid=19 where=$tmp/bacula-restores2
m *
done
yes
wait
messages
quit
EOF

run_bconsole

stop_bacula

#
# We only used one log so copy it to the second log
#  so that any restore errors will be picked up
#
check_two_logs
check_restore_diff

grep 'Termination: *Restore OK' $tmp/log4.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: restore in error in $tmp/log4.out"
    estat=1
fi

if [ -f $tmp/bacula-restore2/$cwd/build/README ]; then
    print_debug "ERROR: Should not find README in $tmp/bacula-restore2"
    estat=1
fi

grep  'JobIds=1,2,3,4$' $tmp/log11.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: jobid list is not right in $tmp/log11.out"
    estat=1
fi

grep  'JobIds=2,3,4$' $tmp/log12.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: jobid list is not right in $tmp/log12.out"
    estat=1
fi

grep  'using Differential level' $tmp/log12.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Final level should be differential in $tmp/log12.out"
    estat=1
fi

grep  'JobIds=2,4$' $tmp/log13.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: jobid list is not right in $tmp/log13.out"
    estat=1
fi

grep  'using Incremental level' $tmp/log13.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Final level should be incremental in $tmp/log13.out"
    estat=1
fi

grep  'JobIds=1,2,3,4,5$' $tmp/log14.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: jobid list is not right in $tmp/log14.out"
    estat=1
fi


if [ ! -f $tmp/restore1/$cwd/build/inc1 -o -f $tmp/restore1/$cwd/build/diff1 -o -f $tmp/restore1/$cwd/build/inc2 ]; then
    print_debug "ERROR: should find only inc1 in restore1"
    estat=1
fi

if [ ! -f $tmp/restore2/$cwd/build/inc1 -o ! -f $tmp/restore2/$cwd/build/diff1 -o -f $tmp/restore2/$cwd/build/inc2 ]; then
    print_debug "ERROR: should find only inc1 and diff1 in restore2"
    estat=2
fi

if [ ! -f $tmp/bacula-restores/$cwd/build/inc1 -o ! -f $tmp/bacula-restores/$cwd/build/diff1 -o ! -f $tmp/bacula-restores/$cwd/build/inc2 ]; then
    print_debug "ERROR: should find inc1, diff1 and inc2 in bacula-restores"
    estat=2
fi

end_test
