#!/bin/sh
#
# Run a simple backup. Then create some
#   new files, do an Incremental and restore those two files.
#
TestName="incremental-test"
JobName=Incremental
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs
change_jobname CompressedTest $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/small.tgz

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

echo "${BackupDirectory}/ficheriro1.txt" >${tmp}/restore-list
echo "${BackupDirectory}/ficheriro2.txt" >>${tmp}/restore-list


start_test

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
@#setdebug level=100 storage=File
@#setdebug level=200 client
@#setdebug level=100 director
label storage=File volume=TestVolume001
label storage=File volume=TestVolume002
run job=$JobName yes
status client
wait
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
#
# Now create two new files to be restored later
#
#sleep 1
echo "ficheriro1.txt" >${BackupDirectory}/ficheriro1.txt
echo "ficheriro2.txt" >${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
@# Force Incremental on the second Volume
update volume=TestVolume001 VolStatus=Used
run level=Differential job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole

#sleep 1
touch ${BackupDirectory}/ficheriro1.txt
touch ${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
run level=Incremental job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole

#sleep 1
echo "next ficheriro1.txt" >>${BackupDirectory}/ficheriro1.txt
echo "next ficheriro2.txt" >>${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
run level=Differential job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole

#sleep 1
touch ${BackupDirectory}/ficheriro1.txt
touch ${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
run level=Incremental job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole

#sleep 1
touch ${BackupDirectory}/ficheriro1.txt
touch ${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
run level=Incremental job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole

#sleep 1
touch ${BackupDirectory}/ficheriro1.txt
touch ${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
run level=Incremental job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole

# make sure, timestamp differs
sleep 1

echo "last test" >> ${BackupDirectory}/ficheriro1.txt
echo "last test" >> ${BackupDirectory}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
setdebug level=200 storage=File
run level=Incremental job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out
setdebug level=10 storage=File
restore where=${tmp}/bareos-restores storage=File file=<${tmp}/restore-list
yes
wait
status client
status storage=File
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs
check_restore_files_diff `cat ${tmp}/restore-list`

end_test
