lto-scripts/mkbak_media.sh

102 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
MEDIA_ID="$1"
MODE="append"
DEV="/dev/nst0"
LIB="./lib.csv"
if [ "$2" != "" ]; then
MODE="$2"
fi
function info()
{
echo -e "\e[34m[I] $1\e[0m"
}
function warn()
{
echo -e "\e[33m[W] $1\e[0m"
}
function err()
{
echo -e "\e[31m[E] $1\e[0m"
}
function succ()
{
echo -e "\e[32m[ ] $1\e[0m"
}
if [ "$MEDIA_ID" == "" ]; then
echo "usage: $0 <media ID> [rewind]"
exit
fi
info "Backing up new directories to $MEDIA_ID in $DEV."
if [ ! -f "$LIB" ]; then
echo "Directory;Media ID;File;Block;Date" > $LIB
fi
info "Configuring tape drive"
mt-st -f $DEV stsetoptions scsi2logical
mt-st -f $DEV compression 0
if [ "$MODE" == "append" ]; then
info "Append to end of data? [ctrl+c to cancel]"
read a
mt-st -f $DEV eod
elif [ "$MODE" == "here" ]; then
info "Appending to tape at current position. This may overwrite existing data. Are you sure? [ctrl+c to cancel]"
read a
else
warn "Rewinding tape. This will delete all existing data on tape. Are you sure? [ctrl+c to cancel]"
read a
mt-st -f $DEV rewind
fi
skipped=0
for dir in *; do
if [ ! -d "$dir" ]; then
continue
fi
exist=`cat "$LIB" | grep "^$dir;"`
if [ "$exist" != "" ]; then
info "Skipping $dir. Already backed up: $exist"
continue
fi
echo ""
info "Attempting to backup $dir"
lsize=`du -s "$dir" | sed "s/\s.*//"`
lsize=$(($lsize / 1024))
rsize=`sg_logs -a $DEV | sed -n 's/\s*Main partition remaining capacity (in MiB):\s*\(.*\)/\1/p'`
if [ $lsize -gt $rsize ]; then
skipped=$((skipped+1))
warn "Not enough space left on device (${rsize} MB remaining, ${lsize} MB required). Trying next directory."
continue;
fi
info "Appending $dir to tape ($lsize MB, $rsize MB available)"
fileno=`mt-st -f $DEV status | sed -n 's/File number=\([0-9]\+\).*/\1/p'`
block=`mt-st -f $DEV tell | sed -n 's/.*At block \([0-9]\+\).*/\1/p'`
tar cvO "$dir" | dd of=$DEV bs=512k
if [ $? -ne 0 ]; then
err "An error occured writing the backup to tape. Aborting."
exit
fi
date=`date`
echo "$dir;$MEDIA_ID;$fileno;$block;$date" >> $LIB
done
info "Rewinding and ejecting tape"
mt-st -f $DEV rewoff
echo ""
if [ $skipped -eq 0 ]; then
succ "Backup complete. All directories are on tape."
else
succ "Tape complete, but there is more data to be backed up."
warn "Insert a new tape and rerun the script to complete the backup."
fi