LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-11-2009, 07:47 AM   #1
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Arch
Posts: 2,905

Rep: Reputation: 77
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:

Code:
 sh remove_user.sh
Rather than how it's intended:


Code:
 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:

Code:
#!/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 carlosinfl; 12-11-2009 at 08:04 AM.
 
Old 12-11-2009, 08:04 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
if [[ $# -ne 1]]; then
    echo "Usage: ${0##*/} username" >&2
    \exit 1
fi
Notes:
  • [[ ]] test preferred over [ ] test
  • >&2 sends output to stderr, conventional for error messages
  • \exit ensures the shell built-in exit is used not any alias of the same name
 
Old 12-11-2009, 09:15 AM   #3
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Arch
Posts: 2,905

Original Poster
Rep: Reputation: 77
Thanks for the clarification. Where would that code position into my script?
 
Old 12-11-2009, 09:40 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Where you want it to be effective, presumably before any other commands are executed.
 
Old 12-12-2009, 11:01 PM   #5
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
Quote:
Originally Posted by catkin View Post
Notes:
  • [[ ]] test preferred over [ ] test
  • >&2 sends output to stderr, conventional for error messages
  • \exit ensures the shell built-in exit is used not any alias of the same name
You are always amazing.. :-)
 
Old 12-13-2009, 04:13 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
this part
Quote:
Code:
ALIASFILES=$(ls /etc/*alias* | grep -v '\.db' | grep -v '\.orig')
for f in $ALIASFILES; do
        grep -H $USERNAME $f
done
you can do it with the shell
Code:
shopt -s extglob
for afile in /etc/*aliases*!(db|orig)/* 
do
  while read -r line
  do
     case "$line" in 
       *$USERNAME* ) echo "$afile: $line";;
     esac
  done < "$afile"
done
regarding the use of [[ vs [, its true that [[ has more features and "supposedly" better, BUT you can still use [ normally, quote your variables and do the necessary and you should be fine.

Last edited by ghostdog74; 12-13-2009 at 04:15 AM.
 
Old 12-13-2009, 01:14 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by WhisperiN View Post
You are always amazing.. :-)
I amaze myself sometimes!
 
Old 12-13-2009, 07:53 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For the passwd & shadow files, you can grep the entries by loginname (1st field in record) and output to a backup file eg

grep login /etc/passwd >login_passwd.bak
grep login /etc/shadow >>login_passwd.bak

You're going to have to remove the entries form the live files. If you do that by hand, you could just copy/paste the info anyway.
If you automate that, make sure the script does a backup first(!), then remove using eg sed.
Make sure you test this thoroughly(!) on a test system.

Last edited by chrism01; 12-13-2009 at 07:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Book Review Shell Script Pearls LXer Syndicated Linux News 0 09-04-2007 07:41 PM
Script Review ryanbrimmer Linux - Newbie 11 06-16-2007 06:17 PM
please review my startup script Serena *BSD 1 07-24-2006 09:54 AM
bash script: Review please carmstrong Linux - General 1 06-13-2004 09:43 AM
IPtables Script Review carmstrong Linux - Security 6 05-04-2004 12:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration