views:

6992

answers:

8

I've created a script that runs every night on my Linux server that uses mysqldump to back up each of my MySQL databases to .sql files and packages them together as a compressed .tar file. The next step I want to accomplish is to send that tar file through email to a remote email server for safekeeping. I've been able to send the raw script in the body an email by piping the backup text file to mailx like so:

$ cat mysqldbbackup.sql | mailx [email protected]

cat echoes the backup file's text which is piped into the mailx program with the recipient's email address passed as an argument.

While this accomplishes what I need, I think it could be one step better, Is there any way, using shell scripts or otherwise, to send the compressed .tar file to an outgoing email message as an attachment? This would beat having to deal with very long email messages which contain header data and often have word-wrapping issues etc.

+7  A: 

From looking at "man mailx", the mailx program does not have an option for attaching a file. You could use another program such as mutt.

echo "This is the message body" | mutt -a file.to.attach -s "subject of message" [email protected]

Command line options for mutt can be shown with "mutt -h".

Chris N
Thanks! That did the trick, I was having trouble getting mutt to do the action silently.
Kit Roed
+2  A: 

You can use mutt to send the email with attachment

mutt -s "Backup" -a mysqldbbackup.sql [email protected] < message.txt
David Schlosnagle
A: 

Depending on your version of linux it may be called mail. To quote @David above:

mail -s "Backup" -a mysqldbbackup.sql [email protected] < message.txt
Nathan Fellman
+3  A: 

Or, failing mutt:

gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB" [email protected]
Daniel Fone
A: 

metamail has the tool metasend

metasend -f mysqlbackup.sql.gz -t [email protected] -s Backup -m application/x-gzip -b
Gunstick
A: 

Hi!! I am able to use mutt succesfully to send email attachments. But,the subject is giving me problems. It is only taking the first word as the subject and ignoring the rest

My subject is something like "Sales Order Line", but the email is being received with the subject "Sales"

Any tip people

Are you enclosing the subject in double quotes?
Kit Roed
A: 

I use mpack.

mpack -s subject file [email protected]

Unfortunately mpack does not recognize '-' as an alias for stdin. But the following work, and can easily be wrapped in an (shell) alias or a script:

mpack -s subject /dev/stdin [email protected] < file
A: 

Hi

I was able to use mutt successfully with the attachment, thank you for the post

echo " message here" |mutt -a /path to file -s "subject" [email protected]

hytham