You are not logged in.

#1 2009-12-11 13:47:02

Carlwill
Member
From: Orlando, FL
Registered: 2008-10-06
Posts: 560
Website

Review My Script Please

I created an automated script that makes deleting someone from my email server a little easier. The script is very basic and just helps me do the following:

1 - back up their home directory.
2 - check to see if they're subscribed to any Mailman lists and removes them to avoid delivery failures.
3 - checks '/etc/aliases' for any entries.
4 - Removes the user and their home directory using the userdel -r "command"

It's very basic and I have never written a script before so please excuse me if I am doing anything wrong. Two things I would like to understand how to do in this script before I use it.

First is to somehow find a way to copy / backup their '/etc/passwd & '/etc/shadow' entry in case I ever need to restore their account for an unknown reason. Is this possible?

Second is I find out the hard way if I run the script on my mail server with no username following it:

 sh remove_user.sh

Rather than how it's intended:


 sh remove_user.sh carlos

The script will run and just start backing up the entire /home/* directory. If you did not happen to catch this process, you risk the script backing up the entire home directory and eventually deleting all accounts. Is there a way I can change the script that must require a username after the script name?

Below is the script I wrote:

#!/bin/bash

USERNAME=$1
HOMEDIR=/home/$USERNAME
BACKUPDIR=/var/backup
MAILMANDIR=/usr/lib/mailman
DATESTAMP=$(date +%m-%d-%Y_%H-%M)

if [ -d /home/$USERNAME ]; then
        echo "* Backing up home directory to $BACKUPDIR:"
        tar jcvf $BACKUPDIR/$USERNAME-$DATESTAMP.tar.bz2 /home/$USERNAME
        echo "--------------------------------------------------"
fi

echo "--------------------------------------------------"
echo "* Removing user from mailing lists:"
$MAILMANDIR/bin/remove_members --fromall $USERNAME@mydomain
echo "--------------------------------------------------"

echo "--------------------------------------------------"
echo "* Checking alias files for entries:"
ALIASFILES=$(ls /etc/*alias* | grep -v '\.db' | grep -v '\.orig')
for f in $ALIASFILES; do
        grep -H $USERNAME $f
done
echo "--------------------------------------------------"

echo "--------------------------------------------------"
echo "* Removing users home directory - /home/$USERNAME:"
userdel -r $USERNAME
echo "--------------------------------------------------"

Last edited by Carlwill (2009-12-11 14:04:25)


./

Offline

#2 2009-12-11 14:20:33

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Review My Script Please

this will sanity-check username:

USERNAME=$1

if [ -z "$USERNAME" ]; then
    echo "Usage: $0 <username>"
    exit 1
fi

Offline

#3 2009-12-11 14:23:03

Carlwill
Member
From: Orlando, FL
Registered: 2008-10-06
Posts: 560
Website

Re: Review My Script Please

Where exactly do I slip that piece of code into my script? What do you mean by sanity check?

USERNAME=$1

                                         <----------------------------------- Here?

HOMEDIR=/home/$USERNAME
BACKUPDIR=/var/backup
MAILMANDIR=/usr/lib/mailman
DATESTAMP=$(date +%m-%d-%Y_%H-%M)


./

Offline

#4 2009-12-11 21:08:39

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Review My Script Please

I would put it there, yes. It checks to see if the USERNAME variable is empty, which is what Cerebral means by sanity checking.

Offline

#5 2009-12-13 20:51:00

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Review My Script Please

Yeah, sorry for vagueness - it checks to ensure the USERNAME isn't empty.  I was going to do stuff like checking if the homedir already existed, but you took care of that anyway. wink

Offline

Board footer

Powered by FluxBB