Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SYSTEM ADMINISTRATION 2023/2024

Lab work 7

TITLE
Programming a script with the BASH interpreter
GOALS
To learn the possibilities of the command interpreter as a basic tool for the Unix
system administrator.

DESCRIPTION
We are going to develop a script in the BASH language. This script is a backup
tool for directories in Linux.

The name of the script is backup.sh, it does not require any arguments and all
the working options are introduced through a simple menu described following.

ASO 2023-2024
Student last name, student first name

Backup tool for directories


---------------------------

Menu
1) Perform a backup
2) Program a backup with cron
3) Restore the content of a backup
4) Exit

Option:

Option 1 performs a directory backup. It will ask for the name of the directory to
save and will ask for confirmation before starting the backup. If the selected
directory is not valid, the program will send a message to the screen and to the
log file, and then go back to the main menu.

Menu 1
Path of the directory: /home <RET>

We will do a backup of the directory /home.


Do you want to proceed(y/n)? y

The <RET> key marks the end of the input data.

The directory path can be absolute or relative to the current directory. The
relative path is specified as “./dir”.

Option 2 allows programming a backup to be executed at a specific time with


the cron program.

System Administration Page 1


Menu 2
Absolute path of the directory: /home <RET>

Hour for the backup (0:00-23:59): 2:00 <RET>

The backup will execute at 2:00. Do you agree? (y/n) y<RET>

If there is a problem (for example, the directory to save is not valid) the program
presents an error message and returns to the main menu.
In options 1 and 2, the backup is generated in the following steps:
• The command used for backup is tar, and you must specify the option z
for the resulting file to be compressed.
• The backup file must be stored in /backups, directory that the script must
create if it does not exist.
• The name of the file is <dir-name>-YYMMDD-HHMM.tgz. For example,
if today is October 25, 2023 and the backup is done at 10:30, the file for
the backup described in Menu 2 will be “home-231025-1030.tgz”.
When the directory name has several levels (e.g. /home/user), we will
use the command “basename” to shorten the name of the file.
• If there was a previous file with the same name, the new one will
substitute the old one.

A log file named “backup.log” will be created in the directory /backups.


When a new backup is generated, we will send the following information to the
log file:

A backup of directory /home has been done on 2023/10/25 at


10:00.

The file generated is home-231025-1000.tgz and ocupies <n>


bytes.

Option 3 allows us to restore the content from a backup, leaving it in the current
directory. The program will present a list of the existing backup files and will ask
which one we want to recover. If the specified backup file does not exist, the
program will show an error message and will not start the recovery process.

Menu 3
The list of existing backups is:
home-231025-1000.tgz
user-230320-2000.tgz

Which one you want to recover: home-231025-1000.tgz <RET>

Option 4 terminates the program execution.

Besides registering logs about the performed backups, the log file
/backups/backup.log will contain information about errors in the execution
of the script backup.sh. A line in the log file has the format:
Date Time Error description

System Administration Page 2


I recommend that you write a second script, backup-cron.sh, for the tasks
programmed with cron.

REVIEW
 Commands echo, tar, date, find, basename, wc, crontab
 Control sentences of the BASH language
 Use of functions to structure the program
 Sentence read for data input

EVALUATION
The grading will consider the following aspects:
• The program should work according to the description.
• Good presentation of the program: blanks, indentation, use of
functions and control sentences, and any other aspect contributing to
the program’s legibility.
• Error control so that the program does not perform illegal operations.
• Registration of errors and cron information in the log file.

System Administration Page 3

You might also like