FreeBSD backups
October 23, 2006

This page details how to make backups with a SCSI tape device using FreeBSD. Two commands are of interest - these are mt and tar. Use mt to prepare the tape, use tar to make the backup.

To check drive status:

mt -f /dev/sa0 status

To erase the tape:

mt -f /dev/sa0 erase

To rewind the tape:

mt -f /dev/sa0 rewind

To retension the tape:

mt -f /dev/sa0 retension

To backup to tape (note: this overwrites any previous backup):

tar -cv /usr/data/dir
tar -cv data/dir
tar -cv /usr/data/dir /usr/data/test

To list tape contents:

tar -tv

To restore from tape:

tar -xv
tar -xv data/dir
tar -xv usr/data/dir/filename.ext

To eject a tape:

mt -f /dev/sa0 offline

Other mt commands:

mt -f $tapedev rewind
mt -f $tapedev density dds-3
mt -f $tapedev comp DCLZ
mt -f $tapedev comp on

If you try an 'mt status' while not logged in as root you get:

mt: /dev/nsa0: Permission denied

If you try an 'mt status' while the drive is empty you get:

mt: /dev/nsa0: Device not configured

If you try an 'mt status' while the drive is busy you get:

mt: /dev/nsa0: Device busy

If you try an 'mt status' when the tape is ready you get something like:

Mode      Density              Blocksize      bpi      Compression
Current:  0x25:DDS-3           variable       97000    DCLZ
---------available modes---------
0:        0x25:DDS-3           variable       97000    DCLZ
1:        0x25:DDS-3           variable       97000    DCLZ
2:        0x25:DDS-3           variable       97000    DCLZ
3:        0x25:DDS-3           variable       97000    DCLZ
---------------------------------
Current Driver State: at rest.
---------------------------------
File Number: 0  Record Number: 0        Residual Count 0

If you try a 'tar -tv' while the tape is unformatted you get:

tar: Unrecognized archive format: Inappropriate file type or format

If you try a 'tar -tv' on an empty tape you get:

tar: Error opening archive: Error reading '/dev/sa0': Input/output error

Here's a sample backup script. It's not fancy, but it works. Note that it doesn't verify, it only lists the contents of tape once the backup is done. I hope to improve on this at some point.

#!/bin/sh

#### backup script 3 - by Stuart Udall 2006 - http://www.blazingfibre.net/ ####

LOGFILE=/var/log/backups.log

# begin
clear
echo "====> backup process started"
date
echo "backup process started:" > ${LOGFILE}
date >> ${LOGFILE}
echo >> ${LOGFILE}

# rewind the tape
echo "rewinding tape.."
mt rewind

# backup user data
echo "starting backup.."
cd /usr
tar -cv data

# list contents of tape to file
echo "contents of tape:" >> ${LOGFILE}
echo "listing tape contents to file.."
tar -tv >> ${LOGFILE}

# eject the tape
mt offline

# exit
echo "backup process completed"
date
echo >> ${LOGFILE}
echo "backup process completed:" >> ${LOGFILE}
date >> ${LOGFILE}

# mail the logfile to various places
grep -v "\-\-r\-\-" ${LOGFILE} | mailx -s "backup report" -c cc@mailbox.com monitor@sysadminsmailbox.com

# dump the logfile to a user-accessible area
cp ${LOGFILE} /usr/data/dir/

Note that the script changes to the appropriate directory first, then runs the backup with a relative path (in the above example, it backs up a folder called "data"). If an absolute (full) path is used, the backup works but restores can only be done to the source folder, since the path is stored in the backup. That is, the filenames in the backup contain the path they came from, when they are restored they go back to that path. To restore to an alternate directory, ensure to use a path without a leading / character.

Schedule a backup by editing /etc/crontab and adding the following lines to the end of the file (this runs the backup every weekday at 23:00):

# run the backups
0      23       *       *       1-5     root    /usr/home/username/backups.sh

How to have a script send an email (sendmail must be configured first):

cat messagebody.txt | mailx -s "subject" destination@mailbox.com

If members of the wheel group are permitted to use the backup device, a special 'backups' user can be created, who is a member of the wheel group, who can do backups, but does not need the root password. This user has a call to the backup script in his login script, followed by automatic logout at the end of the login script - this permits manual backups to be run by anyone with the password to the 'backups' account - but nothing else. Use these commands to permit members of the wheel group to use the backup device:

chgrp wheel nsa0.0
chgrp wheel sa0.0

How to have a script log itself out:

kill -9 -1

How to give the wheel group execute permissions to the script:

chmod 754 backups.sh

How to give the wheel group write permissions to the logfile:

chmod 664 backups.log