| View previous topic :: View next topic |
| Author |
Message |
Red Bow Tie
Joined: Sun Mar 06, 2011 11:20 pm Posts: 4
|
Posted: Tue Mar 08, 2011 3:19 am Post subject: error in restore code |
|
|
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 |
|
 |
Dutch_Master LXF regular
Joined: Tue Mar 27, 2007 2:49 am Posts: 2358
|
Posted: Tue Mar 08, 2011 3:48 am Post subject: |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8036 Location: Warrington, UK
|
Posted: Tue Mar 08, 2011 9:28 am Post subject: |
|
|
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 |
|
 |
Red Bow Tie
Joined: Sun Mar 06, 2011 11:20 pm Posts: 4
|
Posted: Tue Mar 08, 2011 3:10 pm Post subject: error in restore code |
|
|
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 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.) |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8036 Location: Warrington, UK
|
Posted: Tue Mar 08, 2011 3:41 pm Post subject: |
|
|
.. 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
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 |
|
 |
ollie Moderator

Joined: Mon Jul 25, 2005 12:26 pm Posts: 2749 Location: Bathurst NSW Australia
|
Posted: Thu Mar 10, 2011 12:12 am Post subject: |
|
|
Got a bit of a stutter there nelz  |
|
| Back to top |
|
 |
towy71 Moderator

Joined: Wed Apr 06, 2005 3:11 pm Posts: 4176 Location: wild West Wales
|
Posted: Thu Mar 10, 2011 12:15 am Post subject: |
|
|
What are you talking about ollie  _________________ still looking for that door into summer |
|
| Back to top |
|
 |
ollie Moderator

Joined: Mon Jul 25, 2005 12:26 pm Posts: 2749 Location: Bathurst NSW Australia
|
Posted: Thu Mar 10, 2011 12:17 am Post subject: |
|
|
| towy71 wrote: | What are you talking about ollie  |
Thanks Towy - you deleted the wrong one  |
|
| Back to top |
|
 |
Red Bow Tie
Joined: Sun Mar 06, 2011 11:20 pm Posts: 4
|
Posted: Thu Mar 10, 2011 12:44 am Post subject: |
|
|
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?  |
|
| Back to top |
|
 |
Dutch_Master LXF regular
Joined: Tue Mar 27, 2007 2:49 am Posts: 2358
|
Posted: Thu Mar 10, 2011 2:45 am Post subject: |
|
|
| 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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8036 Location: Warrington, UK
|
Posted: Thu Mar 10, 2011 9:19 am Post subject: |
|
|
If the tarball is given as the argument, you can get it's directory with dirname, e.g.
_________________ "Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein) |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|