LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Christmas exams study....menu driven backup script. (https://www.linuxquestions.org/questions/linux-newbie-8/christmas-exams-study-menu-driven-backup-script-918690/)

badlyconfused 12-14-2011 08:36 AM

Christmas exams study....menu driven backup script.
 
Hi everyone,

I am studying for christmas exams and we have been given a sample paper and been told it will be nearly the same as the real one..

I am having a problem with the last questions (5)

In question 4 it asks to create an archive of a directory I have already (archivetest) of all files with a .txt extension

I do this by running:

tar cvf archivetest.tar *.txt

and this is successful.


Then question 5 asks to add a date and timestamp to the files...

I have tried adding $(date '+%m%y%d') in different locations in the script above but just cant get it working!


this is also stopping me from making a script using the above two answers which gives menu options to
tar a specific file
or
browse to select a file you want to tar
or
a collection of files containing a certain filename
or
a directory

If anyone could let me know what they think about this as I have been trying it for over a week now and the lecturer is not very forthcoming with helpful info except "look over your notes" -_-

Thanks in advance for any help!!!

catkin 12-14-2011 08:46 AM

Quote:

Originally Posted by badlyconfused (Post 4549696)
Then question 5 asks to add a date and timestamp to the files...

I have tried adding $(date '+%m%y%d') in different locations in the script above but just cant get it working!

If you want to add the timestamp to the tar archive file name, this should do it:
Code:

tar cvf archivetest.(date '+%m%y%d').tar *.txt
but it's not a very friendly timestamp -- from the appearance of nnnnnn, it could be myd, dym, ymd (which is the most sortable) ...

badlyconfused 12-14-2011 08:49 AM

Thanks Catkin
 
Ok that is working well now.

I think it was the $ sign. I was convinced it had to be part of the command!!

Any idea of the script? To be honest the lecturer was a bit "all over the place" and nothing is clear enough from notes to be able to write anything tht works!!

catkin 12-14-2011 09:49 AM

Quote:

Originally Posted by badlyconfused (Post 4549709)
Ok that is working well now.

I think it was the $ sign. I was convinced it had to be part of the command!!

Any idea of the script? To be honest the lecturer was a bit "all over the place" and nothing is clear enough from notes to be able to write anything tht works!!

It doesn't work for me; sorry, it should have been
Code:

tar cvf archivetest.$(date '+%m%y%d').tar *.txt
A useful technique to see what bash creates from a command like that is to echo it, here shown demonstrating that the $ is required:
Code:

c@CW8:~$ echo tar cvf archivetest.(date '+%m%y%d').tar *.txt
bash: syntax error near unexpected token `('
c@CW8:~$ echo tar cvf archivetest.$(date '+%m%y%d').tar *.txt
tar cvf archivetest.121114.tar *.txt

Regards the script, is this the specification:
Give menu options to tar:
  1. a specific file
    or
  2. browse to select a file you want to tar
    or
  3. a collection of files containing a certain filename
    or
  4. a directory
If so:
  1. OK -- just a fixed name.
  2. What sort of browse? Are you supposed to start a GUI and somehow get back the path of a file or is it a small number of files somewhere so you can generate a subsidiary menu listing them all to choose one by typing its number?
  3. Doesn't make sense. Does it mean a collection of files with the last path component including a fixed/specifiable string ("certain" is lousy English used that way)? If so under which directory?
  4. OK -- just a fixed name.
First rule of programming is to have a clear specification of what the program is required to do!

badlyconfused 12-14-2011 10:03 AM

Thanks Catkin
 
Sorry this is getting to me now I have been trying it for so long,

it is:

"a script to give menu options to choose to tar:

1:a specific file
2:browse to select a specific file
3:a collection of files containing requested text in their name
4:a driectory"

that is the actual questions.

He worder it badly but basically the script needs to tar whichever option is choosen and give it a date and time aswell....

This is just too confusing!!!!!!! >_<

catkin 12-14-2011 10:18 AM

The tarring is easy now, you just need to script the menus. Given an ambiguous specification and no practicable opportunity to clarify it, you can interpret it as you want :D

How about this:

A script to give menu options to choose to tar:
  1. a specific file
  2. a specific file from a list of files in a specific directory
  3. several files in a specific directory with names containing a string entered by the user
  4. a specific directory's contents
You OK with that?
Assuming you are, here are commands to create a directory tree to test with:
Code:

mkdir ~/test_dir
cd ~/test_dir
touch foo.txt bar.txt foobar.txt

Now we come to the tricky part; I am willing to help you write the script but not to write it for you. First off, what do you know about how to script a menu?

badlyconfused 12-14-2011 10:26 AM

HEre is what he has given us as a sample...........


your own menu driven backup script
Apart from being a reasonably practical exercise in learning bash scripting, it might also be
vaguely useful, and reasonable guidelines are given, so it should not take too long to do – a
few hours at most.
BUT THINK, PLAN AND EVEN CODE BEFORE GOING TO THE LAB.
THEN CHECK IT OUT A PART AT A TIME, BEFORE JAMMING IT ALL TOGETHER.
Later, (not as an assignment, but as ongoing demonstrations) we can look at modifications
and extensions to it, which would make it more useful, such as incremental backup, (backing
up only changed files etc.) and varying the underlying backup command, from tar to
something else.
The assignment is designed in relatively independent component parts, so I donʼt expect you
all to get it all worked out, and you get marks for what is accomplished. So it is not an ʻall or
nothingʼ assignment.
The numeric orderings below indicate the order in which things (c/sh)ould be done, which is
effectively
from the inside :-
1) the basic backup command : tar as youʼve used in your last assignment
to the outside : basically naming the archive.
2) with a filetype and timestamp in the name
(I know that files have system times intrinsically associated with them,
but these are often changed in copying and backing up, so it is handy to have the real
original time as part of the name : besides you can see at a glance the timestamp from
the archive name!)
3) with a menu to allow the user to select the options: file, directory etc.
GUIDELINES
Tar basically works as: tar flags $source $destination
So you have to modify the strings for source and destination
Remember strings are concatenated simply by
$c=$a + $b + “whatever you like yourself”
Part 3 is basically choosing and setting the source string, from the source files
Part 2 is basically setting the destination from string for the archive name,
Which includes some filetype info from source + the timestamp when the script ran.
Part 1 is basically issuing the archive command : tar flags $source $destination
FORMAL SPECIFICATIONS … APPLY ONLY TO FILES IN THE CURRENT DIRECTORY.
(You can assume there are no directories within; so the scriptester directory will do fine!)
So here is the formal spec…insofar as formality is useful…
3) Give menu options for user to choose which files etc. to archive…
a) a specific file [10%]
b) browse to select a specific file [10%]
(merely an ls and then repeat menu for option a) is easiest) [20%]
c) a collection of files containing requested text in their name [10%]
d) the current directory [20%]
the archive_name is to be set according to the name of the
2) Include some source information in the archive name
a) filetype / file extension to the archive file [10%]
b) along with date and time of archive [10%]
(Here's a code snippet along those lines)
# the timestamp
this_run=`/bin/date +%F+%T` # e.g. 2011-05-06+10:56:59
# to clean up date strings for appending to filenames, replace all non-numeric symbols with '_'
# e.g. from 2011-05-06+10:56:59 to 2011_05_06_10_56_59
# (other chars may confuse filesystem)
this_run="`echo $this_run | sed -e s/[^0-9]/_/g `"
and just include $this_run as part of the tar archive filename...
tar sourcepath tarred_road/$archive_name_$thisrun [10%]
1) tar whatever is asked in menu 3...
setting the archive_name according to selections from menu input..
Please do not try to implement an untar or dearchive option, as youʼll have enough to do…it
could be done by generating a toc from the archive, listing it like the browse option 3b) and
then allowing an extraction as in 3a) – but it would only complicate issues needlessly.



I cant make senes of this to be honest. He had shown us quickly how to script menus but not given concise notes.....would you have any suggestions as to what to do etc?

Im not asking for you to write the actual script just give me a general idea or any helpful links or websites?.....

catkin 12-14-2011 10:37 AM

Thanks -- that's a bit clearer now. The easiest way to generate a menu is the select command. Example here (scroll down ~80%) and reference here (scroll down to select).

Post back when you get stuck or when you have a basic menu working. The commands to run when the user chooses a menu option can be added later. When posting the script or its output, used CODE tags -- easiest in "Advanced Mode" using the # button.

badlyconfused 12-14-2011 01:21 PM

Ok thanks...Ill try again in the morning and see how far I can get! :)

Thanks again.

asimba 12-14-2011 01:39 PM

I would have modelled mine on Amanda pattern.

I would have take following basic options

1. Create backup
a. Specify Source file/directory
*verify if source directory already exists - verify file version/date/time etc - option to overwrite existing file or rename existing destination file or use new destination name

2. Delete Backup
a. Specify backup location
3. Verify Backup
b. specify backup location to verify
4. Exit

I also would have used a configuration file to save last location/last backup done and so forth so as to make life easy + not to miss out logging. So forth
I apologize if I missed context

joepeger 01-07-2013 04:35 AM

I'm doing the same question at the moment. I know this is > 12 months late but was a solution found to this problem?

TB0ne 01-07-2013 08:40 AM

Quote:

Originally Posted by joepeger (Post 4864435)
I'm doing the same question at the moment. I know this is > 12 months late but was a solution found to this problem?

Post #8 has two links that essentially solve your problem. It tells you how to write a menu-driven script, how to do input verification, and has full examples. Modify them to suit your needs.

joepeger 01-07-2013 01:34 PM

Thanks TB0ne!

Habitual 01-07-2013 01:52 PM

I hope this doesn't get me kicked off the LQ Christmas Card List...

Basic Linux Menu « Bash recipes
ActiveState Code A menu box - Linux Shell Scripting Tutorial - A Beginner's handbook
bash select menu example
Menu Driven Shell Script

joepeger 01-08-2013 10:00 AM

Clueless
 
Thanks for your help thus far:

I think I have completed part a) and part b) but I'm having trouble with parts c) and d). Part c) I really don't have a clue about, part d) I'm not sure what's wrong because it feels like what I have should work. Any help would be greatly appreciated.

Cheers,
Joe.



QUESTIONS:

3) Give menu options for user to choose which files etc. to archive…
a) a specific file [10%]
b) browse to select a specific file (merely an ls and then repeat menu for option a) is easiest) [30%]
c) a collection of files containing requested text in their name [10%]
d) the current directory [20%]

WHAT I HAVE COMPlETED:

#!/bin/bash

selection=
until [ "$selection" = "0"]; do

echo ""
echo "PROGRAM MENU"
echo "1 - Archive specific files"
echo "2 - Browse to select a specific file"
echo "3 - Archive files containing specific characters"
echo "4 - Archive current directory"
echo ""
echo "0 - Exit program"
echo -n "Enter Selection: "
read selection
echo ""
case $selection in

1 ) echo "This archives files"
echo "Please type name of file to archive"
read archivefiles
tar cvf archivetest.$(date '+%m%y%d').tar $archivefiles;;

2 ) echo "Browse and select a file to archive"
ls -l
read archivefiles
tar cvf archivetest.$(date '+%m%y%d').tar $archivefiles;;

3 ) echo "This archives files containing specific characters"
read ???
tar cvf archivetest.$(date '+%m%y%d').tar $???;;

4 ) echo "Select Current Directory:"
read cd
tar cvf archivetest.$(date '+%m%y%d').tar $cd;;

0 ) exit;;

* ) echo "Please enter 1,2,3,4 or 0"

esac
done


All times are GMT -5. The time now is 02:20 PM.