|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Trying to mess with a backup script but not having much luck
If anyone can give a helping hand would be much appreciated. Im trying to get the script to: 1) Prompt for the name of the user account; 2) Prompt for the file name to be backed up 3) If the filename is a directory display the message "<Filename> is a directory" Else Backup the user file by copying it into users home directory Hope to get some help Cheers, |
|
#2
|
||||
|
||||
|
Show us what you have so far.
I assume it will have: A prompt for user name. A check to see if user is valid. A prompt for file to select A check on file type A check on permissions on source and target A copy A check of exit code from the copy |
|
#3
|
|||
|
|||
|
Sorry for late reply havent been on over weekend.
So far what i got i need a check on the setup,spacing and how exactly to finish the copy of the file. echo "Enter username:" read name if [ -d \home\$name ] then echo "Valid username" ls $name else echo "Invalid username" echo "enter filename" read file if [ -d $file] then echo "Valid file" else echo $file "is a directory" (not to sure whether $file should be inside the brackets here) now as for the actual backup will it just be a: cp source | $file not to sure here. Thanks in advance any help at all will be appreciated. |
|
#4
|
||||
|
||||
|
Ok, just a few pointers,as what you have is basically just about there.
Just checking that a directory called /home/$name exists does not prove that user exists/is valid. You do not have a check for null input when you prompt for data. Your if statements need a balancing fi to terminate them. The test for file/directory seems to be inverted from your original requirement. Some hints: Code:
# Obtain user name
echo "Enter username: \c"
read name
if [ "$name" = "" ]
then
echo "No name entered, exiting"
exit 2
fi
# Check user exists and get their home directory
namedir=`grep "^${name}:" /etc/passwd | awk -F: '{print {$?}'`
if [ "$namedir" = "" ]
then
echo "Invalid user"
exit 3
else
if [ ! -d $namedir]
then
echo "user home dir is not valid"
fi
fi
# Got here,so valid user, valid home dir.
# Now get file to backup
echo "Enter file to backup: \c"
read $file
if [ ["$file" = "" ]
then
echo "No file specified"
exit 4
fi
if [ -d $file ]
then
echo "$file is a directory"
elif [ ! -f $file ]
echo "$file is not a standard file or does not exist"
else
cp $file $namedir
cp_rc=$?
if [ [ $cp_rc -ne 0 ]
then
echo "backup of $file to $namedir failed - code $cp_rc"
fi
fi
__________________
According to Sod's Law, buttered toast lands butter side down, when dropped. Per nature, cats always land on their feet. So, what happens when you strap buttered toast to the back of a cat and throw it out a window?. |
|
#5
|
|||
|
|||
|
Quote:
Thanx heaps for the help man when i get some time later on tonight ill get in there and have a fiddle around. And ill have to "man" some of those commands and learn something new Thanx again ill let you know how i go |
|
#6
|
||||
|
||||
|
Meant to say that the ? in the awk command is just a placeholder as I cannot, off the top of my head, remember which colon-delimited field the hoe directory is! Find that out and change the ? to the right number.
And it is nice to see someone willing to RTFM! ![]() |
![]() |
| Viewing: Dev Shed Forums > Web Site Management > Scripts > Back up script |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|