Skip to main content
deleted 29 characters in body
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

If you only have two tape drives that are always named as /dev/nst0 and /dev/nst1 and want the script to work when either one or the other is used as initial --file argument, you could 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=1other=1 ;; 1) TAR_VOLUME=0other=0 ;; *) exit 1 esac echo "${TAR_ARCHIVE%?}$TAR_VOLUME"${other}" >&$TAR_FD 

If you only have two tape drives that are always named as /dev/nst0 and /dev/nst1 and want the script to work when either one or the other is used as initial --file argument, you could 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 

If you only have two tape drives that are always named as /dev/nst0 and /dev/nst1 and want the script to work when either one or the other is used as initial --file argument, you could extract the drive number from the current value of TAR_ARCHIVE and use the other drive number 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) other=1 ;; 1) other=0 ;; *) exit 1 esac echo "${TAR_ARCHIVE%?}${other}" >&$TAR_FD 
added 1899 characters in body
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

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:

Indeed, it is executed only when tar runs out of destination files, which means you have to use only one destination file e.g. tarball-1:

tar -cvlp -M -F './my_script.sh' --file=tarball-1 --tape-length=4M 10MBrandom.bin 

The other destination files will be created by the script, which runs once per file created.
Quote from the same manual (including the content of the sample script which you can alter per your needs):

Indeed, it is executed only when tar runs out of destination files, so 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.

tar -cv -M -F './my_script.sh' --file=/dev/nst1 --tape-length=4M 10MBrandom.bin 

and 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, a 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. 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 volume name to TAR_FD e.g. change the last 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 script to work when either one or the other is used as initial --file argument, you could 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:

Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

Indeed, it is executed only when tar runs out of destination files, which means you have to use only one destination file e.g. tarball-1:

tar -cvlp -M -F './my_script.sh' --file=tarball-1 --tape-length=4M 10MBrandom.bin 

The other destination files will be created by the script, which runs once per file created.
Quote from the same manual (including the content of the sample script which you can alter per your needs):

the most flexible approach is to use a volume script, that writes new archive name to the file descriptor $TAR_FD. For example, the following volume script will create a series of archive files, named ‘archive-vol’, where archive is the name of the archive being created (as given by --file option) and vol is the ordinal number of the archive being created:

#! /bin/bash # For this script it's advisable to use a shell, such as Bash, # that supports a TAR_FD value greater than 9. echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE. name=`expr $TAR_ARCHIVE : '\(.*\)-.*'` case $TAR_SUBCOMMAND in -c) ;; -d|-x|-t) test -r ${name:-$TAR_ARCHIVE}-$TAR_VOLUME || exit 1 ;; *) exit 1 esac echo ${name:-$TAR_ARCHIVE}-$TAR_VOLUME >&$TAR_FD