Backing Up and Restoring Objectives Upon completion of this module, you should be able to: • Use the tar and compress commands to back up files and directories • Use the jar command to s
Trang 1Backing Up and Restoring
Objectives
Upon completion of this module, you should be able to:
• Use the tar and compress commands to back up files and directories
• Use the jar command to save and compress files and directories
• Position a tape to a selected data set using the mt utility
Discussion – How frequently do you back up or restore files?
Contents Overview
In order to back up or easily transfer files via ftp or another method, you need to be able to package and restore the files easily This module covers the following commands:
• compress
In addition, specific instructions for backing up and restoring your home directory using tar
and compress are included
The tar Command
The tar (tape archive) command enables you to back up single or multiple files in a directory hierarchy
Command Format
tar function_[letter/modifier] [output file]
filename(s)/directory(s)
Function Letters
c Create a new tar file
t List the table of contents of the tar file
x Extract the specified files from the tar file
Trang 2Function Modifiers
f Specify the tar file ( /tmp/file.tar) or tape
drive (/dev/rmt/x) if other than default
v Execute in verbose mode
Saving a Directory to Tape
You can create the tar file using a relative path name
$ cd /home
$ tar cv user1
This archives the user1 directory to the default device,/dev/rmt/0
This command is also useful for creating archive files to share with other users or attach
to mail messages
$ cd
$ tar cvf files.tar file1 file2 file3
file1
file2
file3
$
You can view the table of contents of the tar file using tf or tvf option:
$ tar tf files.tar
file1
file2
file3
$
$ tar tvf files.tar
-rw-rw-r vominh/vominh 10240 2002-09-16 04:10:21 file1
rw-r vominh/vominh 12345 2002-09-16 04:10:21 file2 -rw-rw-r vominh/vominh 10240 2002-09-16 04:10:34 file3
Use tar command to extract files from an archive:
$ tar xf files.tar
Note – The existed files will be overwritten without a notice.
Trang 3The compress Command
The compress command is used to compress files using a special format to reduce the size of the file from 20 percent to 80 percent
Command Format
compress filename
Compressing a File
The following example uses the -v (verbose) option to compress a file called
bin.file:
The file is compressed and a Z suffix is appended to the file name
$ compress -v bin.file
bin.file: Compression: 70.84% replaced with
bin.file.Z
Files are uncompressed using the uncompress command
Command Format: Uncompressing
uncompress filename
Uncompressing a File
$ uncompress bin.file.Z
The jar Command
The jar command is similar to the tar command, but compresses the resulting file in the same step It is a Java™ application that combines multiple files into a single JAR (Java archive) file It is also a generalpurpose archiving and compression tool, based on ZIP and the ZLIB compression format The jar command was originally created for Java programmers to download multiple files with one request, rather than having to issue a download request for each separate file jar is standard with the Linux operating system, but is available on any system that has Java virtual machine (JVM) installed
Command Format
The syntax for the jar tool is almost identical to the syntax for the tar
command
Trang 4jar options [ output file ] filename(s)/directory(s)
Options
c Create a new jar file
t List the table of contents of the jar file
x Extract the specified files from the jar file
f Specify the jar file ( /tmp/file.jar) or tape drive (/dev/rmt/x) if other
than default
v Execute in verbose mode
Note – It is not necessary to use a hyphen (-) before options when
issuing the jar command
Adding All the Files in a Directory to an Archive
$ ls
dante dir1 dir3 file1 file3 fruit
dante_1 dir2 dir4 file2 file4 fruit2
$ jar cvf bundle.jar *
adding: dante (in=1320) (out=744) (deflated 43%)
adding: dante_1 (in=368) (out=242) (deflated 34%)
adding: dir1/ (in=0) (out=0) (stored 0%)
adding: dir1/coffees/ (in=0) (out=0) (stored 0%)
adding: dir1/coffees/beans (in=0) (out=0) (stored 0%)
adding: dir1/coffees/nuts (in=0) (out=0) (stored 0%)
adding: dir1/fruit/ (in=0) (out=0) (stored 0%)
adding: dir1/trees/ (in=0) (out=0) (stored 0%)
adding: dir2/ (in=0) (out=0) (stored 0%)
<output omitted>
adding: file1 (in=1696) (out=874) (deflated 48%)
<output omitted>
Note – The jar tool is not intended for backing up symbolic links
Error messages will display if this is attempted
The mt Command
The mt command enables direct tape manipulation
Trang 5Command Format
mt [ -f tape-device-name] command[ count]
The -f option is used to specify the tape device file name, typically a no-rewind device file name If the -f option is omitted, the value of the TAPE environment variable is used to determine the tape device to manipulate
Commands
status Displays status information about the tape drive
rewind Rewinds the tape
retension Rewinds the cartridge tape completely, winds
the tape forward to the end of the tape, then rewinds back to the beginning of the tape to smooth out the tape tension
erase Erases the entire tape
fsf Forward skips count tape files
bsf Backward skips count tape files
eom Skips to the end of the recorded media
off Ejects 4- or 8-mm tapes
Note – Only the unique prefix of a command is required For example,
use ret in place of retension
Use the following mt command to rewind a tape:
$ mt rew
To have the tape skip forward a tape file, use the following command:
$ mt -f /dev/rmt/0n fsf 1
The device name /dev/rmt/0n tells the tape drive not to rewind the tape after it has performed a function This device naming convention can also be used when archiving files with tar and jar
Note – The retension option does not work with 8-mm tapes.
Trang 6Backing up and Restoring the Home Directory
Backing up the Home Directory
Since most work is done in users’ home directories, these directories are often scheduled for backup on a nightly basis by system administrators If your home directory is not being backed up by the system administrator, it is good practice to perform a regular nightly backup of changing data
1 Create the tar file using a relative path name
$ cd /export/home
$ tar cvf /tmp/home.tar user1
2 Display the table of contents
$ tar tvf /tmp/home.tar
3 Compress the tar file
$ compress -v /tmp/home.tar
/tmp/home.tar: Compression: 80.85% replaced with /tmp/home.tar.Z
4 Change directory to /tmp Back up the compressed file on to a tape using the tar
command
$ cd /tmp
$ tar cv home.tar.Z
Restoring the Home Directory
To restore files,
1 In the home directory, make a new directory and change to it to prevent overwriting of files
$ cd
$ mkdir newhome
$ cd newhome
Trang 72 Extract the home directory from tape.
$ tar xv
3 Uncompress the file
$ uncompress home.tar.Z
4 Extract the tar file
$ tar xvf home.tar
5 Move files as needed
Exercise: Saving and Restoring Files
Exercise objective – In this exercise you will practice backing up files to tape or disk.
Tasks
Note – If you get a “Permission Denied” error while performing the following exercises, check
the write protect switch on the tape
Complete these steps:
1 Back up your home directory to a file using the tar command
2 Compress the tar file and archive it to tape
3 Use the jar utility to back up ~/practice
4 Use the tar utitlity to back up ~/practice and compress the file
5 Compare the tar and jar file backups of ~/practice for size
6 Retension the tape
Workshop Labs
Use what you have learned so far in this course to work through the following:
Trang 81 Another user in your group needs a copy of the files in your personal binary directory Create a compressed tar file, place it in that directory, and make it available to the other user
Exercise Summary
Discussion – Take a few minutes to discuss what experiences, issues, or discoveries you had
during the lab exercises
Manage the discussion here based on the time allowed for this module, which was given in the “About This Course” module If you find you do not have time
to spend on
discussion, then just highlight the key concepts students should have learned from the lab exercise.
• Experiences
Ask students what their overall experiences with this exercise have been You might want to go over any trouble spots or especially confusing areas at this time.
• Interpretations
Ask students to interpret what they observed during any aspects of this exercise.
• Conclusions
Have students articulate any conclusions they reached as a result of this exercise
experience.
• Applications
Explore with students how they might apply what they learned in this exercise to
situations at their workplace.
Exercise Solutions
1 Back up your home directory to a file using the tar command
$ cd
$ cd
$ tar cvf /tmp/homedir.tar login-ID
$ tar tvf /tmp/homedir.tar
2 Compress the tar file and archive it to tape
$ cd /tmp
Trang 9$ compress homedir.tar
$ tar cv homedir.tar.Z
3 Use the jar utility to back up your practice directory
$ cd ~
$ jar cvf ~/practice.jar practice
4 Use the tar utility to back up your practice directory and compress the file
$ cd ~
$ tar cvf ~/practice.tar practice
$ compress -v practice.tar
5 Compare the tar and jar file backups of your practice directory for size
$ ls -l *jar *tar.Z
6 Retension the tape
$ mt ret
Check Your Progress
Before continuing on to the next module, check that you are able to accomplish or answer the following:
Use the tar and compress commands to back up files and directories
Use the jar command to save and compress files and directories
Position a tape to a selected data set using the mt utility
Think Beyond
What tasks do you currently complete that you can apply the tar or jar commands to, besides saving and restoring files and directories?