0% found this document useful (0 votes)
6 views2 pages

Shell Scripting for Directory Backup

The document outlines a Unix lab exercise focused on shell scripting. It includes tasks for creating a backup script using 'tar', modifying the script to use variables, and setting up a cron job to run the script weekly. The exercises emphasize user input handling and automation through scheduling.

Uploaded by

Nelson
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Shell Scripting for Directory Backup

The document outlines a Unix lab exercise focused on shell scripting. It includes tasks for creating a backup script using 'tar', modifying the script to use variables, and setting up a cron job to run the script weekly. The exercises emphasize user input handling and automation through scheduling.

Uploaded by

Nelson
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unix Lab 10 - Shell Scripting Lab 6

Exercise 1:
Write a Shell script that backs up a directory using “tar” commands into a single
file. As part of the script, ask for input into what you want to back up, and the
output location and filename. Example:

Example Input:
Enter the directories or files for backup: /etc /var/log /home/mydirectory
What do you want to name this file (add .tar): [Link]
Where do you want to backup the file: /home/myhome

The script should then do this:


tar –cvf /home/[Link] –exclude='[Link]'
/etc /var/log /home/mydirectory

Exercise 2:

Modify the shell script above for the following:

1) Instead of asking for Input, utilize 3 variables


example: $1 = backupfiles
$2 = backupname
$3 = backuplocation
2) Output a message if the user does not enter 3 variables.

example: mybackup “/etc /var/log /home/mydirectory” “mybackup-10-09-


[Link]”
Since only two entries are entered:
printout: Incorrect syntax: please enter the following syntax. (Give them
syntax)

Example Input:

mybackup “/etc /var/log /home/mydirectory” “[Link]”


“/home/myhome”
My script
Output

Exercise 3:
Create a cronjob using the script from Exercise 2.

The script should run at 1:02 am every Friday night

*** Hint: Remember to run crontab –e


Do a crontab –l to list after complete

Common questions

Powered by AI

The steps to create the shell script include: 1) Prompt the user to enter the directories or files they wish to back up, 2) Ask the user for a name to save the backup file with a .tar extension, and 3) Request the location for saving the backup. The script will then use the 'tar' command with options -cvf to create a tarball that includes the specified directories, excluding the output file name itself. For example, if the user inputs to backup /etc, /var/log, and /home/mydirectory, with the backup named mybackup.tar and saved in /home/myhome, the command would construct as: tar –cvf /home/myhome/mybackup-10-09-2017.tar –exclude='mybackup-10-09-2017.tar' /etc /var/log /home/mydirectory .

To schedule a shell script to run at 1:02 am every Friday night, you use the Unix 'cron' utility. First, open the cron editor using crontab –e, then add a new line for the cron job schedule with the time and command to execute the script. The cron timing syntax for 1:02 am every Friday is '2 1 * * 5', where '2' indicates the minute, '1' indicates the hour, and '5' is for Friday. After adding the cron schedule line like '2 1 * * 5 /path/to/script', you should save the changes in the cron editor. You can verify the scheduled tasks by running crontab –l, which lists all the current cron jobs .

The modification involves using positional parameters $1, $2, and $3 for inputs instead of interactive prompts. $1 holds the directories or files to back up, $2 stores the backup file name, and $3 designates the backup location. If fewer than three variables are provided, the script should output a message that incorrect syntax is used and show the correct syntax usage. This could alert the user to input all necessary information by printing an error message and an example of the correct command line usage. For example, it should display: 'Incorrect syntax: please enter the following syntax.' Followed by an example: mybackup "/etc /var/log /home/mydirectory" "mybackup-10-09-2017.tar" "/home/myhome" .

You might also like