Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux script, Copy Folder to a Backup folder.

Status
Not open for further replies.

Kahungunu

Technical User
Aug 10, 2002
4
NZ
I am new to Linux using vi to create In bash shell.
I am trying to design a menu that allows input to select options.
I want an option that will copy my FriendsDetails folder to a backup folder".

I have created the menu interface I am going to use with the user being able to use Key Board letters to select each option but am unsure about a how the backup option script should be made.
[My code so far]
clear
If File BackupFriendsDetails Exists then
echo "File Do you want to overwrite File Yes, No"
if (Y)es then
cp /home/FriendsDetails > /home/FriendsDetails/BackupFriendsdetails
echo "File saved"
end if
if (N)o then
echo "Warning File not backed up"
clear
end if
else (first if) If file does not exist
mkdir /home/FriendsDetails/BackupFriendsDetails
cp /home/FriendsDetails > /home/FriendsDetails/BackupFriendsDetails
echo "File saved"
clear
end else
end if
[End code so far]
Any thoughts or suggestions of where help can be found will be greatly appreciated.

Thanks

 
Some suggestions and code:
A menu is easily created in this manner:
echo "
a) backup thisfolder
b) another option
etc...
"
read -p "Enter your selection: " selection

To process the menu selection this is an established way:

case $selection in

a|A) action ;;

b|B) action ;;

etc....
esac

An easier way to code for each option is to use shell functions. YMMV.
Here is an example of some code that recursively copies files and directories
from one directory to a target directory.
You should replace the actual cp and mkdir statements and run a few tests to
see what the echo lines are reading to
make sure you have it right.

function ex_w_err() {
msg=${1:-"undefined"}
printf "$msg\n"
exit 1 > /dev/null
}

function basetrans() {
dir=$1
target=$2
for xx in $dir/*
do
if [ -f "$xx" ] ; then
echo -e "Copying $xx $target/`basename $xx`"
cp $xx $target
fi
done
recurdir $dir $target
}

function recurdir() {
dir=$1
target=$2
if [ ! -z "$dir" ] ; then
for xx in $dir/*
do
if [ -d "$xx" ] ; then
mkdir $target/`basename $xx`
echo "Descending into $xx"
cdir=`basename $xx`
export cdir
recurdir "$xx" || ex_w_err "Failed in recursion at $dir/$xx"
else
echo -e "Copying $xx to $target/$cdir/`basename $xx`"
cp $xx $target/$cdir
fi
done
else
ex_w_err "Target directory was not passed to function recur()."
fi
return $?
}

So for your a case option you could now
put:
a|A) basetrans dirtobackup backupdir ;;

HTH.


 
Thank you very much marsd for taking the time to answer, and sharing your knowledge, much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top