[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bacula-users
Subject:    Re: [Bacula-users] Tape check
From:       Bill Arlofski <waa-bacula () revpol ! com>
Date:       2018-08-11 19:49:38
Message-ID: b40e9f97-18a3-2bcd-7691-f9b5b2de4297 () revpol ! com
[Download RAW message or body]

On 08/09/2018 01:00 PM, Steven Hammond wrote:
> We are looking at implementing a tape check procedure.   Essentially, once a
> quarter we will routinely verify that various tapes are still readable and we
> can actually restore files from them (as a test). Our volumes are YEARLY,
> MONTHLY, WEEKLY, and DAILY.   I was wondering if there might be a way to store
> in the database with the volume somewhere a date, for example, of when we last
> tested the tape. Yes, I could just create a spreadsheet and keep track, but I
> didn't know if there was already a feature in Bacula (we are using version
> 9.06).   Any suggestions would be greatly appreciated.
> 
> Steven Hammond
> Technical Chemical Company
> Cleburne, TX

Hello Steve,

OK, this script is finished and attached as promised.


In addition to adding a comment to all the volumes that were read in a
specified restore job (or appending a volume comment if one already exists),
this script will do the same for the restore job itself so an admin can also
quickly know which Restore jobs have had their volumes commented, and which
have not.

Just edit some variables near the top including the volume and job comment
'templates' and you should be set.

I have tested this script extensively here, and it works exactly as expected,
but since I do make a couple 'sql' calls from in bconsole to update the volume
and job comment fields, please review this script carefully, and test in a
sandbox environment if there are any concerns.

Hope this is useful to someone 'as-is', or maybe as an example to do something
else.   :)

I'll post it to my website at some later time.

Best regards,

Bill


-- 
Bill Arlofski
http://www.revpol.com/bacula
-- Not responsible for anything below this line --

["checkAndCommentOnRestoreVolumesAndJobs.sh" (application/x-shellscript)]

#!/bin/bash
#
#  - checkAndCommentOnRestoreVolumesAndJobs.sh
#
#  A script which, given a Restore Job's jobid will determine all the volumes
#  that were read during the restore, then either add or append a comment to the
#  volume's 'comment' field in the catalog's 'media' table. Additionally, this
#  script will add or append a comment to the Restore Job's 'comment' field in
#  the catalog's 'job' table.
#
# ------------------------------------------------------------------------------
#
# Copyright (c) 2018, William A. Arlofski waa-at-revpol-dot-com
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1.  Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2.  Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ------------------------------------------------------------------------------


# Set some variables
# ------------------
now="$(date +%Y%m%d-%H%M%S)"
bcbin="/usr/sbin/bconsole"
bcconfig="/etc/bacula/bconsole.conf"
newvolcomment="A successful test restore using this volume was done ${now} with \
restore JobId $1" newjobcomment="This restore job was checked for volume comments \
with \"$0\" on ${now}"


# ------------------------------------------------
# Nothing should need to be edited below this line
# ------------------------------------------------

# Do some simple tests to make sure command
# line parameter exists and is a number
# -----------------------------------------
# Short and sweet, but a non-POSIX bashism :(
# if [ -z ${hist} ] || ! [[ ${hist} =~ ^[0-9]+$ ]]; then
jobidisnum=$(echo $1 | grep -E '^[0-9]+$')
if [ -z $1 ] || [ -z ${jobidisnum} ]; then
	echo
	echo "USE: $0 <Restore jobid>"
	echo
	exit 1
fi

# Test the binary, and configuration file
# ---------------------------------------
if [ ! -e ${bcconfig} ]; then
	echo
	echo "The bconsole configuration file does not seem to be '${bcconfig}'."
	echo "Please check the setting for the variable 'bcconfig'."
	echo
	exit 1
fi

if [ ! -x ${bcbin} ]; then
	echo
	echo "The bconsole binary does not seem to be '${bcbin}', or it is not executable."
	echo "Please check the setting for the variable 'bcbin'."
	echo
	exit 1
fi

echo "- The jobid passed to the script was: $1"
echo "- Verifying that it was a Restore job"
jobsummary=$(echo -e "list jobid=$1;\n\nquit" | ${bcbin} -c ${bcconfig})

jobtype=$(echo "${jobsummary}" | grep -A2 "^| jobid" | tail -n1 | awk '{ print $9 }')
if [ ! "X${jobtype}" = "XR" ]; then
	echo "- The job being checked (jobid $1) is not a Restore job"
	echo "- Exiting..."
	exit 1
		else
			echo "- The job being checked (jobid $1) is a Restore job... Continuing."
