Hi, I want to mail a file to someone from a cronjob. How would I do that? It's a cronjob starting every day at 00:00, creating a backup file. After the backup, I want to mail the created file to myself (on another host). TIA, Rogier +---------------------------------+ | Rogier Maas | | icarus@guldennet.nl | | http://www.guldennet.nl~icarus/ | | ICQ# 2403780 | +---------------------------------+ -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
I want to mail a file to someone from a cronjob. How would I do that?
It's a cronjob starting every day at 00:00, creating a backup file. After the backup, I want to mail the created file to myself (on another host).
I've got a couple of similar things running at work. Assuming the file is in /home/rogier and it goes to you at 01:01am, try this: 01 01 * * 1 root cat /home/rogier/backup | sendmail -f rogier@host.whatever icarus@guldernet.nl Simon * Simon Chapman * simon@dylan.org * ICQ# 26523352 "Fear leads to anger. Anger leads to hate. Hate leads to using Windows NT for mission-critical applications." - What Yoda *meant* to say -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Simon Chapman wrote:
I want to mail a file to someone from a cronjob. How would I do that?
It's a cronjob starting every day at 00:00, creating a backup file. After the backup, I want to mail the created file to myself (on another host).
I've got a couple of similar things running at work. Assuming the file is in /home/rogier and it goes to you at 01:01am, try this:
01 01 * * 1 root cat /home/rogier/backup | sendmail -f rogier@host.whatever icarus@guldernet.nl
What does the command 'root' do? (root cat /home/rogier/backup) Currently, I have this in my crontab: 0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null I could make that into: 0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null; cat backup | sendmail -f backup@host icarus@guldennet.nl If I cat the backupfile, wouldn't that leave me with a mail full of rubbish instead of a mail with the file as an attachment? Rogier -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
What does the command 'root' do? (root cat /home/rogier/backup)
it runs the command as root - I was assuming that the command goes in your root crontab, which of course it may not. Sorry about that. :-)
Currently, I have this in my crontab:
0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null
I could make that into:
0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null; cat backup | sendmail -f backup@host icarus@guldennet.nl
If I cat the backupfile, wouldn't that leave me with a mail full of rubbish instead of a mail with the file as an attachment?
Well, yeah, I guess it would. I'm so used to mailing myself (or other people) plain text files that I didn't consider that. Perhaps I'll just go away and not give people dumbass advice :-) Simon -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Simon Chapman wrote:
What does the command 'root' do? (root cat /home/rogier/backup)
it runs the command as root - I was assuming that the command goes in your root crontab, which of course it may not. Sorry about that. :-)
Currently, I have this in my crontab:
0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null
I could make that into:
0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null; cat backup | sendmail -f backup@host icarus@guldennet.nl
If I cat the backupfile, wouldn't that leave me with a mail full of rubbish instead of a mail with the file as an attachment?
Well, yeah, I guess it would. I'm so used to mailing myself (or other people) plain text files that I didn't consider that. Perhaps I'll just go away and not give people dumbass advice :-)
Oh hehe.. Don't go away now, you've just learnt me something new about executing cronjobs! ;-D I guess the right question would be: How can I mail a binary file as an attachment along with my mail-body? When I do: echo "Hello, here is your binary file" | mail icarus@guldennet.nl -s "Binary file" But this doesn't get me the binary file I've been waiting for all week. I could just do: cat file | mail icarus@guldennet.nl -s "Binary file" But this would get me rubbish. I just cannot send a binary file in this manner. I'm working on a script that can send MIME-encoded messages with binary files as attachments. It is finished, but to one thing: When I have a complete mailfile, sendmail -i user@domain.com < script gives me an attachment, but that one is empty. I haven't got a clue how I can insert binaries into that stupid (sorry) sendmail thingy. Am I re-inventing the wheel here? Rogier -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Hi Rogier, Rogier Maas wrote:
Simon Chapman wrote:
What does the command 'root' do? (root cat /home/rogier/backup)
it runs the command as root - I was assuming that the command goes in your root crontab, which of course it may not. Sorry about that. :-)
Currently, I have this in my crontab:
0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null
I could make that into:
0 0 * * * /home/rogier/backup >/dev/null 2>/dev/null; cat backup | sendmail -f backup@host icarus@guldennet.nl
If I cat the backupfile, wouldn't that leave me with a mail full of rubbish instead of a mail with the file as an attachment?
Well, yeah, I guess it would. I'm so used to mailing myself (or other people) plain text files that I didn't consider that. Perhaps I'll just go away and not give people dumbass advice :-)
Oh hehe.. Don't go away now, you've just learnt me something new about executing cronjobs! ;-D I guess the right question would be: How can I mail a binary file as an attachment along with my mail-body?
When I do:
echo "Hello, here is your binary file" | mail icarus@guldennet.nl -s "Binary file"
But this doesn't get me the binary file I've been waiting for all week. I could just do:
cat file | mail icarus@guldennet.nl -s "Binary file"
But this would get me rubbish. I just cannot send a binary file in this manner. I'm working on a script that can send MIME-encoded messages with binary files as attachments. It is finished, but to one thing: When I have a complete mailfile, sendmail -i user@domain.com < script gives me an attachment, but that one is empty. I haven't got a clue how I can insert binaries into that stupid (sorry) sendmail thingy.
Am I re-inventing the wheel here?
Hmmm. I don't know whether you missed this message or not, but Stefan Troeger wrote the following: ---start--- Hi, On Mon, Feb 21, 2000 at 00:23 +0100, Rogier Maas wrote:
I want to mail a file to someone from a cronjob. How would I do that?
mutt -a <file> <recipient> http://www.suse.com/Support/Doku/FAQ/
On Mon, 21 Feb 2000, Rogier Maas wrote:
But this doesn't get me the binary file I've been waiting for all week. I could just do:
cat file | mail icarus@guldennet.nl -s "Binary file"
But this would get me rubbish. I just cannot send a binary file in this manner. I'm working on a script that can send MIME-encoded messages with binary files as attachments. It is finished, but to one thing: When I have a complete mailfile, sendmail -i user@domain.com < script gives me an attachment, but that one is empty. I haven't got a clue how I can insert binaries into that stupid (sorry) sendmail thingy.
Am I re-inventing the wheel here?
What's wrong with doing: cat file | gzip -9 | uuencode | mail icarus@guldennet.nl -s "Binary file" Isn't this what makes the command line so glorious? :) -- Robert S. McMillan rmcmilla@ptinet.net "The right to privacy... is the most comprehensive of rights and the right most valued by civilized man" - Justice Louis Brandeis, US Supreme Court, 1928 -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
On Sun, 20 Feb 2000, Rogier Maas wrote:
Hi,
I want to mail a file to someone from a cronjob. How would I do that?
It's a cronjob starting every day at 00:00, creating a backup file. After the backup, I want to mail the created file to myself (on another host).
Ooh! I know this one! :) Have a look at my backup script for my one and only MySQL database (I'm particularly proud of this): #!/bin/sh # Script to create daily backups of the IRM database mysqldump -u root -ppassword irm > /tmp/irm-`date '+%y%m%d'`.dump # Makes an sql dump of the irm database in /tmp gzip /tmp/irm-`date '+%y%m%d'`.dump # Compresses the file IRM_BACKUP=irm-`date '+%y%m%d'`.dump.gz ; export IRM_BACKUP # Less typing down the road ;) mv /tmp/$IRM_BACKUP /home/jpennington # Moves the file to a remote location chown jpennington.users /home/jpennington/$IRM_BACKUP chmod 600 /home/jpennington/$IRM_BACKUP # Makes the file irmadmin's property echo "$IRM_BACKUP has been created" | mail jpennington -s $IRM_BACKUP # Notifies user that the cron job terminated successfully # EOF I'm also fishing for some suggestions concerning the script in general. I need to integrate some error-trapping with this, since my script will go merrily about it's business wether the file was created or not. Ideas? :) -- -=|JP|=- Jon Pennington | Atipa Linux Solutions -o) jpennington@atipa.com | Kansas City, MO /\\ 816-241-2641 x107 | http://www.atipa.com _\_V -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Hi, On Mon, Feb 21, 2000 at 10:24 -0600, Jon Pennington wrote:
Ooh! I know this one! :) Have a look at my backup script for my one and only MySQL database (I'm particularly proud of this):
#!/bin/sh # Script to create daily backups of the IRM database mysqldump -u root -ppassword irm > /tmp/irm-`date '+%y%m%d'`.dump # Makes an sql dump of the irm database in /tmp
gzip /tmp/irm-`date '+%y%m%d'`.dump # Compresses the file
IRM_BACKUP=irm-`date '+%y%m%d'`.dump.gz ; export IRM_BACKUP # Less typing down the road ;)
mv /tmp/$IRM_BACKUP /home/jpennington # Moves the file to a remote location
chown jpennington.users /home/jpennington/$IRM_BACKUP chmod 600 /home/jpennington/$IRM_BACKUP # Makes the file irmadmin's property
echo "$IRM_BACKUP has been created" | mail jpennington -s $IRM_BACKUP # Notifies user that the cron job terminated successfully # EOF
I'm also fishing for some suggestions concerning the script in general. I need to integrate some error-trapping with this, since my script will go merrily about it's business wether the file was created or not. Ideas? :)
You run date several times and expect it to return the same value every time. Think about what your script would do if it was started at 11:59.59 pm. Better store the current date in a varible and use that whenever you need it.
mysqldump -u root -ppassword irm > /tmp/irm-`date '+%y%m%d'`.dump
Before you do this, check if /tmp/irm-`date '+%y%m%d'`.dump already exists and if it is a symbolic link. Some malicious user could create a symbolic link with that name that points to an arbitrary file. When your backup script is invoked with root privileges, it will overwrite that file. The same for
gzip /tmp/irm-`date '+%y%m%d'`.dump
This won't overwrite /tmp/irm-`date '+%y%m%d'`.dump.gz but if this file already exists, gzip will probably ask you if it may overwrite it and wait forever.
IRM_BACKUP=irm-`date '+%y%m%d'`.dump.gz ; export IRM_BACKUP ^^^^^^^^^^^^^^^^^^^ That's not necessary.
echo "$IRM_BACKUP has been created" | mail jpennington -s $IRM_BACKUP
You'll receive this message even if your script failed to create a backup. To remedy this you might want to use something like dothis && dothat && anddothattoo && echo "done" |mail jpennington -s "done" || echo "failed" |mail jpennington -s "failed" Ciao, Stefan -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Hi, On Mon, 21 Feb 2000, Stefan Troeger wrote:
Before you do this, check if /tmp/irm-`date '+%y%m%d'`.dump already exists and if it is a symbolic link. Some malicious user could create a symbolic link with that name that points to an arbitrary file. When your backup script is invoked with root privileges, it will overwrite that file. The same for
gzip /tmp/irm-`date '+%y%m%d'`.dump
This won't overwrite /tmp/irm-`date '+%y%m%d'`.dump.gz but if this file already exists, gzip will probably ask you if it may overwrite it and wait forever.
Have a look at "mktemp(1)": NAME mktemp - make temporary file name (unique) SYNOPSIS mktemp [-d] [-q] [-u] template DESCRIPTION The mktemp utility takes the given file name template and overwrites a portion of it to create a file name. This file name is unique and suit able for use by the application. The template may be any file name with exactly six of `Xs' appended to it, for example /tmp/temp.XXXXXX. The trailing six `Xs' are replaced with the current process number and/or a unique letter combination. The number of unique file names mktemp can return are roughly 26 ** 6 combinations. Bye, LenZ -- ------------------------------------------------------------------ Lenz Grimmer SuSE GmbH mailto:grimmer@suse.de Schanzaeckerstr. 10 http://www.suse.de/~grimmer 90443 Nuernberg, Germany -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
On Mon, 21 Feb 2000, Stefan Troeger wrote:
dothis && dothat && anddothattoo && echo "done" |mail jpennington -s "done" || echo "failed" |mail jpennington -s "failed"
Okay, I've cleaned up my script quite a bit since this posting. The part that still eludes me is the usage of the || operator. Remember, I've never written a script in my life ;) #!/bin/sh # Script to create daily backups of the IRM database IRM_DATE=`date '+%y%m%d'` IRM_DUMP=/tmp/irm-$IRM_DATE.dump IRM_BACKUP=/tmp/irm-$IRM_DATE.dump.gz # Establishes values for variables used later. mysqldump -u irm -pqwerty69 irm > $IRM_DUMP && gzip $IRM_DUMP && # Creates the dump file and compresses it. mv $IRM_BACKUP /home/irmadmin && chown irmadmin.users /home/irmadmin/irm-$IRM_DATE.dump.gz && chmod 600 /home/irmadmin/irm-$IRM_DATE.dump.gz && # Makes the compressed dump file property of the irmadmin user echo "$IRM_BACKUP was successfully created." | mail irmadmin -s $IRM_BACKUP # Notifies irmadmin of (un)successful backup creation # EOF If a backup or a fake backup file exists (/tmp/irm-$IRM_DATE.dump.gz), gz asks if I want to overwrite it. Since this script is meant to be non-interactive, what would you do if you wanted to overwrite the file automatically? I still don't know how an error could be trapped and an alternate message sent; would you mind elaborating on that concept a bit? -- -=|JP|=- Jon Pennington | Atipa Linux Solutions -o) jpennington@atipa.com | Kansas City, MO /\\ 816-241-2641 x107 | http://www.atipa.com _\_V -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Hi, On Mon, 21 Feb 2000, Jon Pennington wrote:
Okay, I've cleaned up my script quite a bit since this posting. The part that still eludes me is the usage of the || operator. Remember, I've never written a script in my life ;)
Well, the || operator is very smart. It's an OR operator that can be used for checking, if an operation failes or not. <operation1> || <operation2> If operation1 succeeds, it will return TRUE, which means that this OR operation is TRUE and the second part will not be executed. If if fails (return value is FALSE), operation2 will be executed. It can be compared to the perl construct "do some action OR die". If action fails, the script will die. Does this make any sense? Bye, LenZ -- ------------------------------------------------------------------ Lenz Grimmer SuSE GmbH mailto:grimmer@suse.de Schanzaeckerstr. 10 http://www.suse.de/~grimmer 90443 Nuernberg, Germany -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
On Mon, 21 Feb 2000, Lenz Grimmer wrote:
Hi,
On Mon, 21 Feb 2000, Jon Pennington wrote:
Okay, I've cleaned up my script quite a bit since this posting. The part that still eludes me is the usage of the || operator. Remember, I've never written a script in my life ;)
Well, the || operator is very smart. It's an OR operator that can be used for checking, if an operation failes or not.
<operation1> || <operation2>
I understand what it does, I just don't know how to use it ;). Probably something like: gzip $IRM_DUMP || exit 1 But then what do I do (in a sh script) with the bad exit status? If I understand correctly, my script would simply die, and I would have no way of knowing how or why. How do I trap that bad exit to send an alternate mail message with a description of why it failed? -- -=|JP|=- Jon Pennington | Atipa Linux Solutions -o) jpennington@atipa.com | Kansas City, MO /\\ 816-241-2641 x107 | http://www.atipa.com _\_V -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Jon Pennington wrote:
On Mon, 21 Feb 2000, Lenz Grimmer wrote:
Hi,
On Mon, 21 Feb 2000, Jon Pennington wrote:
Okay, I've cleaned up my script quite a bit since this posting. The part that still eludes me is the usage of the || operator. Remember, I've never written a script in my life ;)
Well, the || operator is very smart. It's an OR operator that can be used for checking, if an operation failes or not.
<operation1> || <operation2>
I understand what it does, I just don't know how to use it ;).
Probably something like:
gzip $IRM_DUMP || exit 1
But then what do I do (in a sh script) with the bad exit status? If I understand correctly, my script would simply die, and I would have no way of knowing how or why. How do I trap that bad exit to send an alternate mail message with a description of why it failed?
AFAIK, that's quite difficult, especially for a script such as this. First
you'd have to get the exit code and then do an if or case to tell it what to do
for each exit code (to find out what the exit codes mean you'll probably have
to do a 'man gzip'). In pseudocode:
case (gzip $IRM_DUMP) in
0 then <mail with OK message or even do nothing>
1 then
Hi, On Mon, Feb 21, 2000 at 15:05 -0600, Jon Pennington wrote:
On Mon, 21 Feb 2000, Stefan Troeger wrote:
dothis && dothat && anddothattoo && echo "done" |mail jpennington -s "done" || echo "failed" |mail jpennington -s "failed"
Okay, I've cleaned up my script quite a bit since this posting. The part that still eludes me is the usage of the || operator. Remember, I've never written a script in my life ;) [...] If a backup or a fake backup file exists (/tmp/irm-$IRM_DATE.dump.gz), gz asks if I want to overwrite it. Since this script is meant to be non-interactive, what would you do if you wanted to overwrite the file automatically? I still don't know how an error could be trapped and an alternate message sent; would you mind elaborating on that concept a bit?
Regarding gzip, you should use the -f option so it will automatically owerwrite files. Error trapping works because every program returns an exit code when it exits. If that exit code is 0 everything went fine, if it's >0 an error occured. If you connect several programs with the && operator like this program1 && program2 && program3 program2 will only be executed if program1 exited Ok (i.e. with exit code 0) and program3 will only be started if program1 and program2 exited Ok. || works similar, but in program1 || program2 program2 will only be executed if program1 failed (exit code >0). If you use this in your script a `done'-mail is only sent if every program exited Ok. If an error occured somewhere you'll receive the `failed' message. Try this (true returns 0, false 1): [sttr]/home/sttr/1> true && true && true && echo Ok || echo Oops Ok [sttr]/home/sttr/1> false && true && true && echo Ok || echo Oops Oops Btw. `man bash' is a good reading ;-) Ciao, Stefan -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
So what you're saying is that my script in it's current iteration should work: #!/bin/sh # Script to create daily backups of the IRM database IRM_DATE=`date '+%y%m%d'` IRM_DUMP=/tmp/irm-$IRM_DATE.dump IRM_BACKUP=/tmp/irm-$IRM_DATE.dump.gz # Establishes values for variables used later. mysqldump -u irm -ppassword irm > $IRM_DUMP && gzip -f $IRM_DUMP && # Creates the dump file and compresses it. mv $IRM_BACKUP /home/irmadmin && chown irmadmin.users /home/irmadmin/irm-$IRM_DATE.dump.gz && chmod 600 /home/irmadmin/irm-$IRM_DATE.dump.gz && # Makes the compressed dump file property of the irmadmin user echo "$IRM_BACKUP was successfully created." | mail irmadmin -s $IRM_BACKUP || echo "$IRM_BACKUP was NOT successfully created." | mail irmadmin -s $IRM_BACKUP # Notifies irmadmin of (un)successful backup creation # EOF Thank you sincerely for all of your help (Stefan and LenZ alike). -- -=|JP|=- Jon Pennington | Atipa Linux Solutions -o) jpennington@atipa.com | Kansas City, MO /\\ 816-241-2641 x107 | http://www.atipa.com _\_V -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Hi, On Mon, Feb 21, 2000 at 16:20 -0600, Jon Pennington wrote:
So what you're saying is that my script in it's current iteration should work:
Yep. Ciao, Stefan -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Jon, If you want to trap the return code use $? imediately after the command is run in the script. eg commandx set x=$? x will now contain the return code of "commandx" Regards, Graham Smith ---------------------------------- -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
participants (8)
-
chris.reeves@iname.com
-
gqs@zip.com.au
-
grimmer@suse.de
-
icarus@guldennet.nl
-
jpennington@atipa.com
-
rmcmilla@ptinet.net
-
simon@dylan.org
-
stefan.troeger@wirtschaft.tu-chemnitz.de