Linux Tape Backup With mt And tar Command Howto

last updated in Categories , , , , , , ,

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

  • 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

# 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 <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
# Last updated on : March-2003 - Added log file support.
# Last updated on : Feb-2007 - Added support for excluding files / dirs.
# -------------------------------------------------------------------------
# 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
# Exclude file
# Backup Log file
# Path to binaries
# ------------------------------------------------------------------------
# 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~
# ------------------------------------------------------------------------
#### Custom functions #####
# Make a full backup
	local old=$(pwd)
	cd /
	$MT -f $TAPE rewind
	$MT -f $TAPE offline
	cd $old
# Make a  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
	local s=0
	for d in $BACKUP_ROOT_DIR
		if [ ! -d /$d ];
			echo "Error : /$d directory does not exits!"
	# if not; just die
	[ $s -eq 1 ] && exit 1
#### Main logic ####
# Make sure log dir exits
[ ! -d $LOGBASE ] && $MKDIR -p $LOGBASE
# Verify 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/

See also:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


43 comment

  1. 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. 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?


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

  4. 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.

  5. 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.

  6. 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)

  7. 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

  8. 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 ).

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

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

  10. 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.

  11. 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.

    1. 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?


  12. 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.

  13. 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.


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

  14. 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

    1. Hey Lucy,

      I know this thread/post is a couple years old and I was wondering if you figured this out. I have multipe LTO6 tapes and need to copy everything off of them, but don’t want to do one TAR at a time. I am not sure how many TARs are on the files. I thought there was a (0..100) type code I could write in Cygwin, but sure.


  15. 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;;
    *) ;;

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

    Hope it’ll help ;-)

  16. Hi,

    My files are backed up as

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

  17. 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…

    1. 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.

  18. 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.

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


  20. 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?????

  21. 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?

  22. 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?

    Examine the space available on your drive, both free space (if any) as well as volumes that are using only a small amount of their allocated space. You will not only need space for the new /tmp volume, but also for a backup snap-shot volume, about 20% of the size of the /home volume.

    v) How much free disk space do you need altogether?

    If you don’t have sufficient free space available on your volume group then you must shrink one (or more) existing logical volumes to free up enough space for the new volumes.

    vi) Which volumes will you need to shrink, and what will be the new sizes of those volumes?

    Now that the volume group has sufficient free space for the new /tmp volume, create a new logical volume and then format it. Here is an example of creating a 1 gigabyte volume for /tmp, using the name LogVol04:

    vgdisplay # Note available space

    lvcreate -n LogVol04 -L 1G VolGroup00

    mkfs -t ext4 /dev/VolGroup00/LogVol04

    vi /etc/fstab # add entry for /tmp

    # Move old /tmp contents to new /tmp partition. This will likely require a reboot before the new /tmp files get used.

    mv /tmp /oldtmp

    mkdir /tmp

    chmod 1777 /tmp

    mount /tmp

    cd /oldtmp

    cp -a $(/bin/ls -A) /tmp

    # Do this after the next reboot:

    rm -ri /oldtmp

    vii) What is the exact find command which will find the names of all files and directories in /etc that have been modified in the past 24 hours?

    viii) What is the purpose of the -print0 find option, and why should you use it in a production-quality script?

    ix) What are the matching options for tar and cpio, to make an archive of the files found by find?

    x) Backup /home using dump. Use a level 0 dump. Since we don’t want to shut the system down to single user mode (or unmount /home). What are the exact commands you used?

    xi) Completely backup /etc using a compressed tar archive. What is the exact command(s) you used for this?

    xii) Completely backup /etc using a cpio archive. Now compress the resulting archive using either gzip or bzip2, whichever method you used for the tar archive in the previous step. What are the exact commands you used for this step?

    xiii) Which archive is smaller, the tar or the cpio archive of /etc, and by how much? Do you think the difference is significant?

    xiv) For each of the two archives, compute the MD5 checksum and store it in a file /tmp/name-of-archive.md5.

    xv) Copy the tar archive backup of /etc along with the matching MD5 checksum file, to your home directory on the server using scp command. What is the exact

    command(s) you used? Before running this command, check your quota on YborStudent and make sure you won’t go over your hard limit!

    xvi) Copy the cpio archive backup of /etc, along with the matching MD5 checksum file, to your home directory on the server using rsync command. What is the exact command(s) you used? (Note by default a modern rsync uses a secure SSH tunnel, the same as scp.) Before running this command, check your quota on YborStudent and make sure you won’t go over your hard limit!

    xvii) Describe briefly what the following command does, and list the meaning of each option used in your own words:

    rsync -HavRuzc /var/www/html/

    Now log into YborStudent and verify the integrity of the backup copies, using the MD5 checksum files.

    xviii) What are the exact commands to do that, and what are the results?

    xix) Still logged on YborStudent, extract the file /etc/group to your home directory from the tar archive. Then extract the file /etc/hosts to your home directory from the cpio archive. What are the exact commands needed for this? Be careful not to try to extract the absolute pathname or you will attempt to over-write /etc/group. (Don’t worry, you don’t have permission to do that!)

    xx) When done, delete both archives.

  23. Hello all,
    I just want to know whether we can check the backup type in LTO tape.
    I want to know whether the backup was done by tar command or dd command.

Leave a Comment