[SOLVED] Script: Automate media pool assignments

DocCyblade

New Member
Oct 23, 2022
7
2
3
So loving PBS, however I wanted to automate some things. One of those things is auto assign media pool depending on the prefix of a tape.

I have an IBM 9 tape changer and I wanted an easy way to add new tapes and run a command to auto assign them to media pools depending on the label prefix so I wrote a bash script to do this. It loops through an array of prefix to media pool names and builds a work list by dumping the changer status getting all the tapes that match the prefixes, then takes that list and ensures the tapes are not already in the media pools (skips those). Takes this work list and loads the tape, assigns it to the correct pool and then ejects the tape. We are using the PBS built in tools pmtx and proxmox-tape commands.

Here is the source, feel free to use it in your system if you would like. If you end up using it post on this thread so I know it's helpful.

v1.0 tested on PBS v2.2-7

Latest source found here:
https://gist.github.com/DocCyblade/9aedb26036bd2c6e894e67576bd2cf01

Bash:
#!/bin/bash

# --------------------------------------------------------------------------------
#     Copyright (C) 2022  Kenneth R. Robinson (DocCyblade)
#     https://github.com/doccyblade
# --------------------------------------------------------------------------------
#                                   GNU GPLv3
# --------------------------------------------------------------------------------
#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <https://www.gnu.org/licenses/>.


# auto-pool-assign.sh
APA_VER=v1.0
#
# Looks at all tapes in the library and assigns them into
# the correct media pool by the prefix of the label if
# the tape has not already been assigned
#
#
# Tested on PBS v2.2-7
#
# Source:
# https://gist.github.com/DocCyblade/9aedb26036bd2c6e894e67576bd2cf01

# Change Log
#
# 1.0 - First release  (2022-10-28)
#
#
#
#



# Tape drive name in PBS to use
export PROXMOX_TAPE_DRIVE=IBM-LTO6


# Add your prefix to media pool below
# APA_PREFIX_TO_POOL[PrefixHere]="PoolNameHere"
declare -A APA_PREFIX_TO_POOL
APA_PREFIX_TO_POOL[BAK]="Backup"
APA_PREFIX_TO_POOL[EXT]="Extra"
APA_PREFIX_TO_POOL[OLA]="Archive"
APA_PREFIX_TO_POOL[LTS]="LongTerm"




# ---------- Do not change anything below --------------


# License/Copyright Notice
echo " ----------------------------------------------------------------------------"
echo "  Proxmox Backup Auto Media Pool Assignment Script $APA_VER"
echo "  (`basename $0`) - Licnesed under GNU GPLv3"
echo " ----------------------------------------------------------------------------"
echo " | Copyright (C) 2022  Kenneth R. Robinson (https://github.com/doccyblade)  |"
echo " | This program comes with ABSOLUTELY NO WARRANTY; This is free software,   |"
echo " | and you are welcome to redistribute it under certain conditions          |"
echo " ----------------------------------------------------------------------------"
echo ""
sleep 2

# Other variables we will used later on
declare -A APA_DISPLAY_LIST
declare -A APA_WORK_LIST


# Grab changer status, stash it in a variable
echo "Fetching changer status"
ABA_RAW_TAPE_LIST=`pmtx status`

# Grab Media library, stash it in a variable
ABA_RAW_MEDIA_LIST=`proxmox-tape media list`


# Loop through the prefix list defined above and create work list
# and display lists
for iPrefix in "${!APA_PREFIX_TO_POOL[@]}"
do

  # echo "DEBUG: Checking for tapes with prefix of $iPrefix ${APA_PREFIX_TO_POOL[$iPrefix]}"

  # Create tape list if any from the current prefix ($i)
  # This should have all the tapes with current prefix ($i)
  xSubList=`echo $ABA_RAW_TAPE_LIST | grep -o $iPrefix.....`

  # Loop through the tape lists and build work list
  # Check to see if tape exists in the media library
  # if it is there we skip it
  for iTape in $xSubList
  do

     # echo "DEBUG: Processing tape: $iTape"

     # Look up tape in media list result 0 if now found
     # result of 1 if found
     xResult=`echo "$ABA_RAW_MEDIA_LIST" | grep -c $iTape`

     # echo "DEBUG: xResult = $xResult"

    # Add tape to work list and mark it on the display List
    # depending on the xResult of the lookup
    if [ "$xResult" == "0" ]
    then

      # Tape label not found in media library, mark it as such in the
      # display list and add it to the work list
      # echo "DEBUG: Tape $iTape is Not in the library add to work list"
      APA_WORK_LIST[$iTape]="${APA_PREFIX_TO_POOL[$iPrefix]}"

      # echo "DEBUG: Update display list"
      APA_DISPLAY_LIST[$iTape]="Add to ${APA_PREFIX_TO_POOL[$iPrefix]}"

    else
      # Tape label was found in the media library, mark it as skipped
      # echo "DEBUG: Tape $iTape was found in the library"
      # echo "DEBUG: Mark as skipped in display list"
      APA_DISPLAY_LIST[$iTape]="was skipped"
    fi

  done

done


# Display work to be done
echo ""
echo "---------------------------------------"
echo "               Work List"
echo "---------------------------------------"
for iTape in "${!APA_WORK_LIST[@]}"
do
   echo "$iTape to ${APA_WORK_LIST[$iTape]} pool"
done
echo "---------------------------------------"
echo ""

echo "---------------------------------------"
echo " This will assign the tapes above to "
echo " pools listed next to each tape "
echo "---------------------------------------"
echo ""
read -r -p "Are you sure you want to do this? [Y/n] " input

case $input in
      [yY][eE][sS]|[yY])

     # Select Yes, get to work
     echo ""
     echo "Lets get to work!"
     # Loop through the work list
     for iTape in "${!APA_WORK_LIST[@]}"
      do
         echo "Processing $iTape.. "
         proxmox-tape load-media $iTape
         echo "waiting for drive to be ready.."
         sleep 5
         proxmox-tape label --label-text $iTape --pool ${APA_WORK_LIST[$iTape]}
         echo "Ejecting $iTape.."
         proxmox-tape eject
         echo "$iTape complete!"
         APA_DISPLAY_LIST[$iTape]="was added to ${APA_WORK_LIST[$iTape]} media pool"
         echo "- - -"
      done

      echo "All tapes in the work list are done!"
      echo "---------------------------------------"
      for iTape in "${!APA_DISPLAY_LIST[@]}"
      do
         echo " - $iTape ${APA_DISPLAY_LIST[$iTape]}"
      done
      echo "---------------------------------------"
      echo ""


            ;;
      [nN][oO]|[nN])
            echo ""
            echo "Canceled! Nothing has been done"
            echo ""
            echo ""
            ;;
      *)
            echo ""
            echo "Invalid input..."
            echo "Canceled! Nothing has been done"
            echo ""
            echo ""
            exit 1
            ;;
esac
echo ""
echo ""
# echo "DEBUG: End of Script"
 
  • Like
Reactions: dcsapak

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!