CT320 HW3: backup script
Bash Shell Script
The purpose of this assignment is to write a bash script called
backup that is both practical and uses many features of bash. Your
script may incidentally use other programs such as grep, but it must
be, primarily, a
This assignment consists of writing a backup and restore program for
user data on a Linux system. This assignment will be graded on how you
meet the requirements in this document, so read all parts of the
assignment carefully. We have listed the requirements and a list of
steps to follow.
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
The manager of system administration at your company is evaluating
backup solutions from different companies, and has not yet found one
that meets all the requirements. In the meantime he wants an interim
solution for backing up user data on the Linux workstations in the
engineering lab. Each workstation can have one or more user, each of
whom often uses the workstation every day and has large amounts of data.
The manager asks you to develop a bash script that will backup data on
all workstations nightly. The same script must be capable of allowing
you to restore data from the backup in case recovery is needed because
files were accidentally deleted.
Develop a bash script called
backup that takes three command-line
arguments. The first argument is an action, which is either
-R for restore. The second is the directory where user
accounts reside, which would normally be
/home. The third argument
is the directory where the backup files will reside, for example
-S action causes the script to:
- Enumerate all users in the specified directory.
- Create tar files, one for each user, in the backup directory,
that contain the entire contents of that user’s home directory.
- The tar file name should be username
.tgz, where username
is, well, the user’s name (login).
.tgz indicates that the
tar file should be compressed using the gzip option to tar.
The script must also create a file in the backup directory called
backups-here that contains backup status:
- user name
- home directory
- backup date, in epoch seconds (seconds since 1970 began)
- number of non-directory files
- number of directories
- total backup size in bytes (after compression)
-R action is requested, the script will:
- Enumerate all users in the
- Find the associated tar file for each user and
unpack it into
- If no backup is found, the
script should emit an error message that includes
There isn't anything available for username” and skip that user.
- Do a recursive file-by-file check of the files in the backup against
the files in the home directory. If a file exists in the backup but
not the home directory, restore it.
/tmp/workingdir before completing this command.
Here is an example of the format of
To verify your script, you must do the following:
- Create a directory in your home directory called
and create user directories in it called alpha, beta, gamma,
delta, and epsilon.
- Populate the user directories. At least one user should have at least
two levels of directory hierarchy, and every user should have multiple
- Put files in the hierarchy that have different content, size, and
protection, for example you could include audio, video, photo, and
- Use the find command to enumerate all of the files in
with a full listing including size, date, and protections, and store
- Run the script as
./backup -S ~/fakehome /tmp/archive. Ensure that
the tar files are created correctly in
/tmp/archive, and check the
- Delete several random files from different user directories at
different levels in the hierarchy to simulate files being accidentally
- Run the script as
./backup -R ~/fakehome /tmp/archive to restore
any missing files for all users.
- Repeat the
find command to enumerate all of the files in
~/fakehome with a full listing including size, date, and
protections, and store it in
- Verify that
restored.txt have identical
contents, thereby showing that the restore worked correctly.
- Display a usage message and exit for:
- wrong number of arguments
- bad first argument
- Display an error message and exit for directory arguments that:
- aren’t directories
- aren’t readable
- aren’t executable
- All error messages go to stderr, not stdout.
- Any error message must include
- Any error message that complains about an argument must mention
the offending argument.
No logging is required for the assignment. It’s ok if your script
displays a few extra messages, but don’t display an extra message
for every file or directory—that’s too much.
How to submit your homework:
Via web checkin, or:
~ct320/bin/checkin HW3 backup
How to receive negative points:
Turn in someone else’s work.