#!/bin/sh
#
# Run a backup  on two Volumes
#   then migrate it to another device.
#
# This script uses the virtual disk autochanger
#
TestName="migration-volume-test"
JobName=MigVolBackup
. scripts/functions

scripts/cleanup

#scripts/copy-migration-confs
/bin/cp -f scripts/bareos-dir-migration-sd-sd.conf bin/bareos-dir.conf

# create start script for second sd
/bin/cp -f bin/bareos-ctl-sd bin/bareos-ctl-sd2
#/bin/sed 's/bareos-sd.conf/bareos-sd2.conf/g' < bin/bareos-ctl-sd > bin/bareos-ctl-sd2
sed "s#BAREOS_CONFIG_DIR=.*#BAREOS_CONFIG_DIR=${cwd}/bin/bareos-sd2.conf#g" < bin/bareos-ctl-sd > bin/bareos-ctl-sd2

/bin/cp -f scripts/bareos-sd-migration-sd-sd1.conf bin/bareos-sd.conf
/bin/cp -f scripts/bareos-sd-migration-sd-sd2.conf bin/bareos-sd2.conf

/bin/cp -f scripts/test-bareos-fd.conf bin/bareos-fd.conf
/bin/cp -f scripts/test-console.conf bin/bconsole.conf
/bin/cp -f scripts/test-console.conf bin/bat.conf

scripts/prepare-disk-changer


# 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


#cp ${cwd}/bin/bareos-sd.conf ${cwd}/tmp/1
#sed "s%# Maximum File Size%  Maximum File Size%" ${cwd}/tmp/1 >${cwd}/bin/bareos-sd.conf

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

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=FileVolume001 Pool=Default
label storage=File volume=FileVolume002 Pool=Default
update Volume=FileVolume001 MaxVolBytes=3000000 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 job=$JobName yes
wait
run job=$JobName yes
wait
update volume=FileVolume001 VolStatus=Used
update volume=FileVolume002 VolStatus=Used
@#list volumes
@#list jobs
@# should migrate two jobs
@# setdebug level=11 dir
@echo "Run migrate-volume Job"
@#setdebug level=100 dir
run job=migrate-volume yes
@sleep 5
list jobs
llist jobid=1
llist jobid=2
llist jobid=3
llist jobid=4
llist jobid=5
status storage=DiskChanger
wait
list volumes
list jobs
llist jobid=1
llist jobid=2
llist jobid=3
llist jobid=4
llist jobid=5
sql
select * from JobMedia where JobId=1;

@# Now run a second Migration job, which should do nothing
run job=migrate-volume yes
wait
list jobs
@# ensure we don't pickup old backup by purging volume
@# purge volume=FileVolume001
@# purge volume=FileVolume002
@echo "Now do a restore"
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bareos-restores select storage=DiskChanger
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

bin/bareos-ctl-sd2 start
run_bareos

check_for_zombie_jobs storage=File
bin/bareos-ctl-sd2 stop
stop_bareos

cp -f ${cwd}/tmp/log1.out ${cwd}/tmp/log2.out
check_two_logs
check_restore_diff

# remove restored data as we want to restore it again after bscan run
rm -Rvf ${cwd}/tmp/bareos-restores


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log3.out
mount storage=DiskChanger volume=ChangerVolume002

purge volume=FileVolume001
purge volume=ChangerVolume001
purge volume=ChangerVolume002

delete volume=FileVolume001 yes
delete volume=ChangerVolume001 yes
delete volume=ChangerVolume002 yes
messages
list jobs
quit
END_OF_DATA

bin/bareos-ctl-sd2 start
run_bareos

check_for_zombie_jobs storage=File
bin/bareos-ctl-sd2 stop
stop_bareos

# prepare BSR file

#echo "volume=ChangerVolume001" >tmp/bscan.bsr
echo "volume=ChangerVolume002" >>tmp/bscan.bsr
# If the database has a password pass it to bscan
if test "x${db_password}" = "x"; then
  PASSWD=
else
  PASSWD="-P ${db_password}"
fi



BSCAN_OUT="write_stdin_to_file ${tmp}/log-bscan.out"
if is_debug; then
   BSCAN_OUT="tee ${tmp}/log-bscan.out"
fi
$bin/bscan -c ${conf}/bareos-sd2.conf $BSCANLIBDBI -B ${DBTYPE} -n ${db_name} -u ${db_user} $PASSWD -m -s -v -b $tmp/bscan.bsr Drive-0 2>&1 | $BSCAN_OUT


# do a restore of the bscan-ed content
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log4.out
@#setdebug level=100 storage=File
@#
@# now do a restore of the consolidated Full
@#
restore where=${cwd}/tmp/bareos-restores select storage=DiskChanger
unmark *
mark *
done
yes
wait
list volumes
list jobs
messages
quit
END_OF_DATA

bin/bareos-ctl-sd2 start
run_bareos

check_for_zombie_jobs storage=File
bin/bareos-ctl-sd2 stop
stop_bareos

check_two_logs
check_restore_diff
end_test
