CS290 Lab1

Writing a Shell Script to Automatically Backup a Directory

Goal

In this lab you will learn shell scripting using bash.

Part 1: Shell Scripting

A shell is an application program that interprets the commands that a user types and executes programs. There are several shell programs: Bourne shell (sh, bsh), C-Shell (csh), Korn-shell (ksh), T-cshell (tcsh), and bash. You will use bash in this lab to implement your shell script.

First of all, read the notes about shell programming in Unix Shell Scripting (google also "bash tutorial" for other tutorials) and study the examples in the lab1-src directory. You can get those examples by downloading the file lab1-src.tar.gz and by typing:

bash> gunzip lab2-src.tar.gz
bash> tar -xvf lab2-src.tar
Now that you are an expert in shell scripting write the following script to automatically back up a directory.

Part 2: Write a Backup Script

Write a backup script backupd.sh  that will automatically backup a directory.

    backupd.sh dir backupdir interval-secs max-backups

This script will first backup the directory dir by making a copy of dir in the directory backupdir/<current-date>. Then, every interval-secs if the dir has been modified since the last backup, it will back it up again in the directory backupdir/<current-date>.

<current-date> will have the format YYYY-MM-DD-hh-ss

To compare if a directory has been modified, first run a "ls -lR dir > ls-lR.last " before backing up dir. The command "ls -lR" lists the entries of a directory and subdirectories and displays the last modification time of every file. Then every interval-secs run again a" ls -lR dir > ls-lR.new". If ls-lR.new and ls-lR.last are different then back up the directory again into backupdir/<current-date> and copy ls-lR.new into ls-lR.last.

Algorithm:

    ls -lR > ls-lR.last
    copy the directory dir into backupdir/<current-date>
    while (1) {
sleep interval-secs
ls -lR > ls-lR.new
diff ls-lR.new ls-lR.last
if (if they are different) {
copy the directory dir into backupdir/<current-date>
mv ls-lR.new ls-lR.last
}
}

Part 3: Limit backup directories to max-backups 

Also make sure that the maximum number of backup directories is limited to max-backups. Only the "max-backups" recent directories will be kept and the other ones will be erased.

Part 4: E-mail notification

Also, when the script performs a backup, it will send an e-mail to the user notifying that a backup has taken place.

How your script will be graded


Feature
Grade (Max)
Backup is done correctly
30
Backup is done at regular intervals
10
Backup is done when there is a difference
10
Number of backups is limited to max-backups
20
Mail Notification works
10
Script is well documented
10
Robustness, runs without crashing.
10

Turning in your lab

To turnin this project

0. Login to lore

1. Write a README file with your

Place the README file and backupd.sh in the directory lab1-src/

2. Make sure that your script is well written, with good comments and it is easy to understand.

3. Go one directory above lab1-src and type:

        turnin -c cs290 -p lab1 lab1-src

4. Verify that your files have been turned in by typing:

        turnin -c cs290 -p lab1 -v

The deadline for lab2 is 11:59pm September 13th, 2010.