• Introducing WebPros Cloud - a fully managed infrastructure platform purpose-built to simplify the deployment of WebPros products !  WebPros Cloud enables you to easily deliver WebPros solutions — without the complexity of managing the infrastructure.
    Join the pilot program today!
  • Support for BIND DNS has been removed from Plesk for Windows due to security and maintenance risks.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS.

Help with a bash script?

L

lpittman

Guest
Help with a bash script to update .qmail files?

Hey,

I'm hoping someone can help me out - I'm trying to write a simple script to do the following:

- Read a list of mailnames (either /domain.ext/* or /domain.ext/specificaddress

- Loop through each and copy a .qmail file into each folder (if its a /* it'll go to every folder in there, otherwise it goes to the specific mailname)

The reason - I have a bunch of customers setup with a unique maildrop filter that gets called by the .qmail file. However every time I do any changes to Plesk or anything they get overwritten. So I need to be able to easily write them back ...

Thanks in advance.

Luke
 
Update!

Since I made the original post I've been reading tutorials, looking at examples and just fooling around and have actually got it to work. So, I thought I'd share it in case anyone wants to do anything similar.

I have 2 files:

mailnames - which contains a list of all the folders of mailnames, or specific mail names I want to copy my new .qmail file into. It looks like this:

Code:
mydomain1.com/*
mydomain2.net/*
mydomain3.ca/info

Notice the "*" to show that you want to do all the directories in that directory.

Next I have:

setup_new_mailnames.sh - this is the fun file, which looks like this:

Code:
#!/bin/bash

# Setup Log File
log=setup_mail_filters.log

if [ -f "$log" ]
then
  rm $log
fi

touch $log

# Grab the list of mailnames we want to work with
mailnames=`cat mailnames`

# Set the base directory of your mailnames
directory=/var/qmail/mailnames

for i in $mailnames
do

  ii="$directory/$i"

  if [[ "$i" =~ "\*" ]]
  then                                          # if $i contains a *, do the following
    iii=${ii%\*}

    if [ -d "$iii" ]
    then
      dir=`ls $iii`
      for dirs in $dir
      do
        if [ -d "$iii$dirs" ]
        then
          qmail=`cat $iii$dirs/.qmail | grep '&'`
          if [[ ! "$qmail" =~ "&" ]]
          then
            cp .qmail "$iii$dirs"
            echo ".qmail copied to $iii$dirs"
            echo ".qmail copied to $iii$dirs" >> $log
          else
            echo ".qmail IGNORED - $iii$dirs (Forward or Group)"
            echo ".qmail IGNORED - $iii$dirs (Forward or Group)" >> $log
          fi
        fi
      done

    else
      echo "Error: $iii is not a directory."
    fi

  else                                          # if $i is a maildir copy the .qmail file
    if [ -d "$ii" ]
    then
      cp .qmail $ii
      echo ".qmail copied to $ii"
      echo ".qmail copied to $ii" >> $log
    else
      echo "Error: $ii does not exist"
    fi
  fi
    fi
  fi

  echo "---"
  echo "---" >> $log
done

exit $?

Now the final thing you need is the new .qmail file you want to copy into these mailname directories... remember to "chown popuser:popuser .qmail" (or whatever your mail user is) or it won't work.

This script will look for a "&" in the .qmail files (only in the directories you specify as "*", it assumes the specific mailnames are mailboxes, not forwards or groups) and only overwrite the .qmail files that are for mailnames and not forwards or groups.

If any of you gurus out there can see any flaws in this, or how I could make it better, please let me know.

As this is my first script (and would be the same for any script) it comes with no warranty. Use at your own risk.

Hope this helps someone else out.

Luke
www.catch22media.com
 
Oh ... and in case you didn't notice, this will create a basic log file of what it did.

This is fun, bash scripts are handy as heck.

Luke
 
Back
Top