Assignment 2: Shell and Perl Programming

Part 1: A backup program

You are to write Bourne shell program that will back up files. The shell program will be called backup and will take one or more arguments which are the names of the files and/or directories to back up.

Your program must work on penguin. It is highly recommended that you do all of your work on a Linux machine. Some of the commands described below are not available on Solaris.

Operational Details

The back-up copy of a file will be stored in a subdirectory of a directory called backup in the directory containing the file. The name of the subdirectory corresponds to the time the back-up was made.

If the file already exists in a back-up subdirectory, and the last modified time of the back-up is more recent or equal to the current copy, then do not make a new back-up of the file. Note that the contents of the files may be the same even if they have different last modified times.

The name of the subdirectory in backup for the current backup will be the current time. Use date -Iminutes for the name of the subdirectory. (Hint: there is a reason I'm recommending this as the subdirectory name!)

If no files need to backed up then the subdirectory of backup corresponding to the back-up time should not exist when the program terminates.

If a directory is given on the command line, then all the files and directories in that directory should be backed up. (Think recursion.)

The program should run silently if it does not encounter errors. Reporting error conditions are left to your discretion. Remember that the errors should be relevant to the user, and take advantage of error messages produced by the commands you are using. You may want to suppress some error messages produced by some of the commands.

Tips

The -t option to stat gives the inode information on one line. The last modified time is in the 13th field of this line. The time is given in terms of the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970), which is how most times on Unix are stored.

Other useful commands/programs are dirname and basename.

My reasonably commented solution is approximately 75 lines including blank lines. Solutions that are much longer than this will be penalized.

You need only use shell programming concepts that we have covered in class. You can use positional parameters beyond the 9 that are available in older Bourne shell versions. To refer to the positional parameter 10, use ${10}. Otherwise the shell thinks you mean positional parameter $1 followed by the string 0.

Here is an example of how backup might be used. Imagine that time passes in between the calls to backup:

% backup a
% ls -l
-rw-rw-r--    1 karen    karen          12 Sep 30 22:01 a
-rw-rw-r--    1 karen    karen           0 Sep 30 22:02 b
drwxrwxr-x    4 karen    karen        4096 Sep 30 22:30 backups/
drwxrwxr-x    4 karen    karen        4096 Sep 30 22:31 c/

% ls -l backups/*
backups/2002-09-30T22:30-0400:
total 0
-rw-rw-r--    1 karen    karen          12 Sep 30 22:30 a

% touch a
% ls -l 
-rw-rw-r--    1 karen    karen          12 Sep 30 23:27 a
-rw-rw-r--    1 karen    karen           0 Sep 30 22:02 b
drwxrwxr-x    4 karen    karen        4096 Sep 30 22:30 backups/
drwxrwxr-x    4 karen    karen        4096 Sep 30 22:31 c/

% backup a
backups/2002-09-30T22:30-0400:
total 0
-rw-rw-r--    1 karen    karen          12 Sep 30 22:30 a

backups/2002-09-30T23:40-0400:
total 0
-rw-rw-r--    1 karen    karen          12 Sep 30 23:40 a

Note that if we called backup c/e we will create a backup directory in c in which to store the back-up copy of e.


Part 2: Now available


What to submit

You will submit electronically under the assignment name A2 two files: a Bourne shell program called backup and a Perl program called trackinclude.
Last modified: Mon Oct 7 21:59:45 EDT 2002