Solved

script

Posted on 1998-12-14
9
310 Views
Last Modified: 2010-04-20
I am trying to backup my system..
I started out by trying to write a script like this:

***
#!/bin/sh

DIR_LIST=`cat /etc/backup.list`
LAST_BU_FILENAME=/etc/.backupfull
FILENAME=`date +%Y%m%d.incr.tgz`
PATHNAME=/home/backup/

find $DIR_LIST -newer $LAST_BU_FILENAME -type f -print > /tmp/filez.list

sed 's/ /\\ /g' /tmp/filez.list >/tmp/filez2.list
LIST=`cat /tmp/filez2.list`

#create the initial tar file.. then we'll append to this
tar cvf fred.tar /tmp/filez2.list

for FILETOARCHIVE in $LIST
do

tar rvf fred.tar $FILETOARCHIVE

done

***

This would work if it wasn't for windows weenies. Windows weenies use my linux system as a file server and hence have whitespace in their files (and directories)

But it isn't the tarring that's the problem since i can do this:

tar rvf fred.tar /home/fred/hello\ world/smeg.doc

and it works no problems..
so my problem is in the for loop declaration
it is giving the wrong names to the tar line

hence i need a way to send a complete line (spaces or no spaces) from $LIST to the for expression without it thinking I am specifying a new variable because of the space

Let me try and explain a bit better:

the filez2.list may look like this:
/home/tig/rhosts
/home/tig/hello\ world.doc

the for loop does this
sends /home/tig/rhosts to tar
sends /home/tig/hello to tar
sends world.doc to tar

which is wrong!
(i'm pretty sure what i say is correct)

the "for" expression (as soon as it hits a space) executes the loop.

basically what i need is this:
some way to do a find on my system for updated files (as above) and direct that output into tar

can i do something like this:

find $DIR_LIST -newer $LAST_BU_FILENAME -type f -print | tar rvf fred.tar -

??????

how can this problem be solved?

or can i somehow work around the for loop problem..
0
Comment
Question by:wehttam
  • 3
  • 3
  • 3
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638834
sed 's/ /</g' /tmp/filez.list >/tmp/filez2.list

for FILETOARCHIVE in `cat /tmp/filez2.lst`; do
   filename="`echo "\"$FILETOARCHIVE"\" | sed -e 's/</\ /g'`"
   tar rvf fred.tar $FILETOARCHIVE
done

0
 
LVL 1

Expert Comment

by:zblaxell
ID: 1638835
Wow...not only is it complicated, it's beyond repair...

    #!/bin/sh

    DIR_LIST=`cat /etc/backup.list`
    LAST_BU_FILENAME=/etc/.backupfull
    FILENAME=`date +%Y%m%d.incr.tgz`
    PATHNAME=/home/backup/

    echo -ne '/tmp/filez2.list\000' > /tmp/filez2.list
    find $DIR_LIST -newer $LAST_BU_FILENAME -type f -print0 >> /tmp/filez2.list
    cpio -ov0Htar < /tmp/filez2.list > fred.tar

This problem is much more appropriately solved in perl than a shell script.  Shell scripts have chronic character quoting problems (not to mention security problems) that make doing anything like this with a for loop in a shell a guaranteed disaster.  In addition to handling spaces correctly (which you aren't doing...you need "$VARIABLE" instead of $VARIABLE everywhere in your shell script), you have to handle newlines and \377's if you want to handle the entire Unix filename character set.
0
 

Author Comment

by:wehttam
ID: 1638836
Ahoffmann,
you solution failed to work. It did not successfully pick up files/directories with spaces.
I appreciate your help..

zblaxell,
your solution worked correctly.
please answer to receive your points


0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638837
as zblaxell said, anything else than [a-zA-Z0-9_+,.-;:_=%@]  in a shell  *may*  be  "a guaranteed disaster"  but *must not* be.
Probaly my solution just misses the quotes in the tar command
(as zblaxell said), sorry.
I also thaught of posting a solution which did tar all files at once, and not one by one (see zblaxell's comment), 'cause there aren't any prblems with any character in filenames as long as the name is not parsed by the shell itself.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:wehttam
ID: 1638838
>I also thaught of posting a solution which did tar all files at once, and not one by one
>(see zblaxell's comment), 'cause there aren't any prblems with any character in
>filenames as long as the name is not parsed by the shell itself.

I am aware that tar won't have a problem with this as initially i was using tar with the -N option to recursively pick up any new files in any directory. This worked fine except for the fact that tar's -N option is a very poor implementation since it seems to focus on directory dates instead of individual file dates. Hence I needed to use the 'find' command and direct it into tar. But this is not an option for tar if the file/directory has whitespace.. although later on i discovered tar can grab whitespace.. but the for loop was causing the problem.
But i explained all this in my question above.

Anyways, I appreciate your help.
0
 
LVL 1

Accepted Solution

by:
zblaxell earned 200 total points
ID: 1638839
"please answer to receive your points"

OK, here it is...
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1638840
ok, how about this (didn't chech tar options):

find $DIR_LIST -newer $LAST_BU_FILENAME -type f -exec tar rvf fred.tar {} \;

(it's a single line, probably wrapped by e-e).
0
 
LVL 1

Expert Comment

by:zblaxell
ID: 1638841
Or:

find $DIR_LIST -newer $LAST_BU_FILENAME -type f -print0 | xargs -0 tar rvf fred.tar
0
 

Author Comment

by:wehttam
ID: 1638842
thanks guys
i'll try em out

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change time in cron 4 67
Replace Ubuntu Desktop with Ubuntu Server 7 92
LINUX CENTOS + APACHE 9 65
Run DOS2UNIX and then execute the command 21 52
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now