fi

restorejobstatus=$(echo "${jobsummary}" | grep -A2 "^| jobid" | tail -n1 | awk '{ \
print $17 }') if [ ! "X${restorejobstatus}" = "XT" ]; then
	echo "- The Restore job (jobid $1) did not complete successfully!"
	echo "- Not checking for volumes that were read during the restore, nor updating any \
volume comment(s)"  echo "- Exiting..."
	exit 1
		else
			echo "- The Restore job (jobid $1) completed successfully... Continuing."
fi

volumes=$(echo -e "list joblog jobid=$1\n\quit" | ${bcbin} -c ${bcconfig} | grep \
"Ready to read from volume" | cut -d\" -f2) for volume in $(echo ${volumes}); do
	echo "- Checking to see if volume \"${volume}\" exists in catalog"
	volnotexists=$(echo -e "list volume=${volume}\nquit" | ${bcbin} -c ${bcconfig} | \
grep "No results")  if [ ! -z "${volnotexists}" ]; then
		echo "- Volume \"${volume}\" does not exist in the catalog."
		echo "- Not attempting to read nor update its comment field"
		else
			echo "- Volume \"${volume}\" exists in catalog..."
			echo "- Checking comment for \"${volume}\""
			volcomment=$(echo -e "llist volume=${volume}\nquit" | ${bcbin} -c ${bcconfig} | \
grep "comment:" | awk '{ $1=""; print }' | sed 's/^ //')  if [ ! -z "${volcomment}" \
]; then  echo "- Volume has the current comment: \"${volcomment}\""
				echo "- Appending new volume comment..."
				volcomment="${volcomment} **** $newvolcomment"
				else
					echo "- Volume has no comment... Adding new one."
					volcomment="${newvolcomment}"
			fi
			# waa - 20180810 - Need a new bconsole feature to allow "update volume=xxxx \
comment=yyyy" to avoid direct SQL update  #                  Additionally, bconsole \
seems to strip out the ability to insert a line feed into the 'comment'  #            \
text field in the media table so I currently use " **** " to separate the statements \
about  #                  when a test restore was done from this volume.
			# ------------------------------------------------------------------------------------------------------------------
  # echo -e "update volume=${volume} comment=${comment}\nquit" | ${bcbin} -c \
${bcconfig}  # <---- This would be ideal  echo -e "sql\nUPDATE media SET \
comment='${volcomment}' WHERE volumename='${volume}';\n\nquit" | ${bcbin} -c \
${bcconfig} 2>&1 > /dev/null  fi
done

# Now, check the Restore job's comment and add or append one
# ----------------------------------------------------------
echo "- Checking comment for jobid \"$1\""
jobcomment=$(echo -e "llist joblog jobid=$1\nquit" | ${bcbin} -c ${bcconfig} | grep \
"comment:" | awk '{ $1=""; print }' | sed 's/^ //') if [ ! -z "${jobcomment}" ]; then
	echo "- Job has the current comment: \"${jobcomment}\""
	echo "- Appending new job comment..."
	jobcomment="${jobcomment} **** $newjobcomment"
	else
		echo "- Job has no comment... Adding new one."
		jobcomment="${newjobcomment}"
fi
echo -e "sql\nUPDATE job SET comment='${jobcomment}' WHERE jobid='$1';\n\nquit" | \
${bcbin} -c ${bcconfig} 2>&1 > /dev/null echo "- Finished..."

# -------------
# End of script
# -------------


# ----------
# Change Log
# ----------
# ----------------------------
# William A. Arlofski
# Reverse Polarity, LLC
# helpdesk@revpol.com
# http://www.revpol.com/bacula
# ----------------------------
#
# waa - 20180810 - Script to automatically add or append a comment to a volume
#                  after a restore job is complete. This script is most usedful
#                  when called in a "special" Restore job after the restore
#                  finishes, but can also be called manually or from a cron job.
#                  It needs to be called from the Restore job's RunScript with:
#                  Command = "/path/to/checkAndCommentOnRestoreVolumes.sh %i"
#
# waa - 20180811 - Initial release with the following addition:
#                - Add or append a comment to the restore job itself so an admin can
#                  quickly tell if this script had been called against that restore \
job. #---------------------------------------------------------------------------------------


# I like small tabs. Use  :set list in vim to see tabbing etc
# vim: set tabstop=2:softtabstop=2:shiftwidth=2 #



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Bacula-users mailing list
Bacula-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-users


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic