#!/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. We test if we restore
#   deleted files after a set of virtualfull
#
# This script uses the disk autochanger
#
TestName="virtual-deleted-files-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

# We do a first full backup, then 2 incr, then we merge the two incremental
# jobs, then we restore and compare.

# 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
@exec "sh -c 'date > ${cwd}/build/date'"
run job=$JobName level=Full accurate=yes yes
wait
messages
list jobs
@exec "sh -c 'touch ${cwd}/build/inc1'"
@exec "sh -c 'touch ${cwd}/build/inc2'"
run job=$JobName level=Incremental accurate=yes yes
wait
messages
@exec "sh -c 'rm -f ${cwd}/build/date'"
@exec "sh -c 'rm -f ${cwd}/build/inc2'"
run job=$JobName level=Incremental accurate=yes yes
wait
messages
list jobs
run job=$JobName jobid=2,3 level=VirtualFull accurate=yes yes
wait
messages
delete jobid=2,3
@# 
@# now do a restore of the consolidated Full
@#
@$out $tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select all done yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File

stop_bacula
check_two_logs
$rscripts/diff.pl -s $cwd/build -d $tmp/bacula-restores/$cwd/build

end_test
