#!/bin/bash

print_usage() {
        echo "  -n";
        echo "     update all volumes to \"actiononpurge=None\"";
        echo "  -t";
        echo "     update all volumes to \"actiononpurge=Truncate\"";
        echo "  -p";
        echo "     prune all volumes ";
        echo "  -Dp";
        echo "     delete all purged volumes from Bacula catalog"
        echo "  -Do";
        echo "     delete obsolete volume files from the disk (those not listed in catalog)"
        echo "       NOTE: this operation can take several minutes to complete"
        echo "  -h";
        echo "     print this screen";
        echo ""
        exit 0

}

if [ $# -lt 1 ]; then
        print_usage
        exit 3
fi

update_volume() {

        BACULA_BATCH="$(mktemp)"

        echo ""
        echo "updating all volumes to \"actiononpurge=$1\"..."
        echo ""

        cd /mnt/sauvegardes/
        ls -1 | while read pool; do
                cd /mnt/sauvegardes/$pool
                echo "update volume=$pool ActionOnPurge=$1" >> $BACULA_BATCH;
        done
        bconsole < $BACULA_BATCH | grep $1
        rm -f $BACULA_BATCH

}

prune_all() {

        BACULA_BATCH="$(mktemp)"

        echo ""
        echo "pruning all volumes and let Bacula mark them as purged once the retention periods are expired..."
        echo ""

        cd /mnt/sauvegardes/
        ls -1 | while read pool; do
                echo "prune volume=$pool" >> $BACULA_BATCH;
                echo "yes" >> $BACULA_BATCH;
        done
        bconsole < $BACULA_BATCH | grep -i "marking it purged"
        rm -f $BACULA_BATCH
}

delete_obsolete_volumes() {

        VOLUMES_TO_DELETE="$(mktemp)"

        echo ""
        echo "searching for obsolete files on disk. These could be some old volumes deleted from catalog manually..."
        echo ""

        cd /mnt/sauvegardes/
        ls -1 | while read pool; do
                if [ "$pool" = "lost+found" ]; then
                    continue
                fi
                echo "list volume=$pool" | bconsole | if grep --quiet "No results to list"; then
                        echo "$pool is ready to be deleted"
                        echo "/mnt/sauvegardes/$pool" >> $VOLUMES_TO_DELETE
                fi
        done

        # Check whether we have found some volumes(files) to delete
        if [ `wc -l $VOLUMES_TO_DELETE | awk {'print $1'}` -gt 0 ]; then

                echo ""
                echo -n "Are you sure you want to delete these volumes(files) from the disk ? (yes|no): "
                read response

                if [ "$response" = "yes" ]; then
                        cat $VOLUMES_TO_DELETE | while read I; do
                                rm -f "$I"
                        done

                        echo ""
                        echo "DONE: following files were deleted: "
                        cat $VOLUMES_TO_DELETE
                fi

        else
                echo "No volumes to delete found on disk"

        fi

        rm -f $VOLUMES_TO_DELETE

}

delete_purged_volumes() {

        echo ""
        echo "searching for all purged volumes to be deleted..."
        echo ""

        PURGED_VOLUMES=`echo "list volumes" | bconsole | grep "Purged" | awk {'print $4'}`
        echo "$PURGED_VOLUMES"

        echo ""
        echo -n "Are you sure you want to delete all these purged volumes from Bacula catalog ? (yes|no): "
        read response

        if [ $response = "yes" ]; then
                BACULA_BATCH="$(mktemp)"
                rm -f /tmp/bacula.log
                echo "@output /tmp/bacula.log" > $BACULA_BATCH
                echo "$PURGED_VOLUMES" | while read I; do
                        echo "delete volume=$I" >> $BACULA_BATCH
                        echo "yes" >> $BACULA_BATCH
                done
                bconsole < $BACULA_BATCH
                rm -f $BACULA_BATCH
                echo "Done. See the log file /tmp/bacula.log"
        fi

}

# Parse parameters
while [ $# -gt 0 ]; do
    case "$1" in
        -h | --help)
                print_usage
                exit 0
                ;;
        -p)
                shift
                prune_all
                exit 0
                ;;
        -t)
                shift
                update_volume Truncate
                exit 0
                ;;
        -n)
                shift
                update_volume None
                exit 0
                ;;
        -Do)
                shift
                delete_obsolete_volumes
                exit 0
                ;;
        -Dp)
                shift
                delete_purged_volumes
                exit 0
                ;;
        *)  echo "Unknown argument: $1"
            print_usage
            exit 3
            ;;
        esac
done
exit 1

