Professional Documents
Culture Documents
Programming A Script With The BASH Interpreter: System Administration 2023/2024 Lab Work 7
Programming A Script With The BASH Interpreter: System Administration 2023/2024 Lab Work 7
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
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>
The directory path can be absolute or relative to the current directory. The
relative path is specified as “./dir”.
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.
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
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
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.