Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #forked from:
- # https://github.com/elonen/bareos-cleaner/blob/master/bareos-cleanup and
- # https://admin.shamot.cz/?p=309
- print_usage() {
- echo " -s"
- echo " status of all catalog volumes"
- echo " -c"
- echo " check configs"
- echo " -z"
- echo " list disk space used"
- echo " -p"
- echo " prune all volumes ";
- echo " -d"
- echo " check/repair catalog ";
- echo " -t"
- echo " update all volumes to \"actiononpurge=Truncate\"";
- echo " -n"
- echo " update all volumes to \"actiononpurge=None\"";
- echo " -Do"
- echo " delete obsolete volume files from disk and cloud storage"
- echo " -Dp"
- echo " delete all purged, recycle, error volumes from catalog"
- echo " -h"
- echo " print this screen";
- echo ""
- exit 0
- }
- if [ $# -lt 1 ]; then
- print_usage
- exit 3
- fi
- dbname="bacula"
- dbuser="bacula"
- dbpass="xxxxxx"
- cloud="backblaze:bacula01"
- #local stores
- cache="/mnt/usb16tb/cache"
- nas="/mnt/nas-bacula"
- qnap="/mnt/nas-qnap"
- #check which stores exist
- if [ ! -d ${cache} ]; then
- cache=""
- fi
- if [ ! -d ${nas} ]; then
- nas=""
- fi
- if [ ! -d ${qnap} ]; then
- qnap=""
- fi
- stores="${nas} ${qnap} ${cache}"
- status() {
- #list status of all catalog volumes
- for vol in $(echo "llist media" | bconsole | grep "volumename:" | awk '{print $2}')
- do
- volname=$(echo "llist volume=${vol}" | bconsole | grep "volumename:" | xargs)
- volstat=$(echo "llist volume=${vol}" | bconsole | grep "volstatus:" | xargs)
- expires=$(echo "llist volume=${vol}" | bconsole | grep "expiresin:" | xargs | sed s/,//g)
- echo "${volname}" "${volstat}" "${expires}"
- done
- }
- configs() {
- if [ -z $(bacula-dir -t) ]; then
- echo "director config OK"
- fi
- if [ -z $(bacula-fd -t) ]; then
- echo "file daemon config OK"
- fi
- if [ -z $(bacula-sd -t) ]; then
- echo "storage daemon config OK"
- fi
- }
- update_volume() {
- echo ""
- echo "updating all volumes to \"actiononpurge=$1\"..."
- echo ""
- for storage in ${stores}; do
- echo "Processing" ${storage}
- BACULA_BATCH="$(mktemp)"
- ls -1 ${storage} | while read vol; do
- echo "update volume=${vol} ActionOnPurge=$1" >> ${BACULA_BATCH}
- done
- bconsole < ${BACULA_BATCH} | grep $1
- rm "${BACULA_BATCH}"
- done
- }
- pool_size() {
- for store in ${stores}; do
- if [[ -d ${store} ]] && [[ -n $(ls -1 ${store}) ]]; then #exists and has files
- echo ${store}
- for pool in $(ls -1 ${store} | cut -d - -f 1 | sort -u); do #filename is pool-sequ
- echo ${pool} $'\t' $(sudo du -sch ${store}/${pool}* | grep total | cut -d $'\t' -f 1)
- done
- echo ${store} "Total" $(sudo du -sch ${store} | grep total)
- fi
- done
- #cloud
- if [[ ${store} = "cloud" ]]; then
- echo "Cloud Total" $(rclone size ${cloud} --human-readable)
- fi
- }
- checkdb() {
- dbcheck /var/lib/bacula/ ${dbname} ${dbuser} ${dbpass}
- }
- delete_obsolete_volumes() {
- #################################################################################################
- #list volumes that have no associated job and hence can be deleted from the catalog and disk/cloud
- #################################################################################################
- echo "############################################"
- echo "Deleting volumes that have no associated job"
- echo "############################################"
- VOLUMES_TO_DELETE="$(mktemp)"
- for vol in $(echo "list media" | bconsole | grep "^| \+[0-9].*" | awk '{print $4}'); do
- echo -e "query 14\n${vol}\n" | bconsole | if grep --quiet "No results to list"; then
- status=$(echo "list volume=${vol}" | bconsole | grep "^| \+[0-9].*" | awk '{print $6}')
- echo ${vol} "has no job" "status="${status} #volumes in catalog that have no associated job
- # if [ ${status} != "Append" ]; then
- echo ${vol} >> ${VOLUMES_TO_DELETE} # do not delete Append status volumes
- # fi
- fi
- done
- # Check whether we have found some volumes to delete
- numvols=$(wc -l ${VOLUMES_TO_DELETE} | awk '{print $1}')
- if [ ${numvols} -gt 0 ]; then
- echo "${numvols} volumes are ready to delete: "
- cat "${VOLUMES_TO_DELETE}"
- echo -n "Are you sure you want to delete these ${numvols} volumes ? \(yes|no\): "
- read response
- if [ ${response} = "yes" ]; then
- cat ${VOLUMES_TO_DELETE} | while read vol; do
- echo -e "delete yes volume=${vol}" | bconsole # delete from catalog
- volpath=$(find ${stores} -name ${vol}) # directories for cloud volumes
- if [ -f ${volpath} ]; then
- sudo -u bacula rm -rf ${volpath} # delete from disk also directories for cache
- echo ${volpath} "deleted"
- fi
- rclone purge ${cloud}/${vol} # cloud
- done
- echo "DONE: following volumes were deleted: "
- cat "${VOLUMES_TO_DELETE}"
- else
- echo "quitting"
- exit 1
- fi
- else
- echo "no volumes found to delete"
- fi
- rm "${VOLUMES_TO_DELETE}"
- #now check for disk volumes that are not in the catalog
- echo "####################################################"
- echo "Deleting volumes on disk that are not in the catalog"
- echo "####################################################"
- VOLUMES_TO_DELETE="$(mktemp)"
- #cache
- echo "cache"
- if [[ -d ${cache} ]] && [[ -n $(ls -1 ${cache}) ]]; then #exists and has files
- for vol in $(basename -a $(sudo -u bacula find ${cache} -mindepth 1 -type d | sort)); do
- echo "list volume=${vol}" | bconsole | if grep --quiet "No results to list"; then
- echo $(sudo -u bacula du -s -b ${cache}/${vol})
- sudo -u bacula rm -rf ${cache}/${vol}
- echo "Deleted" "${cache}/${vol}"
- fi
- done
- fi
- #cloud
- echo "cloud"
- if [[ ! -z $(rclone ls ${cloud}) ]]; then
- for vol in $(basename -a $(rclone lsd ${cloud} | awk '{print $5}' | sort)); do
- echo "list volume=${vol}" | bconsole | if grep --quiet "No results to list"; then
- echo ${vol} $(rclone size ${cloud}/${vol}) #print volumes/size that do not exist in catalog
- rclone purge ${cloud}/${vol} #delete on remote
- echo "Deleted" ${vol} $(rclone size ${cloud}/${vol})
- fi
- done
- fi
- #dns-325
- echo "DNS-325"
- if [[ -d ${nas} ]] && [[ -n $(ls -1 ${nas}) ]]; then #exists and has files
- for vol in $(basename -a $(sudo find ${nas} -mindepth 1 -type f | sort)); do
- echo "list volume=${vol}" | bconsole | if grep --quiet "No results to list"; then
- echo $(sudo -u bacula du -s -b ${nas}/${vol})
- sudo -u bacula rm -f ${nas}/${vol}
- echo "Deleted" "${nas}/${vol}"
- fi
- done
- fi
- #QNAP
- echo "QNAP"
- if [[ -d ${qnqp} ]] && [[ -n $(ls -1 ${qnap}) ]]; then #exists and has files
- for vol in $(basename -a $(sudo find ${qnap} -mindepth 1 -type f | sort)); do
- echo "list volume=${vol}" | bconsole | if grep --quiet "No results to list"; then
- echo $(sudo -u bacula du -s -b ${qnap}/${vol})
- sudo -u bacula rm -f ${qnap}/${vol}
- echo "Deleted" "${qnap}/${vol}"
- fi
- done
- fi
- }
- prune_all() {
- BACULA_BATCH="$(mktemp)"
- echo ""
- echo "pruning all volumes and let Bacula mark them as purged once the retention periods are expired..."
- echo ""
- for st in ${stores}; do
- ls -1 ${st} | while read vol; do
- echo "prune volume=${vol}" >> ${BACULA_BATCH}
- echo "yes" >> ${BACULA_BATCH}
- done
- bconsole < ${BACULA_BATCH} | grep -i "marking it purged"
- done
- rm "${BACULA_BATCH}"
- }
- delete_purged_volumes() {
- # delete purged, recycle or error volumes from catalog
- echo "searching for all purged, recycle, read-only or error volumes to be deleted..."
- echo "purged"
- PURGED_VOLUMES=`echo "list volumes" | bconsole | grep "Purged" | awk '{print $4}'`
- if [[ -n ${PURGED_VOLUMES} ]]; then
- echo "${PURGED_VOLUMES}"
- echo -n "Are you sure you want to delete these purged volumes from the catalog ? \(yes|no\): "
- read -r response
- if [[ ${response} = "yes" ]]; then
- for vol in ${PURGED_VOLUMES}; do
- echo "delete volume=${vol} yes" | bconsole
- done
- fi
- else
- echo "no purged volumes"
- fi
- echo "recycled"
- RECYCLE_VOLUMES=$(echo "list volumes" | bconsole | grep "Recycle" | awk '{print $4}')
- if [[ -n ${RECYCLE_VOLUMES} ]]; then
- echo "${RECYCLE_VOLUMES}"
- echo -n "Are you sure you want to delete these recycled volumes from the catalog ? \(yes|no\): "
- read -r response
- if [[ ${response} = "yes" ]]; then
- for vol in ${RECYCLE_VOLUMES}; do
- echo "delete volume=${vol} yes" | bconsole
- done
- fi
- else
- echo "no recycle volumes"
- fi
- echo "errored"
- ERROR_VOLUMES=$(echo "list volumes" | bconsole | grep "Error" | awk '{print $4}')
- if [[ -n ${ERROR_VOLUMES} ]]; then
- echo "${ERROR_VOLUMES}"
- echo -n "Are you sure you want to delete these errored volumes from the catalog ? \(yes|no\): "
- read -r response
- if [[ ${response} = "yes" ]]; then
- for vol in ${ERROR_VOLUMES}; do
- echo "delete volume=${vol} yes" | bconsole
- done
- fi
- else
- echo "no error volumes"
- fi
- echo "read only"
- READ_ONLY_VOLUMES=$(echo "list volumes" | bconsole | grep "Read-Only" | awk '{print $4}')
- if [[ -n ${READ_ONLY_VOLUMES} ]]; then
- echo "${READ_ONLY_VOLUMES}"
- echo -n "Are you sure you want to delete these read-only volumes from the catalog ? \(yes|no\): "
- read -r response
- if [[ ${response} = "yes" ]]; then
- for vol in ${READ_ONLY_VOLUMES}; do
- echo "delete volume=${vol} yes" | bconsole
- done
- fi
- else
- echo "no read only volumes"
- fi
- }
- # Parse parameters
- while [ $# -gt 0 ]; do
- case "$1" in
- -h | --help)
- print_usage
- exit 0
- ;;
- -s)
- shift
- status
- exit 0
- ;;
- -c)
- shift
- configs
- exit 0
- ;;
- -z)
- shift
- pool_size
- exit 0
- ;;
- -t)
- shift
- update_volume Truncate
- exit 0
- ;;
- -d)
- shift
- checkdb
- exit 0
- ;;
- -p)
- shift
- prune_all
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement