Bacula Reclaim Space Script

From Tech Wiki
Jump to navigation Jump to search

Create a file called in the /root directory. To do this do the following:

cd /root

Then copy and paste the following in to that file:

## get a list of volumes that do exist on the disk but do not in the catalog.
CATALOGVOLS=$(mktemp -p /tmp catvols.XXXXXXXX);
DISKVOLS=$(mktemp -p /tmp diskvols.XXXXXXXX);
mysql -N -u root bacula <<<'SELECT VolumeName from Media' >$CATALOGVOLS;
find /home/bacula -name "srv-*" -printf "%f\n" | egrep -v \.bsr$ >$DISKVOLS;
## iterate through catalogvols and look for the diskvol entry
cat $DISKVOLS | while read VOL; do
if ! grep -q $VOL $CATALOGVOLS; then
echo -n $VOL does not exist! Deleting ..;
find /home/bacula -name $VOL -exec rm -f {} \;
echo "Deleted"
## Get a list of jobs which failed and are using a volume unnecessarily.
mysql -N -u root bacula <<<'
SELECT DISTINCT Job.JobId,VolumeName FROM Media LEFT JOIN (Job, JobMedia)
ON (Media.MediaId = JobMedia.MediaId
AND Job.JobId = JobMedia.JobId
AND Job.JobStatus NOT IN ("C","R","e","T"))
WHERE Media.VolumeName LIKE "srv-%"' | while read DATA; do
JOBID=$(echo $DATA | tr -s " " | cut -d " " -f1)
VOL=$(echo $DATA | cut -d" " -f2)
COUNT=$(mysql -N -u root bacula <<<'SELECT COUNT(DISTINCT Job.JobId) from Job, Media, JobMedia WHERE VolumeName="'$VOL'" AND Media.MediaId = JobMedia.MediaId AND Job.JobId = JobMedia.JobId AND JobStatus IN ("C","R","e","T")')
if [ "$COUNT" -gt "0" ]; then
# the volumes in use. Don’t delete!
if [ "$JOBID" != "NULL" ]; then
echo -e "delete job JobID=$JOBID\nyes\nquit\n" | /usr/sbin/bconsole 2>&1 >/dev/null
echo -n "Deleting $VOL as unusable.. "
echo -e "delete volume=$VOL\nyes\nquit\n" | /usr/sbin/bconsole 2>&1> /dev/null
find /home/bacula -name $VOL -exec rm -f {} \;
echo "OK";

Then use the following command to make the script executable:

chmod +x

Then execute the file using the following command: