fbpx

Monitoring / Troubleshooting

Email files from server (shell script)

by ,


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

I have compiled a quick script that allows you to send files from a server in zip format. It enables you to email files (or even whole directories) as opposed to downloading them via SFTP or other means.

Pre-requisites: yum install mutt zip

Put the script below under /usr/local/bin/mail-it.sh

Email any file or directory from Linux

Now you can invoke it like this:

mail-it.sh filename email@example.com

You will get email with ZIP file attachment of the file or directory specified. Fast and convenient!
You can also provide third argument to add some descriptive text to your mail message:

mail-it.sh filename email@example.com "Here is the file you requested!"

mail-it.sh

#!/bin/bash
# requires: yum install zip mutt
# invoke: mail-it.sh filename email@example

if [ -z "$1" ]
then
    	echo " is required"
        exit 1
fi

if [ -z "$2" ]
then
    	echo " is required"
        exit 1
fi

file="$1"
to="$2"
msg="$3"

TFILE="/tmp/$(basename $file).zip"
rm -rf $TFILE

zip -jr $TFILE $file
echo "$msg" | mutt -a "$TFILE" -s "ZIP of $file" -- $to
rm -rf $TFILE

echo "Mail sent."

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.