error in restore code

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

error in restore code

Postby Red Bow Tie » Tue Mar 08, 2011 3:19 am

Could someone tell me what I am doing wrong by entering the following code take from the restore script in LXF#126?

#!/bin/sh

if [ -f $1/../backup.tar.gz ]
then
tar -xz --file=$1/../backup.tar.gz
rsync --recursive --times --perms $1 $2
rm -rf $1
echo "Restore successful"
elif [ -d $1 ]
rsync --recursive --
times --perms $1 $2
tar -cz --file=$2/../backup.tar.gz $1 $2
rm -rf $1
echo "Restore successful"
then
else
echo "Restore failed"
fi

Every time I run it I get the following error
restore.sh: line 15: syntax error near unexpected token `else'
restore.sh: line 15: `else'
Red Bow Tie
 
Posts: 4
Joined: Sun Mar 06, 2011 11:20 pm

Postby Dutch_Master » Tue Mar 08, 2011 3:48 am

That code is either missing something or you managed to mess things up. Why invoke the backup twice? Check syntax, punctuation and correct order of commands given. (it's an exercise, try finding the fault yourself)

Oh and btw: when posting code, use the code tags, like this:
Code: Select all
[code]code goes here[/code]
(but only one code tag, I used 2 to let you see it)
Dutch_Master
LXF regular
 
Posts: 2445
Joined: Tue Mar 27, 2007 1:49 am

Postby nelz » Tue Mar 08, 2011 9:28 am

Your second then is in the wrong place, it should be after the elif line, not before the else.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8490
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

error in restore code

Postby Red Bow Tie » Tue Mar 08, 2011 3:10 pm

Thanks to both Dutch_Master and nelz for their help. Actually, you're right Dutch_Master for suggesting it as an exercise. I looked at the code over and over, compared it to other if elif else conditionals and came to the same conclusion that you and nelz stated. The then is in the wrong place.
The strange part is that this code was taken directly from the ShellScripting article on page 51 of LXF#126. Seems no one has actually tried the code out or no correction was ever published unless it was caught in a later issue of LXF. If it wasn't maybe someone should inform the publishers of a correction for this script.
Could you please answer one more question I have about this script? I'm not sure what you mean by code tags Dutch_Master, so if I do it wrong below please show me how to correct it.
What does this mean?
Code: Select all
 tar -xz --file=$1/../backup.tar.gz


I know the tar command is extracting the files from a previously compressed file specified on the command line, but what does the
Code: Select all
/../
refer to? I assume it's some kind of path statement :?:
Also, how would I restore the files only and not the folder? For example, I used the script as an exercise to backup the Pictures folder in another folder called BackupFolder. How do I restore the Pictures folder w/o restoring the BackupFolder inside the Pictures folder (i.e when I run the restore.sh script I get the BackupFolder inside the Pictures folder which, in turn, stores the actual pictures.)
Red Bow Tie
 
Posts: 4
Joined: Sun Mar 06, 2011 11:20 pm

Postby nelz » Tue Mar 08, 2011 3:41 pm

.. is the parent directory.

It sounds like you don't want the BackupFolder in the archive in the first place, go into it before creating the archive. You can do this with

Code: Select all
cd BackupFolder

before running tar, but it is cleaner to use the -C option with tar

[code]tar -C BackupFolder czf somebackup.tar.gz .[code]
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8490
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby ollie » Thu Mar 10, 2011 12:12 am

Got a bit of a stutter there nelz :P :lol:
User avatar
ollie
Moderator
 
Posts: 2749
Joined: Mon Jul 25, 2005 11:26 am
Location: Bathurst NSW Australia

Postby towy71 » Thu Mar 10, 2011 12:15 am

What are you talking about ollie ;-)
still looking for that door into summer
User avatar
towy71
Moderator
 
Posts: 4263
Joined: Wed Apr 06, 2005 2:11 pm
Location: wild West Wales

Postby ollie » Thu Mar 10, 2011 12:17 am

towy71 wrote:What are you talking about ollie ;-)


Thanks Towy - you deleted the wrong one :wink: :P
User avatar
ollie
Moderator
 
Posts: 2749
Joined: Mon Jul 25, 2005 11:26 am
Location: Bathurst NSW Australia

Postby Red Bow Tie » Thu Mar 10, 2011 12:44 am

Try as I might I cannot get the restore script to recognize an existing tarball. Again this is taken from LXF#126. Here is the compress.sh script
Code: Select all
 
#!/bin/sh
if [ -f $1/../backup.tar.gz ]
then
tar -uz --file=$1/../backup.tar.gz $1
elif [ -d $1 ]
then
tar -cz --file=$1/../backup.tar.gz $1
echo "Backup packaged"
else
echo "Backup packaging failed"
fi


I can create the tarball. $1 is the Backup folder I created under the home folder. My scripts are in a Shellscripting folder I created also under the home folder. Here is the code I used to run the compress.sh script contained within the Shellscripting folder.
Code: Select all
 sh compress.sh ../Backup
I used ../Backup as the Backup folder is in the parent of the Shellscripting folder. With the code above this creates a tarball in the home folder.
Here is the restore script
Code: Select all
 
#!/bin/sh
if [ -f $1/../backup.tar.gz ]
then
tar -xz --file=$1/../backup.tar.gz
rsync --recursive --times --perms $1 $2
rm -rf $1
echo "Restore successful"
elif [ -d $1 ]
then
rsync --recursive --times --perms $1 $2
tar -cz --file=$2/../backup.tar.gz $1
rm -rf $1
echo "Restore successful"
else
echo "Restore failed"
fi


Actually I corrected an out of place then statement (This was taken from LXF#126) My question is, if I am in my Shellscripting folder and run the restore.sh script, how do I get it to see the backup.tar.gz archive in the parent directory of my Shellscripting folder so that line 2 in the script above will carry out lines 4 to 7? I can restore successfully if I specify the Backup folder as $1. Then lines 8 to 12 do the job, but how can I use the archived tarball to restore? :cry:
Red Bow Tie
 
Posts: 4
Joined: Sun Mar 06, 2011 11:20 pm

Postby Dutch_Master » Thu Mar 10, 2011 2:45 am

LXF126 is quite a while ago, so I can't remember exactly, but IMO it is the intention of the script to specify the required backup to restore. You may have made more then one backup over a period of time, so which one should the script use to restore?
Dutch_Master
LXF regular
 
Posts: 2445
Joined: Tue Mar 27, 2007 1:49 am

Postby nelz » Thu Mar 10, 2011 9:19 am

If the tarball is given as the argument, you can get it's directory with dirname, e.g.

Code: Select all
cd $(dirname $1)
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8490
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests