Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

error in restore code

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Red Bow Tie



Joined: Sun Mar 06, 2011 11:20 pm
Posts: 4

PostPosted: Tue Mar 08, 2011 3:19 am    Post subject: error in restore code Reply with quote

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'
Back to top
View user's profile Send private message
Dutch_Master
LXF regular


Joined: Tue Mar 27, 2007 2:49 am
Posts: 2435

PostPosted: Tue Mar 08, 2011 3:48 am    Post subject: Reply with quote

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:
[code]code goes here[/code]
(but only one code tag, I used 2 to let you see it)
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8464
Location: Warrington, UK

PostPosted: Tue Mar 08, 2011 9:28 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
Red Bow Tie



Joined: Sun Mar 06, 2011 11:20 pm
Posts: 4

PostPosted: Tue Mar 08, 2011 3:10 pm    Post subject: error in restore code Reply with quote

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:
 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:
/../
refer to? I assume it's some kind of path statement Question
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.)
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8464
Location: Warrington, UK

PostPosted: Tue Mar 08, 2011 3:41 pm    Post subject: Reply with quote

.. 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:
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)
Back to top
View user's profile Send private message
ollie
Moderator


Joined: Mon Jul 25, 2005 12:26 pm
Posts: 2749
Location: Bathurst NSW Australia

PostPosted: Thu Mar 10, 2011 12:12 am    Post subject: Reply with quote

Got a bit of a stutter there nelz Razz Laughing
Back to top
View user's profile Send private message
towy71
Moderator


Joined: Wed Apr 06, 2005 3:11 pm
Posts: 4260
Location: wild West Wales

PostPosted: Thu Mar 10, 2011 12:15 am    Post subject: Reply with quote

What are you talking about ollie Wink
_________________
still looking for that door into summer
Back to top
View user's profile Send private message
ollie
Moderator


Joined: Mon Jul 25, 2005 12:26 pm
Posts: 2749
Location: Bathurst NSW Australia

PostPosted: Thu Mar 10, 2011 12:17 am    Post subject: Reply with quote

towy71 wrote:
What are you talking about ollie Wink


Thanks Towy - you deleted the wrong one Wink Razz
Back to top
View user's profile Send private message
Red Bow Tie



Joined: Sun Mar 06, 2011 11:20 pm
Posts: 4

PostPosted: Thu Mar 10, 2011 12:44 am    Post subject: Reply with quote

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:
 
#!/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:
 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:
 
#!/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? Crying or Very sad
Back to top
View user's profile Send private message
Dutch_Master
LXF regular


Joined: Tue Mar 27, 2007 2:49 am
Posts: 2435

PostPosted: Thu Mar 10, 2011 2:45 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8464
Location: Warrington, UK

PostPosted: Thu Mar 10, 2011 9:19 am    Post subject: Reply with quote

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

Code:
cd $(dirname $1)

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast