Monitoring / Troubleshooting

Email files from server (shell script)

by ,


We have by far the largest RPM repository with dynamic stable NGINX modules and VMODs for Varnish 4.1 and 6.0 LTS. If you want to install nginx, Varnish and lots of useful modules for them, this is your one stop repository to get all performance related software.
You have to maintain an active subscription in order to be able to use the repository!

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

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