Can I automate tar's multi-volume-feature?
Here is a solution:
printf 'n file-%02d.tar\n' {2..100} |
tar -ML 716800 -cf file-01.tar Documents/ 2>/dev/null
where 100 is a number greater or equal to the number of volumes.
Edit
Setting a big number should not be a problem, though I tend to not take a ridiculous one.
An alternative could be a "next volume" script, that you can set with the -F
option,
tar -ML 716800 -F './myscript file' -cf file.tar Documents/ 2>/dev/null
then in ./myscript
put
#!/bin/bash
prefix="$1"
n=1
while [[ -e "$prefix-$n.tar" ]]; do
((n++))
done
mv "$prefix.tar" "$prefix-$n.tar"
echo "$prefix-$n.tar"
It will be executed at each volume end, and will move file.tar
to the appropriate fileNNN.tar
. For the last volume the script will not be executed, so the last volume name stay file.tar
.
Edit 2
I ended up with the following elaborated solution.
Here are two script, one for the creation and the other for the extraction:
#!/bin/bash
# CREATION SCRIPT
# save on file the initial volume number
echo 1 >number
# multi-volume archive creation
tar -ML 100000 -F './tar-multi-volume-script c file' -cf file.tar Documents2/ 2>&-
# execute the "change-volume" script a last time
./tar-multi-volume-script c file
and
#!/bin/bash
# EXTRACTION SCRIPT
# save on file the initial volume number
echo 1 >number
# execute the "change-volume" script a first time
./tar-multi-volume-script x file
# multi-volume archive extraction
tar -M -F './tar-multi-volume-script x file' -xf file.tar 2>&-
# remove a spurious file
rm file.tar
where ./tar-multi-volume-script
is given by
#!/bin/bash
# TAR INVOKED SCRIPT
mode="$1"
prefix="$2"
n=$(<number)
case $mode in
c) mv "$prefix.tar" "$prefix-$n.tar" ;;
x) cp "$prefix-$n.tar" "$prefix.tar" ;;
esac
echo $((n+1)) >number
Obviously you have to change many bits here and there to adapt to your situation and to be sure it would work in cron
, that is always a little challenge.