Linux Tape Backup With mt And tar Command Howto

Magnetic tape is a non-volatile storage medium consisting of a magnetic coating on a thin plastic strip. Nearly all recording tape is of this type, whether used for video, audio storage or general purpose digital data storage using a computer. How do I make backup using tapes under Linux operating systems?

Linux (and other Unixish system) use mt command to control magnetic tape drive operation. You need to use mt command while working with tape drive. It allows you to reading and writing to tape.

The default tape drive under Linux is /dev/st0 (first SCSI tape device name). You can read more about tape drives naming convention used under Linux here. Following paragraph summaries command you need to use control tape drive for backup/restore purpose.

Rewind tape drive:# mt -f /dev/st0 rewindBackup directory /www and /home with tar command (z - compressed):# tar -czf /dev/st0 /www /homeFind out what block you are at with mt command:# mt -f /dev/st0 tellDisplay list of files on tape drive:# tar -tzf /dev/st0Restore /www directory:# cd /
# mt -f /dev/st0 rewind
# tar -xzf /dev/st0 www
Unload the tape:# mt -f /dev/st0 offlineDisplay status information about the tape unit:# mt -f /dev/st0 statusErase the tape:# mt -f /dev/st0 eraseYou can go BACKWARD or FORWARD on tape with mt command itself:
(a) Go to end of data:# mt -f /dev/nst0 eod(b) Goto previous record:# mt -f /dev/nst0 bsfm 1(c) Forward record:# mt -f /dev/nst0 fsf 1 Replace /dev/st0 with your actual tape drive name.

Linux Tape Backup Example

To backup to multiple tape use the following command (backup /home file system):
# tar -clpMzvf /dev/st0 /home
To compare tape backup, enter:
# tar -dlpMzvf /dev/st0 /home
To restore tape in case of data loss or hard disk failure:
# tar -xlpMzvf /dev/st0 /home
Where,

  • d : find differences between archive and file system
  • x : extract files from an archive
  • l : list the contents of an archive
  • p : ignore umask when extracting files
  • M : create/list/extract multi-volume archive (multiple tapes)
  • z : Compress backup using gzip
  • v : verbosely list files processed
  • f /dev/st0 : Tape device name
  • /home : Backup /home file system

Putting it all tougher

#!/bin/bash
# A UNIX / Linux shell script to backup dirs to tape device like /dev/st0 (linux)
# This script make both full and incremental backups.
# You need at two sets of five  tapes. Label each tape as Mon, Tue, Wed, Thu and Fri.
# You can run script at midnight or early morning each day using cronjons.
# The operator or sys admin can replace the tape every day after the script has done.
# Script must run as root or configure permission via sudo.
# -------------------------------------------------------------------------
# Copyright (c) 1999 Vivek Gite <vivek@nixcraft.com>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Last updated on : March-2003 - Added log file support.
# Last updated on : Feb-2007 - Added support for excluding files / dirs.
# -------------------------------------------------------------------------
LOGBASE=/root/backup/log
 
# Backup dirs; do not prefix /
BACKUP_ROOT_DIR="home sales"
 
# Get todays day like Mon, Tue and so on
NOW=$(date +"%a")
 
# Tape devie name
TAPE="/dev/st0"
 
# 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)
	cd /
	$TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR
	$MT -f $TAPE rewind
	$MT -f $TAPE offline
	cd $old
}
 
# Make a  partial backup
partial_backup(){
	local old=$(pwd)
	cd /
	$TAR $TAR_ARGS -cvpf $TAPE -N "$(date -d '1 day ago')" $BACKUP_ROOT_DIR
	$MT -f $TAPE rewind
	$MT -f $TAPE offline
	cd $old
}
 
# 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
 

Customize above shell script as per your needs and setup a cron job to execute it:
@midnight /path/to/tapebackup.sh

See also:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 1 trackback }

Exclude certain files when creating a tarball using tar command | Frequently Asked Questions
10.18.06 at 5:32 pm

{ 12 comments… read them below or add one }

1 vinayak nageli 10.04.06 at 5:33 am

I am new to Red Hat Linux. I want to know How to copy the whole content of tape drive to thum drive or on system directory

2 nikto 10.06.06 at 3:45 pm

I think you should talk about verifying the backup just after copying files to tape

3 Leon 11.26.06 at 3:55 pm

Hi. My name in leon. I thanks you for the support shown for linux. I wish you all the very best and good luck on your work.

4 Chris 04.08.07 at 2:37 pm

Info about how to manage multiple tapes, and what happens when a tape gets full would be handy!

5 Vijay 03.05.08 at 10:36 am

whall all should be checked as regular health check of the HP proliant servers having Linux OS platform. what all are the commands to do so?

Regards,

6 Athan 03.10.08 at 4:26 am

Hi am new to linux redhat and require some information in determining if tape drive compression is enabled on the system (ibm ultrium lto3

7 Glen 04.07.08 at 5:10 pm

I have found that on RHL FC8 the mt command doesn’t work. I have had to use the full mount command and it tells me its already mounted. Has the command changed for the free version versus enterprise?

some help on this would be appreciated.

8 orionsune 04.23.08 at 5:43 pm

mt is not mount… if mt doesn’t work then you don’t have the utility installed.
mt stands for magentic tape, it is not short for ‘mount’ like your probably thinking.

9 vz 06.25.08 at 10:05 pm

Hi Glen,

On my fedora FC8 mt was not installed from the beginning. The suitable package for installation is mt-st (something, mt-st-0.9b-4.fc8.i386)

10 Daniele 12.30.08 at 1:48 am

For multiple tapes just add the -M option.

Typical use:
tar -clpMzvf /dev/st0 /anydir <— to backup to tape
tar -dlpMzvf /dev/st0 /anydir <— to compare
tar -xlpMzvf /dev/st0 /anydir <— to restore

11 Paolo 05.05.09 at 2:12 pm

Thank you very much for this tutorial. It’s really useful.

Thanks to Daniele too.

12 roberto 06.04.09 at 4:34 am

Thank you! Works great.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , , , , , ,

Previous post: How to make a Linux File unchangeable ( unalterable ) so that no one can modify it

Next post: Linux display information about installed hardware