#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a two backup of the Bacula build directory + other files
#   then restore one file and see if we have restore problems
# We need a large directory to store the volume
# MA 1150
#
# The main goal is to have the following layout on the volume
#
# JobMedia <-----------------
# Job1
#   File1
#   File2
# Job2
#   File1
# Job1
#   File2
# JobMedia <-----------------
# Job1
#   File2
#
TestName="restore-multi-session-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

$bperl -e "add_attribute('$conf/bacula-sd.conf', 'MaximumFileSize', '500MB', 'Device')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MaximumVolumeBytes', '10GB', 'Pool')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Label Format', 'Vol-', 'Pool', 'Default')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'FileSet', 'FewFiles', 'Job', 'BackupClient1')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MaximumConcurrentJobs', 10, 'Job')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MaximumConcurrentJobs', 10, 'Client')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MaximumConcurrentJobs', 10, 'Storage')"
$bperl -e "add_attribute('$conf/bacula-sd.conf',  'MaximumConcurrentJobs', 10, 'Device')"
$bperl -e "add_attribute('$conf/bacula-sd.conf',  'CommCompression', 'no', 'Storage')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'CommCompression', 'no', 'Director')"
$bperl -e "add_attribute('$conf/bacula-fd.conf',  'CommCompression', 'no', 'FileDaemon')"
$bperl -e "add_attribute('$conf/bacula-sd.conf', 'MaximumPartSize', '50MB', 'Device')"

#$bperl -e "add_attribute('$conf/bacula-dir.conf', 'SpoolData', 'Yes', 'Job', 'BackupClient1')"
#$bperl -e "add_attribute('$conf/bacula-sd.conf', 'MaximumSpoolSize', '200MB', 'Device')"

change_jobname BackupClient1 $JobName
start_test

# If available, compress files on the FS, we will store only 0
if [ "$FORCE_CLOUD" = "" ]; then
    touch $tmp/Vol-0001 $tmp/Vol-0002

    # might not be available
    chattr +c $tmp/Vol-0001 2> /dev/null
    chattr +c $tmp/Vol-0002 2> /dev/null
fi

cat >> $conf/bacula-dir.conf <<EOF
FileSet {
  Name = "FewFiles"
  Include {
    # start with some small files
    File = "$cwd/build/po"
    File = "$tmp/0.dat"
    File = "$tmp/1.dat"
    File = "$tmp/2.dat"
    File = "$cwd/build/src"
  }
}
EOF
director=`$bperl -e "get_dirname()"`

(
dd if=/dev/zero of=$tmp/0.dat count=1 obs=500M seek=1
dd if=/dev/zero of=$tmp/1.dat count=1 obs=500M seek=1
dd if=/dev/zero of=$tmp/2.dat count=1 obs=500M seek=1
) 2> /dev/null

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setbandwidth limit="82400 kb/s" client
run job=$JobName storage=File1 level=full yes
@sleep 2
run job=$JobName storage=File1 level=full yes
wait
llist jobmedia jobid=1
messages
END_OF_DATA

run_bacula

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log2.out  
@# 
@# now do a restore
@#
setdebug level=500 trace=1 storage=File1
restore where=$tmp/bacula-restores
3
1
cd "$tmp"
mark "1.dat"
done
@sleep 2
@exec "sh -c 'cp $working/*.restore.1.bsr $working/restore1.bsr'"
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File1
stop_bacula

check_two_logs

cat $tmp/1.dat | md5sum > $tmp/1.dat.sum
cat $tmp/bacula-restores/$tmp/1.dat | md5sum > $tmp/1.dat.sum2

diff $tmp/1.dat.sum $tmp/1.dat.sum2
rstat=$?

end_test
