Skip to content

Instantly share code, notes, and snippets.

@stackcoder
Created February 19, 2024 17:30
Show Gist options
  • Save stackcoder/55147cb0ae5b4102dd6b0028ce35be5a to your computer and use it in GitHub Desktop.
Save stackcoder/55147cb0ae5b4102dd6b0028ce35be5a to your computer and use it in GitHub Desktop.
Bash script to batch erase tapes
#!/bin/bash
set -euo pipefail
# Redirect everything to stdout and logfile
exec > >(tee -a erase_tape.log) 2>&1
# {{ ansible_managed }}
TAPE="{{ tape_backup__tape_drive }}"
if [[ ! -c "${TAPE}" ]]; then
echo "Tape device '${TAPE}' not found"
exit 1
fi
while true; do
echo "$(date +'%Y-%m-%d %H:%M:%S'): Wait for tape medium"
while true; do
TAPE_STATUS=$(mt-st -f "${TAPE}" status | sed -nE 's/General status bits on \(([0-9]+)\):/0x\1/p')
if (( (TAPE_STATUS & 0x01000000) != 0 )); then
# GMT_ONLINE
break;
fi
sleep 1
done
echo ""
echo "$(date +'%Y-%m-%d %H:%M:%S'): Rewind tape"
mt-st -f "${TAPE}" rewind
echo ""
# Print status
mt-st -f "${TAPE}" status
echo ""
echo "$(date +'%Y-%m-%d %H:%M:%S'): Erase tape"
mt-st -f "${TAPE}" erase
echo ""
echo "$(date +'%Y-%m-%d %H:%M:%S'): Eject tape"
mt-st -f "${TAPE}" eject
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment