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 backup@email.com
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.
using mailx command
using sendmail
the shortest way for me is
so for your example it'll be
the good part is that I can recall it with Ctrl+r to send another file...
Depending on your version of linux it may be called mail. To quote @David above:
or also:
Not a method for sending email, but you can use an online Git server (e.g. Bitbucket or a similar service) for that.
This way, you can use
git push
commands, and all versions will be stored in a compressed and organized way.Send a Plaintext body email with one plaintext attachment with mailx:
Below is the same command as above, without the newlines
Make sure you have a file
/home/el/attachfile.txt
defined with this contents:If you don't have uuencode read this: https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work
On Linux, Send HTML body email with a PDF attachment with sendmail:
Make sure you have ksh installed:
yum info ksh
Make sure you have sendmail installed and configured.
Make sure you have uuencode installed and available: https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work
Make a new file called
test.sh
and put it in your home directory:/home/el
Put the following code in
test.sh
:Change the export variables on the top of
test.sh
to reflect your address and filenames.Download a test pdf document and put it in
/home/el
called pdf-test.pdfMake a file called /home/el/email_body.htm and put this line in it:
Make sure the pdf file has sufficient 755 permissions.
Run the script
./test.sh
Check your email inbox, the text should be in HTML format and the pdf file automatically interpreted as a binary file. Take care not to use this function more than say 15 times in a day, even if you send the emails to yourself, spam filters in gmail can blacklist a domain spewing emails without giving you an option to let them through. And you'll find this no longer works, or it only lets through the attachment, or the email doesn't come through at all. If you have to do a lot of testing on this, spread them out over days or you'll be labelled a spammer and this function won't work any more.