Cover V10, I04


Using One Script to Back Up Linux and Solaris

Clark Cooper

Backups are one of many critical tasks systems administrators must perform. Unfortunately, we too often hear horror stories of those who have not done them. This article introduces two shell scripts, and, which can be used to handle cpio backups and restores for an administrator needing a simple solution.

With a desire to have backup and restore scripts that can function across both Linux and Solaris using a native command, cpio was chosen as the archive method. Attempts with tar on Solaris showed that the shipping version would not back up the root file system as a relative path. Alternatively, the recommended Solaris method of ufsdump was not included with Linux. Using cpio itself did not come without problems. The GNU version typically used on Linux appeared to output everything to standard error. For instance, the following should have created two files, one for stdout and the other for stderr:

find . -print | cpio -oacBv -O /dev/st0 1>/tmp/files 2>/tmp/files.err
Instead, output for both stdout and stderr were directed to /tmp/file.err. Because there was a need to have these in their respective files, a workaround had to be implemented. Both scripts have been tested with Red Hat Linux 7 and Solaris 2.x.

Warning! Testing is essential for any backup and recovery process. All backup and restore operations mentioned in this article should be tested before production usage. Also, running applications may need to be shut down before using this backup. Consult your application's documentation for backup requirements.

All code for this article is available for download from the Sys Admin Web site at:
Backup Features
  • A backup completion email is sent to a defined recipient. An example of a successful email message is as follows:
    The backup on testbox to device /dev/rmt/0 was \
    successful. See testbox:/var/log/backup_0.11222000 \
    for a list of files backed up.
  • The backup script with no parameters will back up all locally mounted file systems to the tape device defined by $TAPE_DEVICE. You can optionally pass the tape device or file systems to be backed up as parameters.
  • All backup log files are named according to the tape device used and the date. The following running example of created a log file named "backup_0_11142000":
    # /usr/local/
    ***Rewinding tape device /dev/rmt/0...
    ***Backing up /export/home /opt /var /
     To view files as they are backed up, "tail -f \
    This allows you to monitor the files as they are being backed up or later search for a file's backup status. Because the backup logs are unique, they must be maintained. This can be achieved by defining a rotation period. Any logs exceeding this period in days will be removed.

  • You may define a list of errors that can be ignored. If the backup script is reporting an error for something that is of no concern (e.g., No such file), you may exclude this error so that successful messages are received. You may also define a list of directories or files to exclude from the backup. Restore Features

  • The restore script ( prompts the user for a list of files or directories to restore. For example:
    # /usr/local/lbin/
    Enter filenames or directories to restore (End with a ".") 
    NOTE:  Directories should be followed with a "/*"
           Example /home/*
    You may restore to an alternate directory. As follows:

    Would you like to restore to an alternate directory? (y/n): y
    Please enter the alternate directory to restore to: /tmp
    Because the backups are done as an absolute path:

  • You may restore the file or directory elsewhere, allowing you to keep the current file or directory intact. If you don't restore to an alternate directory, the current file or directory will be overwritten.
  • Restores are recorded into a file. This allows you to monitor the restore in progress:
    ***Rewinding tape device /dev/rmt/0...
    ***Restoring  => /tmp
     To view files as they are restored, "tail -f /tmp/files_restored"
    or search for a file's restore status at a later time. Note above that we are restoring to an alternate directory of /tmp. If we had chosen not to install to an alternate directory, this would have been /.


    The following steps comprise the minimum that should be done to get the setup working:

    1. If you do not want to use the -d option to pass the tape device, modify TAPE_DEVICE in both and to reflect the device you want to use.

    2. In, check EmailRecipients to reflect any email addresses to which a completion status should be sent.

    Script Operation

    The backup script takes two optional parameters and does not require any user input:

    Usage: ./ [-d tape_device] [-f /dirone /dirtwo /dirthree...] [-?]
    All arguments are optional and will override their respective \
    definitions in ./
    -d tape_device is the backup device (e.g., /dev/rmt/0).
    -f defines which file systems to back up. If not passed, all \
    locally mounted file systems will be backed up.
    The script can be put into cron for automation. Here is an example cron entry for every weekday night at 11:00 pm:

    0 11 * * 1-5 /usr/local/lbin/ >/dev/null 2>&1
    The restore script takes one optional parameter, which can be used to specify the tape device you want to use:

    # ./ -?
    Usage: ./ [-d tape_device]
      -d tape_device is the backup device (e.g., /dev/rmt/0). Optional \
      and will override $TAPE_DEVICE in ./
    Input from the user in the form of the full path of the file or directory to be restored is required. Any directories to be restored should be appended with /* (see example under "Restore Features" above). This is not absolutely necessary if the directory currently exists. Cpio requires directory restores to be appended with /*. However, the restore script checks whether the requested restore is an existing directory and, if not appended with /*, will correct the entry. Be aware that if the requested directory is not appended with /* and only exists on tape, the directory will not be restored. After all files and directories to be restored have been named, enter a . to go on to the next step. You will then be asked whether an alternate directory is desired. Following this, the restore will begin.

    Recovery by Hand

    If you need to recover complete file systems by hand with the cpio command, use the following guidelines to restore from the archive (tape_device represents the device, e.g., /dev/rmt/0).

  • Root File System Only:

    Use the -f option with cpio to exclude all non-root file systems backed up as demonstrated with /var and /home:

    cpio -icBdumv -I tape_device -f "var/*" "home/*"
  • Selective File Systems:

    Specify the file system(s) as shown here for restoring /usr and /export:

    cpio -icBdumv -I tape_device "usr/*" "export/*"
  • Everything on Tape:

    Do not enter any patterns as shown below:

    cpio -icBdumv -I tape_device

    These backup and restore scripts are not designed to span across multiple tapes on one device. Therefore, these scripts should only be used when you have a tape device that can back up all desired data onto one tape. However, if you have multiple tape devices, you can break up the backup to fit onto multiple tapes. Use the -d and -f options to make these definitions (see Script Operation). Backups are done as full. Incremental backups are not optional.

    Clark has worked as a systems administrator and/or client support for nine years. He is currently employed by VC3 Inc. and can be reached at: