Last modified: Nov. 12, 2011
CIS 191B
Extra Credit Lab 8x: Backup and Restore
The purpose of this lab is to
explore the different types of backups and their methods of restoring. We will
look at three utilities often used for backups and learn the advantages and
disadvantages of each.
Background
You have used the tar command before, for gathering up many files
into a single tarball file, for the purpose of transferring them to a new location. cpio and dump are two other utilities that work similarly, but use different syntax.
Part One: TAR
Aside from the cp command, used in backing up a
single file, tar is probably the easiest command-line backup
utility that comes with UNIX or Linux. It archives many files into a single
archive that can be stored on removable media or on another file system.
TAR is particularly useful for archiving a directory tree.
TAR archives may be created for a variety of purposes, from backups to packaging
files for easy downloads. In this procedure, we will backup the /var/log
directory, and then restore a particular file from the archive.
- Log in as root and change directory to /var/log.
- How much disk space is being used by this directory?
du -sh /var/log
Record this number.
- When you make an archive, you must decide what you are going to archive and where you are going to archive it. The where can be a device or a file.
There are no archive devices on the virtual machines, so we will backup these log files to a tarball file.
- Create the archive in the /tmp filesystem:
tar cvf /tmp/logfiles.tar .
Note that the dot (.) references the local directory that is being archived.
- Change directory back to /tmp, and view the archive you just made:
tar tvf logfiles.tar | more
Note the names of the files in the archive. Are they absolute or relative?
- How large is this tarball? How does it compare to the disk usage recorded above?
- Extract from this archive the messages file:
tar xvf logfiles.tar messages
Why doesn't this work?
- Try again with:
tar xvf logfiles.tar ./messages
- How does the messages file you just extracted compare with the messages
file in the /var/log directory? Are they the same? Look at dates, permissions,
ownership as well as contents. (You can use the diff command to
compare content.)
- Create a second tarball by archiving the /var/log directory as an
absolute name:
tar cvf logfile2.tar /var/log
- List the files in this archive and notice how the names are specified. Are they absolute or relative?
Save this listing to a file called lab8x.
- See if you can extract the messages file from this archive.
Where will the file be extracted?
- Clean up by deleting the two logfile tarballs and any subdirectories you
may have made.
Part Two: CPIO
Tar is useful for archiving files that are all contained
within a few directories, but what happens if you want to archive files that
might be spread all over several file systems? For instance, what if you wanted
to collect all the files belonging to the group users on the system? Or
what if you wanted to back up all files that have been modified in the last
week? For that, the better command is cpio.
- For this procedure, you will have to log on as root, or su
to the super-user account.
- If we were performing the seventh day of a differential backup, then we
would want to archive all the files on the root file system that have been modified
within the last 7 days. To see which files these are, run the following
find command:
find / -mount -mtime -7 | more
What does the -mount option do?
- Since cpio reads its input of filenames from stdin and
writes the archive to stdout, we can use a pipeline to archive these
files to a level-7 backup:
find / -mount -mtime -7 | cpio -vocB > /home/level-7.bak
- Verify the backup with the following command:
cpio -vitB <
/home/level-7.bak
Where would these files be restored if we were to extract this archive?
Concatenate this listing to the lab8x file:
cpio -vitB < /home/level-7.bak >> lab8x
- Why did I have you create the archive in the /home directory rather then in /root or /tmp?
- Extract this archive:
cpio -vicdumB < /home/level-7.bak
Notice that the archive file remains even after you extract its contents.
Where do the extracted files go?
Part Three: DUMP
In this procedure we will use the dump utility to perform a level 0 backup of the boot, home, and root (/) file system. We will
backup the /boot and /home file systems onto the root filesystem in the /opt directory, then we will dump the root file system to a partition you will
create on your hard disk, /dev/sda10.
- Log in as root and use the fdisk command to create a 1GB logical partition (sda10).
After writing the partition table, run the partprobe command so that the kernel will recognize the change.
Note: since we are going to use this device as an archive, there is no need
to format it as a filesystem.
- Verify that the /etc/dumpdates file is present:
ls -l /etc/dumpdates
Note: it may well be 0 bytes in size, but it should be there.
- Mount the /boot filesystem, and run the df -h command on the /boot directory. Then unmount the boot file system, /dev/sda2:
mount /dev/sda2 /boot; df -h /boot; umount /boot
Note the size of the filesystem usage.
- Run the dump command:
dump -0uf /opt/boot.0-MM-DD-YY /dev/sda2
where MM-DD-YY is the current month-day-year.
Note that dump can backup a file system that isn't mounted.
- Check the size of the dump archive you just made and compare it to the
size of the file system. Also look at the entry in the /etc/dumpdates file and note what information is recorded.
- Now dump the /home file system using the dump command:
dump -0uf /opt/home.0-MM-DD-YY /dev/sda9
- You will now dump the root file system to the hard disk device, /dev/sda10. This will backup not only the root file system, but also your backups of
/home and /boot. To begin this backup, bring the system down to runlevel 1:
init 1
- Now perform the backup with the following command:
dump -Ouf /dev/sda10 /dev/sda3
- When the dump is complete, append to the bottom of your lab8x file the contents of the /etc/dumpdates file:
cat /etc/dumpdates >> lab8x
- You may now remove the two backup aarchive files in the /opt directory.
- Change directory to /etc and do a long listing of all files ending in cap:
ls -l *cap
- Move the above four files to the /tmp directory.
- To actually restore the files from this archive, you must be in the top
directory of the file system, in this case: /.
- Use the interactive mode of the restore program to extract
the three files you just moved.
Hint: use the man page to see how
this is done using the -i option. When restore asks you which volume do you want restored, select 1
If asked to set owner/mode, select "n".
- When you are done with the restore, how do the files you restored compare to the files you moved to the /tmp directory? How about the inodes?
To turn in
Copy the lab8x file to your account on
opus.cabrillo.edu using the following command:
scp lab8x logname@opus.cabrillo.edu:lab8x