Skip to content

Instantly share code, notes, and snippets.

@DocCyblade
Last active October 29, 2022 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DocCyblade/9aedb26036bd2c6e894e67576bd2cf01 to your computer and use it in GitHub Desktop.
Save DocCyblade/9aedb26036bd2c6e894e67576bd2cf01 to your computer and use it in GitHub Desktop.
Proxmox Backup script to auto assign tapes to media pools
#!/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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment