Small College homework

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Small College homework
# 1  
Old 11-29-2011
Small College homework

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

My work is to write or design a script in bash shell script format for the Linux O/S Debian distro "Knoppix 6.7".


What the script needs to do is basically delete temporary files and back up data from one directory/folder to another designated Backup.

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):


Code:
#!/bin/bash
#This script will automatically delete temporary files
rm -rf /tmp/*
rm -rf/var/tmp/*
fsck -A
exit

Code:
#! /bin/bash
# Variables
location=/home/veiset
directory=Documents
backuplocation=/media/veiset/backup
log=~/backup.log
echo -e “\nBackup started: `date`” >> $log
if [ -d $backuplocation ]; then
mkdir -p $backuplocation/`date +%y%m%d`
cd $location
tar -cvvf $backuplocation/`date +%y%m%d`/data.`date +%H%M%S`.tar.gz $directory
echo ” completed: `date`” >> $log
cp $log $backuplocation/backup.log
echo -e “\n — Backup completed –\n”;
else
echo ” FAILED: `date`” >> $log
echo -e “\n– WARNING: –”
echo -e “– BACKUP FAILED –\n”;
fi
exit

I'm not too sure on what else to do, and if I am doing it right!?

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
wanstead sixth form
london
england

Last edited by DukeNuke2; 11-29-2011 at 06:43 PM..
# 2  
Old 11-29-2011
You're on the right path as far as I can tell. A couple of suggestions/comments

1) I'm not sure why you're running fsck in your first script. Typically fsck is run on a filesystem that is not mounted. Maybe your instructions indicate you should, but it's not something that I've commonly (if ever) done especially in a tmp cleaner.

2) Your options to tar might be mistyped. Did you mean to have tar do the gzipping? I gather you did from the name of the output file, but the options aren't correct if that is the case.

3) Verbose messages from tar will not be captured in your log file. Have a look at the tar man page, or run a small test at the command line to see how you can capture the verbose output (I assume you want to).

4) Indention will make the script much easier to read.

Hope this helps.

---------- Post updated at 18:41 ---------- Previous update was at 18:39 ----------

One more thing I noticed.... You don't handle the log file the same when you have an error condition. If you want to copy it to the same spot regardless of the state (success or failure), then just have one copy command just before the exit which will take care of all cases.
# 3  
Old 11-30-2011
You shouldn't be calling fsck on a booted system, yes, especially not fsck -A. If you're lucky it will refuse to do anything, if you're not, you could mess up your system.

You shouldn't be using * on something that could have lots of files.

Code:
#!/bin/bash
# This will work sometimes, but is likely to die with 'too many arguments'
# when /tmp/ has a lot of files in it.  * has limits.
# rm -rf /tmp/*

# This should find all files and folder in the base directory of '/tmp'
# and print "rm -Rf file1 file2 ...".  Remove the 'echo' once you're sure
# it does what you want.
find /tmp -mindepth 1 -maxdepth 1 | 
while read LINE
do
        # Remove the 'echo' once you're sure it does what you want
        echo rm -Rf "$LINE"
done

You don't need to reopen $log 9,000 times to print 9,000 times. Open it once, and print to it.

Code:
exec 1>$log
echo "This line will now go directly into logfile"
echo "This line will still be printed to the terminal" >&2

Use printf, not "echo -e", as echo -e is not portable. Note that printf needs you to specify \n on the end when you want them.

Code:
echo -e "\nThis is not portable"
printf "\nThis is\n"

Run date once and save it in a variable, or else the date might just change halfway through the script on an unlucky midnight.

Code:
# Variables
location=/home/veiset
directory=Documents
backuplocation=/media/veiset/backup
backupdate=`date +%y%m%d`
backuptoday="${backuplocation}/${backupdate}"

Check the return values of things. Your script at present won't have any idea whether it succeeded or failed. Your script returns success even when it knows it failed, too.

And instead of nesting your if-statements, you should quit immediately. That way you can just list conditions to quit on instead of nesting 9 deep when checking 9 things.

If you want tar to make a tar.gz, you have to give it -z as well.

The "cd" is pointless since you're giving tar absolute paths anyway, you can remove it.

Code:
function die
{
        # Print to logfile
        echo "$@"
        # Print again to terminal
        echo "$@" >&2
        # return error code, not success
        exit 1
}

[ -d "$backuplocation" ] || die "Backup location $backuplocation doesn't exist"
mkdir -p "$backuptoday" || die "Couldn't create today's folder $backuptoday"

tar -zcvvf $backuplocation/`date +%y%m%d`/data.`date +%H%M%S`.tar.gz $directory ||
        die "tar failed"

Note that pasting these together won't give you a complete script, I'm commenting on bits and pieces and advising what to change.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making a bash script and small C program for a homework assignment

Here's the assignment. I'll bold the parts that are rough for me. Unfortunately, that's quite a bit lol. The syntax is, of course, where my issues lie, for the most part. I don't have a lot of programming experience at all :/. I'd post what I've already done, but I'm so lost I really don't know... (1 Reply)
Discussion started by: twk101
1 Replies

2. Shell Programming and Scripting

need a small help

Hi, sorry for inconvenience..wrong query posted. Thanks for your help. Thanks (1 Reply)
Discussion started by: kirankumar
1 Replies

3. Shell Programming and Scripting

Need small help

Hi all, I have two files in my folder 1.index.jsp 2.maintenance.jsp Once hit the URL in IE,It will call the index.jsp file than application working fine. I want to some maintenance in my application, during the application maintenance than it will... (1 Reply)
Discussion started by: lkeswar
1 Replies

4. Shell Programming and Scripting

Need small help

Hi, i have a client requirement to post the files into generic folder named as "source".file identification is to retrieve Publication code (Instance name) from the file name.So thereafter we move the files to different instances of specific folders for the same unix server.(means if the file... (2 Replies)
Discussion started by: kirankumar
2 Replies

5. Windows & DOS: Issues & Discussions

Research for College

I am researching the reasons why Unix / Linux is the chosen operating system versus Windows. I have had difficutly narrowing down resources. I am wondering if anyone has any favorite sources that they would care to share. Thanks Dan (2 Replies)
Discussion started by: isenhart
2 Replies

6. UNIX for Dummies Questions & Answers

College Question

Hello all, I have a quick question for those that might know, Would a degree in Computer and Information Science (CIS) be enough to get in the Unix Systems Administration door at the junior level? Or is Computer Science a better choice. The reason I ask is because a degree in CIS is the only... (8 Replies)
Discussion started by: bru
8 Replies
Login or Register to Ask a Question