Skip to content

Instantly share code, notes, and snippets.

@fennectech
Last active March 28, 2026 20:50
Show Gist options
  • Select an option

  • Save fennectech/30f7e1706afb615de9c5b4a8bd4c0b52 to your computer and use it in GitHub Desktop.

Select an option

Save fennectech/30f7e1706afb615de9c5b4a8bd4c0b52 to your computer and use it in GitHub Desktop.
LTO-Restore.sh
#!/usr/bin/env bash
#Copyright 2026 fennectech
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
echo "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
set -euo pipefail
TAPE_DEVICE="/dev/nst0"
FIFO="/tmp/btrfs_restore_fifo_$$"
[[ $# -ne 1 ]] && { echo "Usage: $0 /path/to/mountpoint"; exit 1; }
DESTINATION="$1"
RECV_PID=""
cleanup() {
if [[ -n "$RECV_PID" ]] && kill -0 "$RECV_PID" 2>/dev/null; then
kill "$RECV_PID" 2>/dev/null
wait "$RECV_PID" 2>/dev/null || true
fi
exec 3>&- 2>/dev/null || true
rm -f "$FIFO"
}
trap cleanup EXIT
mkfifo "$FIFO"
btrfs receive "$DESTINATION" < "$FIFO" &
RECV_PID=$!
exec 3>"$FIFO"
wait_for_drive_ready() {
local attempts=0
local max_attempts=60
echo "Waiting for drive to become ready..."
until mt -f "$TAPE_DEVICE" status &>/dev/null; do
sleep 2
(( attempts++ ))
if (( attempts >= max_attempts )); then
echo "ERROR: Tape drive did not become ready after $(( max_attempts * 2 )) seconds."
exit 1
fi
done
echo "Drive is ready."
}
wait_for_rewind() {
local attempts=0
local max_attempts=120
echo "Waiting for rewind to complete..."
until mt -f "$TAPE_DEVICE" status 2>&1 | grep -q "File number=0, block number=0"; do
sleep 1
(( attempts++ ))
if (( attempts >= max_attempts )); then
echo "ERROR: Rewind did not complete after ${max_attempts} seconds."
exit 1
fi
done
echo "Rewind complete."
}
TAPE_NUM=1
while kill -0 "$RECV_PID" 2>/dev/null; do
echo "Insert tape #${TAPE_NUM} and press ENTER when the drive load light is solid (or Ctrl+C to abort)..."
read -r < /dev/tty
wait_for_drive_ready
echo "Rewinding tape #${TAPE_NUM}..."
if ! mt -f "$TAPE_DEVICE" rewind; then
echo "ERROR: Rewind command failed on tape #${TAPE_NUM}."
exit 1
fi
wait_for_rewind
echo "Setting variable block mode on tape #${TAPE_NUM}..."
if ! mt -f "$TAPE_DEVICE" setblk 0; then
echo "ERROR: Failed to set variable block mode on tape #${TAPE_NUM}."
exit 1
fi
echo "Reading tape #${TAPE_NUM}..."
if ! dd if="$TAPE_DEVICE" bs=1024k status=progress >&3; then
echo "ERROR: Failed reading tape #${TAPE_NUM} — aborting."
exit 1
fi
echo "Tape #${TAPE_NUM} done. Ejecting..."
mt -f "$TAPE_DEVICE" eject || echo "WARNING: Eject failed — please remove tape #${TAPE_NUM} manually."
echo "You may remove tape #${TAPE_NUM}."
(( TAPE_NUM++ ))
done
exec 3>&-
if ! wait "$RECV_PID"; then
echo "ERROR: btrfs receive failed — restore may be incomplete or corrupt."
exit 1
fi
echo "Restore complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment