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


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer
#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
sed 's/migrate/copy/g' ${cwd}/bin/bareos-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/bareos-dir.conf

# 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

#
# Note, we first backup into Pool Default,
#          then Copy 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=Scratch
update Volume=FileVolume001 MaxVolBytes=3000000 pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Scratch drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Scratch 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
messages
@$out ${cwd}/tmp/log11.out
@############################################################
@# should copy one job, and the second is empty
@############################################################
@# setdebug level=11 dir
@echo "Run copy-volume Job"
@#setdebug level=100 dir
run job=copy-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;

messages
@$out ${cwd}/tmp/log2.out
@############################################################
@# now do a restore
@# Expect:
@#  Check that copy uses JobId 1,2
@#  Use File instead of DiskChanger
@#  Files are there
@############################################################
restore where=${cwd}/tmp/bareos-restores select
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
check_restore_diff

# cleanup previous restore
rm -rf ${cwd}/tmp/bareos-restores

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@############################################################
@# Purge File backup, and restore again
@# Expect:
@#  Check that copy uses other JobId
@#  Use DiskChanger
@#  Files are there
@############################################################
@$out ${cwd}/tmp/log3.out
purge volume=FileVolume001 yes
purge volume=FileVolume002 yes
restore where=${cwd}/tmp/bareos-restores select storage=DiskChanger
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=DiskChanger
check_restore_diff

# cleanup previous restore
rm -rf ${cwd}/tmp/bareos-restores
touch ${BackupDirectory}/test.po

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@############################################################
@# Run a new Incremental, restore melting Copy and Backup
@# and copy the new job to Changer
@# Expect:
@#  Files are there
@#  Copy OK
@############################################################
@$out ${cwd}/tmp/log4.out
run job=$JobName yes
wait
messages
restore where=${cwd}/tmp/bareos-restores select all
done
yes
wait
messages
update volume=FileVolume001 VolStatus=Used
update volume=FileVolume002 VolStatus=Used
run job=copy-volume yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=DiskChanger
check_restore_diff

stop_bareos

get_mig_info 1 ${cwd}/tmp/log11.out

if [ -z "$RET" -o "$RET" = 0 ]; then
    bstat=2
    print_debug "The first job must have been copied"
fi

get_mig_info 2 ${cwd}/tmp/log11.out

if [ -n "$RET" ]; then
    bstat=2
    print_debug "The second job has no files, it can't have been copied"
fi

JOBID=`awk -F= '/Job queued. JobId/ {if(!f){print $2; f=1;}}' ${cwd}/tmp/log4.out`
get_mig_info $JOBID ${cwd}/tmp/log4.out

if [ -z "$RET" -o "$RET" = 0 ]; then
    bstat=2
    print_debug "The new incremental job must have been copied"
fi

check_two_logs
end_test
