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
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)
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)
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)
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)
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)
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)
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)