Indeed, it is executed only when tar runs out of destination files, which means youso in order to run the script each time a volume is created you have to use only one destination file in your command e.g. tarball-1:
tar -cvlpcv -M -F './my_script.sh' --file=tarball-1file=/dev/nst1 --tape-length=4M 10MBrandom.bin
The other destination files will be created byand have the script create the other archives. The script inherits tar’s shell environment and additional data is passed to it via environment variables e.g.:
TAR_ARCHIVE
The name of the archive tar is processing.
TAR_VOLUME
Ordinal number of the volume tar is about to start.
TAR_FD
File descriptor which can be used to communicate the new volume name to tar.
So, which runs once per filea script like
#! /bin/bash echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE. name=${TAR_ARCHIVE%?} echo ${name}$TAR_VOLUME >&$TAR_FD
would run each time another archive is created and produce /dev/nst2, /dev/nst3 etc.
Quote Note that TAR_VOLUME starts at 1 (that is the initial volume specified in your command via --file=). If you want to use --file=/dev/nst0 and have the archives numbered consecutively you'll have to subtract 1 from TAR_VOLUME when passing the same manualvolume name to (includingTAR_FD e.g. change the content oflast line to
echo ${name}$((TAR_VOLUME - 1)) >&$TAR_FD
If you only have two tape drives that are always named as /dev/nst0 and /dev/nst1 and want the sample script whichto work when either one or the other is used as initial --file argument, you can alter per your needscould extract the drive number from the current value of TAR_ARCHIVE and use the other drive number as TAR_VOLUME when you write the next archive name to TAR_FD:
#! /bin/bash echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE. drive=${TAR_ARCHIVE: -1} case "$drive" in 0) TAR_VOLUME=1 ;; 1) TAR_VOLUME=0 ;; *) exit 1 esac echo "${TAR_ARCHIVE%?}$TAR_VOLUME" >&$TAR_FD
This would create two archives, /dev/nst1 and /dev/nst0 or vice-versa, the first being the one you specified on the command line via --file
For the record, here is the quote from the manual (section 9.6.1) including the sample script provided there: