Skip to content

Instantly share code, notes, and snippets.

@yoshi314
Created September 28, 2023 12:05
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 yoshi314/f25ca806da86152dadc2b3b691417501 to your computer and use it in GitHub Desktop.
Save yoshi314/f25ca806da86152dadc2b3b691417501 to your computer and use it in GitHub Desktop.
Tape backup script with % capacity tracking per-tape (across sessions)
#!/bin/sh
die() {
echo $@
exit 1
}
which bc || die "no bc found"
if [ $# -lt 2 ] ; then
echo "$0 directory_to_archive logfile"
exit 1
fi
date
tapedensity=$(mt-st -f /dev/nst0 status | grep Density | grep -o -e LTO-.)
case $tapedensity in
"LTO-2")
tapesize="200*1024^3"
;;
"LTO-3")
tapesize="400*1024^3"
;;
*)
tapesize="200*1024^3"
;;
esac
tapesize=$(echo "$tapesize" | bc -l)
size=$(du -sb "$1" | awk '{ print $1}')
logfile=$2
if [ ! -e "$logfile" ] ; then
echo "========================================================================" >> $logfile
echo "Tape size : $tapesize" > $logfile
echo "first record on this tape"
record=0
lastwrite=0
else
echo "========================================================================" >> $logfile
text=$(grep asf $logfile)
echo "${text##*asf}"
record=$(grep asf $logfile)
record=${record##*asf}
# find out current record from txt file
echo "starting with record $record"
record=$((record+1))
lastwrite=$(grep "written" $logfile)
lastwrite=${lastwrite##*written}
fi
echo "mt-st -f /dev/nst0 asf ${record}" >> $logfile
echo "files : " >> $logfile
find "$1" -type f >> $logfile
echo "Total size : $size bytes" >> $logfile
du -hs "$1" >> $logfile
echo "Data to write : $size"
size_gb=$(echo "scale=2; $size / 1024 / 1024 / 1024" | bc -l)
echo "That is $size_gb GB"
tar cf - "$1" | pv -s ${size} > /dev/nst0
written=$(echo "$size+$lastwrite" | bc -l)
echo "written $written" >> $logfile
capacity=$(echo "scale=4; $written/$tapesize*100" | bc -l)
written_gb=$(echo "scale=2; $written /1024/1024/1024" | bc -l)
tapesize_gb=$(echo "scale=2; $tapesize /1024/1024/1024" | bc -l)
echo "that is $capacity % of the tape so far" | tee -a $logfile
echo "$written_gb / $tapesize_gb (GB)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment