Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Ok, so I've just had a read through this page after a way to improve my current backup solution on my Debian server. Tar seems to be offering a quite nice multi-volume feature, although when I try it out, it asks me to Prepare volume #X for ‘mybackup.tar.gz’ and hit return:.

How should I automate this as I would like to take usage of this feature in an automated CRON script where no one is there to push return and enter whatever is rquired by the multi-volume prompt.

Is using split the only way?

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
I like that, but then I need to know the max number of volumes per directory in beforehand? I mean, what happens when I exceed 716m in 100 directories? Can I just set max to a ridiculously high number like 919191 and leave it? –  Industrial Sep 25 '11 at 9:09
    
@Industrial: see edit –  enzotib Sep 25 '11 at 9:34
    
Thanks a lot for this great answer, Enzotib! How would you suggest restoring/extracting using this "next-volume" script? –  Industrial Sep 25 '11 at 15:31
    
@Industrial: here an almost complete solution –  enzotib Sep 25 '11 at 16:30
add comment

When in doubt, read the manual ...

http://www.gnu.org/software/tar/manual/html_node/Multi_002dVolume-Archives.html

or simply use the following search string at your favorite internet search engine:

"Archives Longer than One Tape or Disk"

That section of the GNU tar manual provides a suitable script (called new-volume) to use with the -F option of the tar command. You could tweak that script to your taste if you wish.

enjoy!

share|improve this answer
    
You seem to have forgotten to mention the --new-volume-script flag? –  SamB Oct 19 '12 at 2:10
2  
Please don't just post links; include some context so that, should the other site go down, there is sufficient information here to make sense of your answer. –  jasonwryan Oct 19 '12 at 2:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.