Bacula Reclaim Space Script

From Tech Wiki
Jump to navigation Jump to search

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

cd /root
vi reclaimspace.sh

Then copy and paste the following in to that file:

#***********************************************************
#!/bin/bash
## 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"
fi
done
 
## 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!
continue;
fi
 
if [ "$JOBID" != "NULL" ]; then
echo -e "delete job JobID=$JOBID\nyes\nquit\n" | /usr/sbin/bconsole 2>&1 >/dev/null
fi
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";
done
rm $CATALOGVOLS;
rm $DISKVOLS;
#***********************************************************

Then use the following command to make the reclaimspace.sh script executable:

chmod +x reclaimspace.sh

Then execute the file using the following command:

./reclaimspace.sh