LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Tape drive backup and Restore (https://www.linuxquestions.org/questions/linux-newbie-8/tape-drive-backup-and-restore-4175411188/)

shubhamuddu 06-13-2012 05:48 AM

Tape drive backup and Restore
 
Hi ...




Am using ultrium LTO4 sas IBMtape drive in RHEL 5.5 for backup.



Device name is /dev/IBMtape0
Someone has written a script for backup as monday is full backup and for rest of the days partial backup, used the below command in the script for backup.
$TAR $TAR_ARGS -cvzpf $TAPE $DIR $BACKUP_ROOT_DIR
when i issue tar -tvzf it lists files in the tape.

when i issue tar -tvzf it lists files in the tape.
but how can i know which date backup it is listing.

Please tell me how to create a directory with date in tape drive so backup should go into the created directory.
And also please tel me how to restore files from tape drive.

Thank you for all.

tronayne 06-13-2012 07:29 AM

The command
Code:

tar -tvzf path_to_tape
should list the content with the date of each file.

If you want to extract you substitute the t with x; however, that will extract everything (which you may not want). To extract one file from the archive you would use
Code:

tar xzvf archive_name file_name_in_archive
I have a compressed archive named moneydance_other.tar.gz and I want to view one file, moneydance/moneydance.xpm from it:
Code:

tar -tvzf moneydance_other.tar.gz moneydance/moneydance.xpm
-rw-r--r-- sreilly/staff  6278 2001-02-01 09:16 moneydance/moneydance.xpm

If I wanted to extract that file, I would change -tvzf to -xvzf to do so.

The above is the GNU version of tar (but most will work like this). You may want to read the manual page for tar or the info page for tar or, using Google, search for "linux tar." Here is one page that you may find useful (and full of examples) http://www.computerhope.com/unix/utar.htm.

Hope this helps some.

shubhamuddu 06-14-2012 05:12 AM

Tape drive backup and restore
 
Hi..

Really helpfull information.

Here is the thing.
Script had the line $TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR(with no archive name)

As i wanted to create a compressed archive in the tape so modified it as

#name for the archive in the tape
DIR=mkdir TEST_BKP_$(date '+%d%m%Y')

$TAR $TAR_ARGS -cvzpf $TAPE $DIR $BACKUP_ROOT_DIR

Still couldn't able to find the name of the archive or directory in the tape.(only listing files)
I think archive is not being created in the tape.

please guide me how to create a archive name with current date in the tape.
(Where archive name should be mentioned in the below tar line)
$TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR.

Also when i extract file from tape which location file will be placed.
(I mean whether in the present working directory where issuing the command or in the original location)

Thanks...

tronayne 06-14-2012 10:50 AM

The way your shell program is written, the first argument to the tar command ($TAR) is the tape drive ($TAPE); i.e., /dev/IBMtape0. So, the tape is the tape archive (that's what tar means, an acronym of tape archive. So, what you see on the tape is a list of directory and file names that are a copy of the location (directory) and content (file).

The other way to look at that is if you used tar to create a file named, say, 2012-06-14.tar (for example) somewhere on your disk drive(s) then compress that into what's called a tarball (say, 2012-06-14.tar.gz) then copy that to the tape as one big (but compressed) tar archive. You'd do that something like this
Code:

tar cvf 2012-06-14.tar $DIR $BACKUP_ROOT_DIR
<wait a while>
gzip 2012-06-14.tar
<which compresses the archive into 2012-06-14.tar.gz>
cp 2012-06-14.tar.gz /dev/IBMtape0

You can do that in a pipeline (see below) but those are the basic steps to get a date stamped compressed archive on tape (or CD-ROM or DVD or Blu-ray disc media for that matter). The way your shell program is doing it, the archive is not compressed or date stamped.

You have to consider, though, whether you have sufficient room on the tape for more than one back up of the system root (or whatever file tree on the disk drive) directory (some tape systems do, some don't, some will write on more than one cartridge, some won't). It may be simpler to, you know, write the date on the label and be done with it.

A slick way to create the archive file name is
Code:

tar -cvf `date +%F`.tar.gz <other arguments>
If you want to add a directory name to that
Code:

tar -cvf /tmp/`date +%F`.tar <other arguments>
Those "`...` are "sideways pipes," they can also be done $(date +%F) (definitely in KornShell, pretty sure in BASH too).

Now, let's combine stuff.

You can compress on-the-fly by adding z for gzip or j for bzip2 in the tar arguments:
Code:

tar -czvf /tmp/`date +%F`.tar.gz <other arguments>
for gzip (replace the z with j for bzip2.

You might want to avoid the v (which shows you the file names) unless you're keeping a log.

So, all in one go
Code:

# Create a date stamp name for the archive
FILE_NAME=/tmp/`date +%F`.tar.gz
# Create a gzip tar archive in /tmp/yyyy-mm-dd.tar.gz
tar czf $FILE_NAME $DIR $BACKUP_ROOT_DIR
# Copy the archive file to tape
cp $FILE_NAME $TAPE
# Remove the archive file
rm $FILE_NAME

You can do the entire thing in one line but you have to use dd to do it which means that you have to know the input block size of the tape and some other stuff. Just as easy to use two line to accomplish the job.

Hope this helps some.

chrism01 06-14-2012 06:45 PM

Just a comment on post #3
Code:

# this doesn't work
DIR=mkdir TEST_BKP_$(date '+%d%m%Y')

# I think you meant
DIR=TEST_BKP_$(date '+%d%m%Y')
mkdir $DIR


Also
Code:

$TAR $TAR_ARGS -cvpf
looks odd. The syntax for tar is tar [options] file... http://linux.die.net/man/1/tar
'-cvpf' are the options.. I think you meant
Code:

TAR_ARGS=-cvpf
$TAR $TAR_ARGS $FILE_NAME...

as per Tronayne's explanation.

shubhamuddu 06-14-2012 11:37 PM

Got it...
See the script for the full backup and partial backup.
For partial backup aleso tar acrchive will be created..?
And please correct if the script is wrong anad also when i type
mt -f /dev/IBMtape0 erase it took hours and get device busy..
How to delete a tar archive,files, directories in the Tape.

# Backup logs are stored here
LOGBASE=/root/backup/log

# Backup dirs; do not prefix /
BACKUP_ROOT_DIR="u05 "

# Get todays day like Mon, Tue and so on
NOW=$(date +"%a")

# Tape device name
TAPE="/dev/IBMtape0"

# Exclude file
TAR_ARGS=""
EXCLUDE_CONF=/root/.backup.exclude.conf

# Backup Log file
LOGFIILE=$LOGBASE/$NOW.backup.log

# Path to binaries
TAR=/bin/tar
MT=/bin/mt
MKDIR=/bin/mkdir

# ------------------------------------------------------------------------
# Excluding files when using tar
# Create a file called $EXCLUDE_CONF using a text editor
# Add files matching patterns such as follows (regex allowed):
# home/vivek/iso
# home/vivek/*.cpp~
# ------------------------------------------------------------------------
[ -f $EXCLUDE_CONF ] && TAR_ARGS="-X $EXCLUDE_CONF"

#### Custom functions #####
# Make a full backup
full_backup(){
local old=$(pwd)
# Create a date stamp name for the archive
FILE_NAME=/tmp/`date +%F`.tar.gz
# Create a gzip tar archive in /tmp/yyyy-mm-dd.tar.gz
tar czf $FILE_NAME $DIR $BACKUP_ROOT_DIR
# Copy the archive file to tape
cp $FILE_NAME $TAPE
# Remove the archive file
rm $FILE_NAME
$MT -f $TAPE rewind
$MT -f $TAPE offline
cd $old
}
[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
mail -s 'Backup status=$status' < mail id>
The backup job finished.

End date: $(date)
Hostname : $(hostname)
Status : $status
# Make a partial backup
partial_backup(){
local old=$(pwd)
cd /
$TAR $TAR_ARGS -cvzpf $TAPE -N "$(date -d '1 day ago')" $BACKUP_ROOT_DIR
$MT -f $TAPE rewind
$MT -f $TAPE offline
cd $old
}
[ $? -eq 0 ] && status="Success!" || status="Failed!!!"
mail -s 'Backup status' <mail id>
The backup job finished.
End date: $(date)
Hostname : $(hostname)
Status : $status

# Make sure all dirs exits
verify_backup_dirs(){
local s=0
for d in $BACKUP_ROOT_DIR
do
if [ ! -d /$d ];
then
echo "Error : /$d directory does not exits!"
s=1
fi
done
# if not; just die
[ $s -eq 1 ] && exit 1
}

#### Main logic ####

# Make sure log dir exits
[ ! -d $LOGBASE ] && $MKDIR -p $LOGBASE

# Verify dirs
verify_backup_dirs

# Okay let us start backup procedure
# If it is monday make a full backup;
# For Tue to Fri make a partial backup
# Weekend no backups
case $NOW in
Mon) full_backup;;
Tue|Wed|Thu|Fri) partial_backup;;
*) ;;
esac > $LOGFIILE 2>&1


All times are GMT -5. The time now is 06:41 PM.