#!/bin/sh
#
# Run a simple backup using the compressed option
#   then backup four times, each with incremental then finally restore.
#   It should require at least 4 different bsrs.
#
TestName="relabel-tape"
JobName=Relabeltape
. scripts/functions

require_tape_drive

scripts/cleanup-tape
scripts/copy-tape-confs

change_jobname $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


start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=150 storage=tape
label storage=tape volume=TestVolume001 slot=0 pool=Default
run job=$JobName level=Full yes
wait
messages
add pool=Default storage=tape
0
TestVolume002
@# set status to append
update volume=TestVolume001
1
.
run job=$JobName level=Full yes
wait
list volumes
unmount storage=tape
unmount storage=tape
purge volume=TestVolume001
relabel oldvolume=TestVolume001 volume=TestVolume003 slot=0 pool=Default storage=tape
list volumes
mount storage=tape
messages
wait
run job=$JobName level=Full yes
wait
list volumes
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=tape


echo "Backup done"
#
# now do several restores to ensure we cleanup between jobs
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
setdebug level=150 storage=tape
list volumes
restore where=${cwd}/tmp/bareos-restores select all done
yes
wait
restore where=${cwd}/tmp/bareos-restores select all done
yes
wait
@$out ${cwd}/tmp/log2.out
@#
@# now unmount the tape and start two restores
@#  at the same time
@#
list volumes
unmount storage=tape
restore where=${cwd}/tmp/bareos-restores select all done
yes
restore where=${cwd}/tmp/bareos-restores select
unmark *
mark *
done
yes
mount storage=tape
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=tape
stop_bareos

check_two_logs
check_restore_diff
end_test
