Link to home
Start Free TrialLog in
Avatar of antum
antum

asked on

gzip cron job help

Hello,

I am a web developer and want to run a series of cron jobs but I don't know where to find the information on how to do each particular one...

1. I want to gzip a series of directories /root/userfiles/images /root/userfiles/images2 etc... but i want to encode some sense of the root structure so in the final gzip file it contains /root/userfiles/images/image1.jpg instead of image1.jpg do you know what I mean?

2. Secondly I want that gzipped file to be ftp'd or emailed or somehow transferred to me and deleted off the server...

We could probably break down question 1 into 2 steps of how do i gzip a series of directories using cron and how do i preserve directory structure while only including files in certain sub-directories

Thanks very much
:Ant
SOLUTION
Avatar of JJSmith
JJSmith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooops - should be sendit.sh in the crontab entry...but you spotted that 'deliberate mistake'...Didn't you?
Obviously replace the tar line with the gzip line in the script if you prefer. I just happen to use tar for this type of thing. Youcould use bzip2 as well - feel free to play;)

man tar
man bzip2

etc

HTH:)
SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep - I'd agree with that:)

Always good to have someone check and improve things:) I'm going to have to work hard to improve the quality of my posts;)
Is '<"."' an attempt to make a "here" document? Look what it does:
07:47:47$ cat <"."
cat: -: Is a directory
bash connected the current directory to standard input - what you asked it to do but not what you want

Take 3:-

--------8X----------
#!/bin/sh
/bin/tar czf /tmp/tarredfile.tar.gz /root/userfiles/images /root/userfiles/images2 && \
/bin/mutt -a /tmp/tarredfile.tar.gz -s "Tarred files" me@mydomain.com <<EOF
.
EOF
[ $? -eq 0 ] && /bin/rm -Rf /tmp/tarredfile.tar.gz
--------8X----------
I see your point, but if you do it that way, surely you can:

--------8X----------
#!/bin/sh
/bin/tar czf /tmp/tarredfile.tar.gz /root/userfiles/images /root/userfiles/images2 && \
/bin/mutt -a /tmp/tarredfile.tar.gz -s "Tarred files" me@mydomain.com <"."
[ $? -eq 0 ] && /bin/rm -Rf /tmp/tarredfile.tar.gz
--------8X----------

Thought about this a bit more.

The meaning of "." depends on the context.

As you quite rightly said:

cat <"."

does try and cat the current directory, because cat expects a filename, and hence "." is assumed to be a filename.

. filename

executes a filename.

Under these circumstances (the use of mutt), I'd always considered the <"." to be perfectly valid(as it works), as I'd assumed that the . concerned was the 'finalising' of the email, which it effectively does....but if you were to change this to <"hello there\n". From a 'purists' perspective, I like your apporach more. Always good to have someone 'red penning' my work ;)

(   (()
(`-' _\
 ''  ''
Hi pjedmond - I think you must have been lucky with mutt - it would have been given a directory as its input and chose not to complain.
cat does not "expect a filename" - the man page documents that in the absence of an argument, it will read from standard input. The construct '<"."' tells the shell (bash, say) to connect "." (the current directory) to the standard input of the program you told it to run (cat or mutt). It seems that mutt chooses to treat the error as EOF (it could just be treating a -1 return from fgets as EOF, I haven't checked).
20:20:27$ cat <"hello there\n"
-bash: hello there\n: No such file or directory
Notice that error comes from *bash* - cat or mutt never gets started. "." is a valid existing filesystem entity, albeit a directory, so bash doesn't complain.
cat does not "expect a filename" - Ok - I'll expand - If given a name it assumes it to be a filename.

I was wondering if this works due to the first 2 'files' in the current directory are:

.
..

Based on the fact that a '.' on its own terminates the email message when telnetting to an SMTP server, hence the email is completed by the first '.' on it's own. However, as you've suggested, it appears that the error produced *really* is interpreted as the EOT! EEeeeek scary - guaranteed to break unexpectedly during an upgrade! I've tweaked the code on a few of my customers to correct (even though I've used this hack for years! I also accept that it is a horrible hack, and I also admit having used it for years on the basis that it it just 'worked'.

How about going for the more conventional then:

/bin/mutt -a /tmp/tarredfile.tar.gz -s "Tarred files" me@mydomain.com < /home/me/messagefile

where messagefile is:

-------8X-------------------
Here are your tarred files
-------8X-------------------

Wow - best question I've participated in this month!

(   (()
(`-' _\
 ''  ''


mutt -a ~/list.txt -s "Tarred files" me@cb.ws < /dev/null

also works:) ......

(   (()
(`-' _\
 ''  ''

</dev/null is fine. Gives an immediate EOF (and *no* error ;)
Avatar of antum
antum

ASKER

Thankyou all very much for your help!