-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP_account_cleanup.ksh
76 lines (68 loc) · 2.85 KB
/
P_account_cleanup.ksh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/ksh
## Account cleanup script
## script handles only one account cleanup
### Belal Koura SSNC
## VERSION 5
#====================================================================================================
# VARs
pttrn=$1
lv_ptrrn="lv$1"
lv_name=$(lvdisplay -c|awk -F: -v x="$lv_ptrrn" '$1 ~ x {print $1}')
#====================================================================================================
if [[ $EUID -eq 0 && -z "$1" ]]; then
echo "[ERROR] Please run $0 <Account pattern> as root ... EXIT"
echo "[INFO] Usage $0 <pattern to be removed>"
exit 1
fi
echo "${lv_name}"
echo -n "Are you sure you want to remove the above logical volumes? (y/n)"
read confirmation
if [[ ! "$confirmation" =~ ^[Yy] ]]; then
echo "[INFO] Aborted by user."
exit 2
fi
#====================================================================================================
# Backup /etc/passwd with date and minutes
echo "[INFO] Backing up /etc/passwd"
cpdate /etc/passwd
# Removing entries containing '$pttrn' from /etc/passwd
echo "[INFO] Removing '$pttrn' entries from /etc/passwd"
awk -F: -v pattern="$pttrn" '$1 ~ pattern {print $1}' /etc/passwd|xargs -I {} echo User {} will be deleted ...
awk -F: -v pattern="$pttrn" '$1 ~ pattern {print $1}' /etc/passwd | xargs -I {} userdel {}
#sed -i "/^$pttrn/d" /etc/passwd
# Backup /etc/group with date and minutes
echo "[INFO] Backing up /etc/group"
cpdate /etc/group
# Removing entries containing '$pttrn' from /etc/group
echo "[INFO] Removing '$pttrn' entries from /etc/group"
sed -i "/^mqm:/s/$pttrn,//;/^mqm:/s/,$pttrn//" /etc/group
# Printing mqm group
echo "[INFO] Printing existing users under 'mqm' group "
grep mqm /etc/group
# Backup /etc/services with date and minutes
echo "[INFO] Backing up /etc/services"
cpdate /etc/services
# Removing entries containing '$pttrn' from /etc/services
echo "[INFO] Removing '$pttrn' entries from /etc/services"
sed -i "/^$pttrn/d" /etc/services
#====================================================================================================
# Check if /$pttrn is mounted and unmount it
echo "[INFO] Unmounting Logical Volume ${lv_name}"
df -h /$pttrn | grep $pttrn && echo "Unmounting /${pttrn}" && umount /$pttrn
# Removing Logical Volume /dev/mapper/vg_lnxm06_5-lv$pttrn
echo "[INFO] Removing Logical Volume/s ${lv_name}"
lvremove -f $lv_name
# Backup /etc/fstab with date and minutes
echo "[INFO] Backing up /etc/fstab"
cpdate /etc/fstab
echo "[INFO] Removing '$pttrn' entries from /etc/fstab"
sed -i "/$pttrn/d" /etc/fstab
systemctl daemon-reload
#====================================================================================================
# Check and remove /$pttrn directory
if [ -e "/$pttrn" ]; then
echo "[INFO] Listing and removing /${pttrn} directory"
ls -lsd /$pttrn*
find / -maxdepth 1 -name "${pttrn}*" -type l -exec rm -v {} \;
rm -rfv /$pttrn
fi