Wednesday, August 10, 2005

tar in pieces

Ever needed to create a tar file that has multiple pieces ?
tar has some support for it, but it's pretty cumbersome.

Essentially, tar supports the "-L" switch which allows you to specify the maximum length before changing tapes. But you can use that to change files.
We will also need the "--new-volume-script" switch which will run a script every time we reached the max size. Finally, "--volno-file" is useful to track what volume you're on.
The basic trick is to rename/prepare the next file between volumes using the script tar will call.

The whole thing can be solved with 4 small scripts :
backup.sh - runs the backup command itself (and takes care of the last file).
pack.sh - gets called from backup.sh to handle each volume
restore.sh - used for restore
unpack.sh - gets called from restore.sh to handle volumes.

First backup :

backup.sh
========================
#!/bin/bash
if [ ! $# -eq "3" ]
then
echo "Usage $0 archive_name max_size_in_KB target_dir"
exit
fi
# remove volume counter
rm -rf volno
#create tar file
tar cvf $1 -L $2 --volno-file volno --new-volume-script "pack.sh $1" $3
# handle the last file
last_file_name=$1__`cat volno`.tar.gz
gzip -c $1 > $last_file_name
rm -rf $1
# END OF backup.sh


pack.sh
============
#!/bin/bash
#
if [ ! $# -eq "1" ]
then
echo "Usage: $0 file_name"
exit
fi
let "archive_no=`cat volno`-1"
echo Processing Archive $archive_no...
gzip -c $1 > $1__$archive_no.tar.gz
#END of pack.sh


and to restore it:

restore.sh
===========
#!/bin/bash
if [ ! $# -eq "1" ]
then
echo "Usage $0 first_archive_name_(without __X.tar.gz)"
exit
fi
# remove volume counter
rm -rf volno
#unzip the first file
gunzip -c $1__1.tar.gz > $1
#restore tar file
tar xvf $1 --volno-file volno --new-volume-script "/root/drm_backup/unpack.sh $1"
# remove temp file
rm -rf $1
# End of restore.sh

unpack.sh
==========
#!/bin/bash
#
if [ ! $# -eq "1" ]
then
echo "Usage: $0 file_name"
fi
let "archive_no=`cat volno`"
echo Processing Archive $archive_no...
gunzip -c $1__$archive_no.tar.gz > $1
# End of unpack.sh

No comments: