You are not logged in.
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
this will sanity-check username:
USERNAME=$1
if [ -z "$USERNAME" ]; then
echo "Usage: $0 <username>"
exit 1
fi
Offline
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
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
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.
Offline