Sponsored Content
Top Forums Shell Programming and Scripting restoring file to its default location... Post 302378013 by Scott on Sunday 6th of December 2009 11:43:05 AM
Old 12-06-2009
You've asked a number of questions pertaining to the same topic.

I've written this for you as a start:

BackupScript:
Code:
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:

Code:
. ./BackupScript

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

Code:
Backup filename

to backup filename and
Code:
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:
Code:
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
}


Last edited by Scott; 12-06-2009 at 02:23 PM.. Reason: removed some debug echos
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Restoring a single file...???

Can anyone please help...? Managed to do a ufsdump of files to tape. Having trouble using ufsrestore to pull a single file back by filename?? I have dumped a single file to tape also because looking through the other threads, I noticed that you have to tell it to skip files before you get to... (1 Reply)
Discussion started by: Jonathan
1 Replies

2. Solaris

Restoring TAR'd file to different location

Is it possible to restore a TAR'ed file off of a tape to a location other than the original location? If so, how? (The MAN pages give examples of how to restore only to the originating location.) Thanks!! (1 Reply)
Discussion started by: FredSmith
1 Replies

3. Shell Programming and Scripting

Restoring a file

I'm new to Unix and have just wrote a little program to move files to a recycle bin (a Directory i created) and restore them. The problem is that i need to keep track of all the full filenames so that i can restore them to the right place. I did this by creating a file called delreg and putting the... (4 Replies)
Discussion started by: zoolz
4 Replies

4. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

5. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

6. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

7. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

8. Shell Programming and Scripting

Restoring a file to its original location

Hello everyone, I am attempting to make a recycling bin type application in shell script (tcsh). I have the whole part of the application done where someone can recycle files from one location to the recycling bin (the lower half of the program), this is not a problem. However I wanted to make... (7 Replies)
Discussion started by: tastybrownies
7 Replies

9. UNIX for Dummies Questions & Answers

Restoring deleted file with rm -rf

Is there a way I could recover a deleted text file with "rm -rf" command. Running CentOS 6.5. Thank you. (5 Replies)
Discussion started by: galford
5 Replies
All times are GMT -4. The time now is 12:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy