restoring file to its default location...

Hello everyone,
I am new to unix shell.

                I have a file called Path.txt....and i have data in that as
      
                1 abhi
                2 avi
                3 ash   so on.....

                1 ,2 ,3 is the index.....

                my concern is user will give me input as

                restore 1

                then i have to restore abhi file to its default location..
                if restore 2
                then again same thing....

                user can give restore 1 2 3
                in this case all three files are suppose to get restored to the 
                default location.

                what i am doing is

                using for loop


                fname=\`grep -w "^$index" $HOME/UnixCw/backup/Path.txt\`
                echo "FILE: $fname"
 
                 in FILE  if user said restore 11 then FILE will contain

                 11 filename

                 now i want to restore the filename

                 i think i have to take this file name in some variable.
                 and by using mv i can restore..but i am not sure about how i 
                 can do this...please help me out

You've asked a number of questions pertaining to the same topic.

I've written this for you as a start:

BackupScript:

BACKUP_LOCATION=/backup

function Backup {
  if [ -z "$1" ]; then
    echo "Specify a file."
    return 1
  fi

  if [ "$(echo $1 | grep "^/")" ]; then
    FILE=$1
  else
    FILE=$PWD/$1
  fi

  if [ ! -f $FILE ]; then
    echo "File to backup ($FILE) does not exist."
    return 2
  fi

  echo Backup $FILE...

  mkdir -p $BACKUP_LOCATION$(dirname $FILE) 2>/dev/null && \
    cp $FILE $BACKUP_LOCATION$FILE && \
    echo $FILE >> $BACKUP_LOCATION/index.lst
}

function Restore {
  cat -n $BACKUP_LOCATION/index.lst
  echo "Enter numbers of files to restore:"
  read NUMBERS
  for FILE in $NUMBERS; do
    NAME=$(sed -n "${FILE}p" $BACKUP_LOCATION/index.lst)

    echo "Restore $NAME..."
    cp $BACKUP_LOCATION$NAME $NAME
  done
}

It lacks a number of features:

  • It doesn't check if the file is already backed-up
  • It doesn't remove the file once you restore it
  • There is limited error handling
  • There's no confirmation before a file is restored (add -i to the cp in the Restore function if you want it)
  • It assumes that your backup directory is /backup. If you don't have permissions to create this, change it to somewhere else
  • The Restore function is interactive only

You should run this script by sourcing it, like:

. ./BackupScript

This gives you two functions you can call directly from the command line:

Backup filename

to backup filename and

Restore

to choose which file(s) to restore.

It should work in Bash, but it's Korn shell.

To remove the need for the "index" file, replace the functions with this:

function Backup {
  if [ -z "$1" ]; then
    echo "Specify a file."
    return 1
  fi

  if [ "$(echo $1 | grep "^/")" ]; then
    FILE=$1
  else
    FILE=$PWD/$1
  fi

  if [ ! -f $FILE ]; then
    echo "File to backup ($FILE) does not exist."
    return 2
  fi

  echo Backup $FILE...

  mkdir -p $BACKUP_LOCATION$(dirname $FILE) 2>/dev/null && \
    cp $FILE $BACKUP_LOCATION$FILE
}

function Restore {
  find $BACKUP_LOCATION -type f | cat -n
  echo "Enter numbers of files to restore:"
  read NUMBERS
  for FILE in $NUMBERS; do
    NAME=$(find $BACKUP_LOCATION -type f | sed -n "${FILE}s+$BACKUP_LOCATION++p" )

    echo "Restore $NAME..."
    cp $BACKUP_LOCATION$NAME $NAME && rm $BACKUP_LOCATION$NAME
  done
}

some clue as below:

file=$(egrep "^$1" Path.txt | awk '{print $2}')
echo $file