blainarmstrong
blainarmstrong

Reputation: 1020

How can I send any error messages to a file when running Mysqldump?

I'm pretty new to using MySql from the command line, so I really need some advice here.

Basically, I've written a bash script that backs up my databases on selected days via a cron job. It's working just fine, but I would like to know if there is any way I can direct any error messages from mysqldump emailed to me in the off chance that there's something wrong. Here's the key part of the code that's doing the dump:

mysqldump -u user -h localhost --all-databases | gzip -9 > $filename

Is there any way to set up a condition that would capture any error messages and send them in an email?

Blain

Upvotes: 0

Views: 1911

Answers (2)

John Zwinck
John Zwinck

Reputation: 249163

You said you're using cron to run the job. Which is great, because cron already has a mail sending feature built in--no need for writing to a temporary file nor using support scripts. Just do this:

[email protected]
00 14 * * * mysqldump ... | gzip -9 >filename # or invoke a script here

And like magic, any output from the job will be mailed to you.

Upvotes: 0

Ahmed Masud
Ahmed Masud

Reputation: 22372

Use:

mysqldump -u user -h localhost --all-databases 2> error.log | gzip -9 > $filename 

In particular, in bash you can redirect any output descriptor to something else by using the n> syntax, notice the LACK of space between n and > :)

Email the error.log to yourself :)

Upvotes: 1

Related Questions