Bacula Test Restore Before Job Script

The following RunBeforeJob Bacula script scans every configured client, and submit restore jobs for each one of them, using last terminated backup jobs. The quantity of random restored files is custom, and the restore client is fixed.

#!/bin/bash
#
# Um Script para Bacula Restore Test
#
# Seleciona, aleatoriamente, x (files_per_job) para restaurar de cada Bacula Configured Client para um Restore Client fixo.
#
# Sugestão e local:
#
# Bacula Community: /etc/bacula/scripts/restore_test.sh
# Bacula Enterprise: /opt/bacula/scripts/restore_test.sh
#
# By Heitor Faria - http://www.bacula.com.br | http://bacula.us
# Alterações efetuadas por Wanderlei Hüttel -  https://github.com/wanderleihuttel
# Testes: Molinero - https://github.com/molinux
#
#######

bconsole=$(which bconsole)
restore_client="srv_bacula-fd"
restore_job="Restore_Files"
restore_where="/tmp/bacula_restore"
dir_tmp="/tmp"
files_per_job=10
debug=1 #0-Executa as restauracoes | #1-Apenas imprime os comandos

for client in $(echo ".clients" | ${bconsole} | sed '1,4d' | grep -v "You have messages" | sort);  do
    jobid=$(echo -e "gui on \nlist jobs client=${client} order=desc limit=1 jobstatus=T" | ${bconsole} | sed '1,9d' | grep "|" | cut -d"|" -f 2 | sed 's/\s//g')
    echo "llist files jobid=${jobid}" | ${bconsole} | sed '1,9d' | grep "|" | cut -d"|" -f 2 | sed 's/^[ \t]*//;s/[ \t]*$//' | sort -R | head -n ${files_per_job} > ${dir_tmp}/files_${jobid}.txt
    total_files=$(cat ${dir_tmp}/files_${jobid}.txt | wc -l)
    # Executa apenas jobs com pelo menos 1 arquivo
    if [ $total_files -gt 0 ]; then
        # Debug - Se 0 executa o restore, se 1 apenas imprime os comandos
        if [ $debug -eq 0 ]; then
            echo "restore jobid=\"${jobid}\" client=\"${client}\" restoreclient=\"${restore_client}\" restorejob=\"${restore_job}\" file=\"</tmp/files_${jobid}.txt\"  where=\"${restore_where}\" done yes" | ${bconsole}
        else
            echo "restore jobid=\"${jobid}\" client=\"${client}\" restoreclient=\"${restore_client}\" restorejob=\"${restore_job}\" file=\"</tmp/files_${jobid}.txt\"  where=\"${restore_where}\" done "
        fi
    fi
done

Don’t forget to provide execution permission to the script. E.g.:

chmod o+rx /etc/bacula/scripts/restore_test.sh

Create an Admin Job at bacula-dir.conf in order to execute the script when desired. E.g.:

Job {
  Name = "RestoreTestJobsCreator"
  JobDefs = "DefaultJob"
  Type = Admin
  Client=hfaria-K46CB-fd
  Messages = Standard
  Schedule = "test_schedule"
  RunBeforeJob = /etc/bacula/scripts/restore_test.sh
}

Create the dedicated Schedule to run the tests series (e.g.: every wonderful Monday):

Schedule {
  Name = "test_schedule"
  Run = Full  monday at 12:00
}

Check the Restore Job logs for eventual errors and also the health of the restored files at the Restore Client machine.


 

Disponível em: pt-brPortuguês (Portuguese (Brazil))enEnglishesEspañol (Spanish)

Leave a Reply