Linux Tape Backup With mt And tar Command Howto

by on April 19, 2006 · 37 comments· LAST UPDATED June 3, 2009

in , ,

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:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 37 comments… read them below or add one }

1 vinayak nageli October 4, 2006 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

Reply

2 nikto October 6, 2006 at 3:45 pm

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

Reply

3 Leon November 26, 2006 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.

Reply

4 Chris April 8, 2007 at 2:37 pm

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

Reply

5 Vijay March 5, 2008 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,

Reply

6 Athan March 10, 2008 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

Reply

7 Thanh September 27, 2013 at 9:48 pm

tapeinfo -f /dev/st#

Reply

8 Glen April 7, 2008 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.

Reply

9 orionsune April 23, 2008 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.

Reply

10 vz June 25, 2008 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)

Reply

11 Daniele December 30, 2008 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

Reply

12 Paolo May 5, 2009 at 2:12 pm

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

Thanks to Daniele too.

Reply

13 roberto June 4, 2009 at 4:34 am

Thank you! Works great.

Reply

14 rafi July 7, 2009 at 7:01 am

thanks to all of you

Reply

15 Anoop July 20, 2009 at 7:10 pm

I just wanted the TAR commands to Backup my data in etc folder to Tape media in Red hat enterprise 5.3 Linux OS. The mt option is not enabled then how can i verify and rewind the files written on the Tape ? How to enable the mt commands in Linux?

Please let me know about this. I can see both the Tape Drives in the Library online from the Host operating system ( Red hat Entrerprise Linux 5.3 ).

Reply

16 k -dat software in redhat linux November 18, 2009 at 3:05 pm

my k-dat software is crashed
what is solution

Reply

17 Jorge November 25, 2009 at 3:07 pm

You can’t use -M (multiple tapes) with -z option (compression).

$ tar -clpMzvf /dev/st0 /anydir
tar: Cannot use multi-volume compressed archives

Reply

18 Shafi Mohammad December 4, 2009 at 6:33 am

# mount (disk name like = /dev/sdc1) /mnt/backup

Reply

19 mahesh March 1, 2010 at 6:12 am

part3/radhika/Maildir/.Sent/cur/1214196153.M754850P28131V0000000000001609I004A8301_0.webmail.srishti,S=60346:2,S

tar: /dev/st0: Cannot write: No space left on device

tar: Error is not recoverable: exiting now

no I want to know that in this case Backup is happen or not.

please Reply me as soon as possible.

Reply

20 Yusoff April 8, 2010 at 6:15 am

Very useful article.

Just one thing to add. I tried the various commands above. For my end, when I run the command:-
# tar -clpMzvf /dev/nst0 /home
I got the following error:-
Cannot use Multi-volume compressed archive.

So I had to drop the “z” before it runs.
Just wondering if you’d know the reason why. In any case, just to highlight.

Reply

21 Jane October 16, 2010 at 5:39 am

I tried using the multiple tape option. Since my data is now bigger than my tape. However, it does not prompt me to put in the next tape the backup would just stop. Is there a way to pause and wait for the next tape to be inserted?

Cheers!

Reply

22 Gopinath April 12, 2010 at 11:31 am

Hello ,

thanking for this help menu… How we check the size of tape after writing or tack backup files …

Reply

23 Chandrakant September 30, 2010 at 2:52 pm

Hello,
I am using IBM Server with OS RHEL ver 5.0
I am using tar command to take the backup as follows
tar -cvfr /dev/st0 —file names—
This is appending the files to earlier backups no of times.
How to list out all files available in the dat?
How to restore the files which have appended no of times. i.e. 2,3,4,5 —.
Will you help me with the examples.

Reply

24 Camilo Macias November 11, 2010 at 10:14 pm

Hi everyone!!!!
I’m trying to record my Backp in a DAT 72 tape.

I’m using the following command:

tar cvMf /dev/nst0 $(date –date=’1 month ago’ +’%B”%Y’)

and in some part of the recording… shows the message:

Prepare volume #2 for `/dev/nst0′ and hit return:

I know it’s an stupid question, but….
I’ve done everything to eject “volume #1″ from the drive,
but anything works….
I’ve pushed “Enter”, but, it just read the same tape,
and shows the message again:

