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!!! |
Quote:
Code:
tar cvf archivetest.(date '+%m%y%d').tar *.txt |
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!! |
Quote:
Code:
tar cvf archivetest.$(date '+%m%y%d').tar *.txt Code:
c@CW8:~$ echo tar cvf archivetest.(date '+%m%y%d').tar *.txt Give menu options to tar:
|
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!!!!!!! >_< |
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:
Assuming you are, here are commands to create a directory tree to test with: Code:
mkdir ~/test_dir |
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?..... |
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. |
Ok thanks...Ill try again in the morning and see how far I can get! :)
Thanks again. |
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 |
I'm doing the same question at the moment. I know this is > 12 months late but was a solution found to this problem?
|
Quote:
|
Thanks TB0ne!
|
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 |
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 07:22 AM. |