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 rewind
Backup directory /www and /home with tar command (z – compressed):# tar -czf /dev/st0 /www /home
Find out what block you are at with mt command:# mt -f /dev/st0 tell
Display list of files on tape drive:# tar -tzf /dev/st0
Restore /www directory:# cd /
Unload the tape:
# mt -f /dev/st0 rewind
# tar -xzf /dev/st0 www# mt -f /dev/st0 offline
Display status information about the tape unit:# mt -f /dev/st0 status
Erase the tape:# mt -f /dev/st0 erase
You 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:
- Howto – Use tar command through network over ssh session (i.e. writing to tape on a remote Linux server system)
- Download : Linux / UNIX Tar Full and Incremental Tape Backup Shell Script
- This small how-to covered all options used in day today life; however, it is highly recommended that you go through man pages of mt and tar command for more options/information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 43 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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
I think you should talk about verifying the backup just after copying files to tape
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.
Info about how to manage multiple tapes, and what happens when a tape gets full would be handy!
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,
Hi am new to linux redhat and require some information in determining if tape drive compression is enabled on the system (ibm ultrium lto3
tapeinfo -f /dev/st#
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.
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.
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)
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
Thank you very much for this tutorial. It’s really useful.
Thanks to Daniele too.
Thank you! Works great.
thanks to all of you
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 ).
my k-dat software is crashed
what is solution
You can’t use -M (multiple tapes) with -z option (compression).
$ tar -clpMzvf /dev/st0 /anydir
tar: Cannot use multi-volume compressed archives
# mount (disk name like = /dev/sdc1) /mnt/backup
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.
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.
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!
Hello ,
thanking for this help menu… How we check the size of tape after writing or tack backup files …
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.
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.
For eject try the below command
mt -f /dev/nst0 offline
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
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.
Thanks,
P
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 ;-)
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
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…
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.
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.
ubunut/linux has been left beinhd quite a bit. i’m open to any helping out especially for linux and macos
Great script at the end, very handy as i have 1.1tb to back up on an 800gb SDLT
Please I need your help and support for the Following Issue:
How I can backup data from Linux to a SDLT tapes 600GB?
Eslam
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?????
Could you please guide me the procedure to take multiple backups on single tape?
how to take the backup using append method
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?
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 YborStudent.hccfl.edu 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 YborStudent.hccfl.edu 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/ example.com:/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.
Hi
I need to take backup on hp tape drive.
Please share the procedure of backup the directory using wtar
Thank you so much guys. Helping me out again and again.. :)
How to start writing after the last tar? (mt -f /dev/st1 eom) Is not workind!!! Plz help!!!!
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.