repare volume #3 for `/dev/nst0′ and hit return:

I’ve pushed the button “eject” right from the drive, nothing happens..
I’ve writen the eject command as follows:

mt -f /dev/nst0 eject

But once again…. nothing happens…

Am I doing something wrong??
Your support and guidance will be very helpfull.

Thanks!

pdt: Sorry for my horrible english! I’m from Bogota, Colombia.

Reply

25 Aravind January 20, 2011 at 10:34 pm

For eject try the below command
mt -f /dev/nst0 offline

Reply

26 Lucy Clark February 9, 2011 at 4:16 pm

I need help writing a script for Linux that will copy everything from tape to system directory. I built a Linux box with 3 TB hardware space. Now I have 60 tapes to move data from on to the system directory. I am using the following commands in order
1) mt –f /dev/st0 rewind
2) tar –xvf /dev/st0
3) tar –xvf /dev/st0 fsf (Using this to move to the next segment of the tape)
and then
4) tar -xvf/dev/st0

I keep repeating steps 3 and 4 until the end of the tape. This is very cumbersome. Being a nubie with Linux I am not sure how to simply the process. I am thinking there has to be a way of having the above in shell script or a command that copies everything from tape to system directory of the new box.
Thanks for your help

Reply

27 Marsupilamies March 16, 2011 at 7:53 am

I’ve noticed a trouble when you’re not on english system (for exemple, mine, french):
NOW=$(date +”%a”) gives not “Mon” for monday but “Lun.”
Maybe you can change NOW=$(date +”%a”) by NOW=$(date +”%u”), so $NOW will be a number (the same in each system).

And just change:
case $NOW in
Mon ) full_backup;;
Tue|Wed|Thu|Fri) partial_backup;;
*) ;;

by:
case $NOW in
1) full_backup;;
2|3|4|5) partial_backup;;
*) ;;

Hope it’ll help ;-)

Reply

28 martin March 22, 2011 at 7:43 pm

Hi,

My files are backed up as
srv/scripts/…
srv/www/.,..
srv/blablabla…

How to restore file srv/www/site1 to home dir?
Is it just
cd TOTHEDIRIWANTFILETOBERESTORED
# tar -xzf /dev/st0 srv/www/site1
?
Thank you

Reply

29 mahesh June 28, 2011 at 4:58 am

Hi all,

Please tell me how to find free space in the tape drive.
And how to restore the backup from the tape drive ( have to move the backup to other server).
Please give me clarity about tape drive information because iam confused about tape drive.

Thanks in adv…

Reply

30 Peter August 3, 2011 at 12:01 pm

I am also having a similar problem. please if you’ve receive a solution to your question then let me know the solution so that i can also apply it . Thanks.

Reply

31 Poppy February 7, 2012 at 3:10 am

Just to avoid snomoee having to spend a few hours restoring from backups; The first example would copy the files from server Y to server X, not the other way around.

Reply

32 Abdull February 10, 2012 at 5:22 am

ubunut/linux has been left beinhd quite a bit. i’m open to any helping out especially for linux and macos

Reply

33 DONALDO October 17, 2012 at 8:36 am

Great script at the end, very handy as i have 1.1tb to back up on an 800gb SDLT

Reply

34 Eslam Esmaiel February 27, 2013 at 9:21 am

Please I need your help and support for the Following Issue:
How I can backup data from Linux to a SDLT tapes 600GB?

Eslam

Reply

35 krishna chaitanya July 17, 2013 at 5:00 am

I have taken a Backup in LTO from RHEL 5.6 using tar command and i want to check the total size of data present in LTO..Please suggest me the command through which i can check the size?????

Reply

36 shiva December 23, 2013 at 4:21 pm

Could you please guide me the procedure to take multiple backups on single tape?

how to take the backup using append method

Reply

37 mohammed March 28, 2014 at 1:19 pm

To estimate the size of the backups, calculate the size of /home and /etc directories.
i) What command(s) did you use for this?

ii) What are the sizes?

Note that compressed archives will take about 30% less space.
However you will need to store one complete backup of /home and two backups of /etc.

iii) What is the estimate of the total space required for your backups?

iv) What would happen if the root partition (/) ran out of space?

Reply

Leave a Comment

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

Previous Faq:

Next Faq: