-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup_quotas.sh
executable file
·266 lines (219 loc) · 7.57 KB
/
backup_quotas.sh
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
# highlights color
Red='\e[0;31m'
Green='\e[0;32m'
Cyan='\e[0;36m'
Yellow='\e[0;33m'
NC='\e[0m'
function enterquota()
{
# enter file quota
flagEnterQuota=0
while [[ $flagEnterQuota != 1 ]]; do
read -ep "Please enter a quota for user (in Gb):" NEWQUOTA
case ${NEWQUOTA} in
[0-9]* )
echo -e "Quota for ${Green} ${NEWUSER} ${NC} is ${Green} ${NEWQUOTA} ${NC} Gb"
flagEnterQuota=1
let "UPDQUOTA = ${NEWQUOTA} * 1024 * 1024"
;;
*)
echo -e "Quota for user ${Yellow} $NEWUSER ${NC} is not correct, this value have to contain only digits. Please choose new one"
;;
esac
done
# approve changes to the system
#setquota -u -F vfsv0 ${NEWUSER} ${NEWQUOTA}000000 ${NEWQUOTA}000000 0 0 /home/
setquota -u -F vfsv0 ${NEWUSER} ${UPDQUOTA} ${UPDQUOTA} 0 0 /home/
}
function createuser()
{
# enter username
flagEnterUser=0
while [[ $flagEnterUser != 1 ]]; do
IFEXISTS=0
read -p "Please enter the name of backup user:" NEWUSER
#IFEXISTS=$(cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}' |grep $NEWUSER| wc -l)
# check user to exists in /etc/passwd
for line in $(cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}' |grep $NEWUSER); do
if [[ $line == $NEWUSER ]]; then
IFEXISTS=1
fi
done
if [[ $IFEXISTS == 1 ]]; then
echo -e "User ${Yellow} $NEWUSER ${NC} is already exists in /etc/passwd. Please choose another name"
continue
fi
echo -e "new user is ${Green} $NEWUSER ${NC}"
flagEnterUser=1
done
#add home directory
flagEnterHome=0
while [[ $flagEnterHome != 1 ]]; do
read -p "Please enter a name of container for user (mvs*) or input NO if this is dedicated server:" NAMECONTAINER
if [[ ${NAMECONTAINER} == mvs* ]]; then
NEWHOMEFOLDER="/home/backup/${NAMECONTAINER}/$NEWUSER"
echo -e "home directory for user is ${Green} $NEWHOMEFOLDER ${NC}"
flagEnterHome=1
elif [[ ${NAMECONTAINER} == NO ]]; then
NEWHOMEFOLDER="/home/backup/$NEWUSER"
echo -e "home directory for user is ${Green} $NEWHOMEFOLDER ${NC}"
flagEnterHome=1
else
echo -e "Name of container ${Yellow} ${NAMECONTAINER} ${NC} is not correct. Please choose new one"
fi
#read -p "Please enter a home directory for user (/home/backup/*):" NEWHOMEFOLDER
# if [[ $NEWHOMEFOLDER == /home/backup/* ]]; then
# echo -e "home directory is user is ${Green} $NEWHOMEFOLDER ${NC}"
# flagEnterHome=1
# else
# echo -e "Folder ${Yellow} $NEWHOMEFOLDER ${NC} is not correct. Please choose new one"
# fi
done
# enter password
read -p "Please enter a password for user:" NEWPASSWD
# approve changes to the system
#useradd -s /bin/bash -p $(openssl passwd -1 ${NEWPASSWD}) -d ${NEWHOMEFOLDER} ${NEWUSER}
useradd -s /bin/bash -d ${NEWHOMEFOLDER} ${NEWUSER}
echo ${NEWUSER}:${NEWPASSWD} | chpasswd
mkdir -p ${NEWHOMEFOLDER}
chown ${NEWUSER}:${NEWUSER} ${NEWHOMEFOLDER}
enterquota
}
function updateuser()
{
echo 'Please find a listing of users below:'
cat /etc/passwd| grep mvs
flagUpdateUser=0
while [[ $flagUpdateUser != 1 ]]; do
#cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}'
IFEXISTS=0
read -p "Please enter the name of user:" NEWUSER
# check user to exists in /etc/passwd
for line in $(cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}' |grep $NEWUSER); do
if [[ $line == $NEWUSER ]]; then
IFEXISTS=1
fi
done
if [[ $IFEXISTS != 1 ]]; then
echo -e "User ${Yellow} $NEWUSER ${NC} is not exists in /etc/passwd. Please choose another name"
continue
fi
echo -e "update user is ${Green} $NEWUSER ${NC}"
flagUpdateUser=1
done
# select new container
read -p "Please enter the name of new container for user(like mvs1):" UPDCONTAINER
# check that new home directory is exists
if [ ! -d /home/backup/${UPDCONTAINER}/${NEWUSER} ]; then
mkdir -p /home/backup/${UPDCONTAINER}/${NEWUSER}
chown -R ${NEWUSER}:${NEWUSER} /home/backup/${UPDCONTAINER}/${NEWUSER}
fi
# move content to the new folder
OLDHOME=$(cat /etc/passwd | grep ${NEWUSER} | awk -F'': {'print $6'})
mv ${OLDHOME} /home/backup/${UPDCONTAINER}/
# change home directory for user
usermod -d /home/backup/${UPDCONTAINER}/${NEWUSER} ${NEWUSER}
echo -e "The home directory for user ${Yellow} ${NEWUSER} ${NC} has been changed to ${Green} /home/backup/${UPDCONTAINER}/${NEWUSER} ${NC}"
}
function updatequota()
{
echo 'Please find a listing of users below:'
repquota -us /home
flagUpdateUser=0
while [[ $flagUpdateUser != 1 ]]; do
IFEXISTS=0
read -p "Please enter the name of user:" NEWUSER
#IFEXISTS=$(cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}' |grep $NEWUSER| wc -l)
# check user to exists in /etc/passwd
for line in $(cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}' |grep $NEWUSER); do
if [[ $line == $NEWUSER ]]; then
IFEXISTS=1
fi
done
if [[ $IFEXISTS != 1 ]]; then
echo -e "User ${Yellow} $NEWUSER ${NC} is not exists in /etc/passwd. Please choose another name"
continue
fi
echo -e "update user is ${Green} $NEWUSER ${NC}"
flagUpdateUser=1
done
enterquota
}
function deleteuser()
{
# delete user
flagSelectUser=0
repquota -us /home
while [[ $flagSelectUser != 1 ]]; do
read -p "Please select user to remove:" DELETEUSER
for line in $(cat /etc/passwd | awk 'BEGIN{FS=":"}{print $1}' |grep ${DELETEUSER}); do
if [[ $line == ${DELETEUSER} ]]; then
IFEXISTS=1
fi
done
if [[ $IFEXISTS != 1 ]]; then
echo -e "User ${Yellow} ${DELETEUSER} ${NC} is not exists in /etc/passwd. Please choose another name"
continue
fi
while true; do
read -p "Do you want to remove user with home directory?:(Y/n)" options
case "${options}" in
Y | y | yes)
userdel -r ${DELETEUSER}
echo -e "User ${Yellow} ${DELETEUSER} ${NC} and home directory has been succesfully removed from system"; break
;;
N | n | no)
userdel ${DELETEUSER}
echo -e "User ${Yellow} ${DELETEUSER} ${NC} has been succesfully removed from system"; break
;;
*)
echo "Option is not correct. Please answer 'yes' or 'no'"
esac
done
exit
done
}
# main functionality
echo ""
echo "Please select operations with backup users."
echo -en "${Cyan}"
cat << 'EOF'
Create user (C)
Delete user (D)
Update user (U)
Update quota (Q)
Show limits for users(repquota -us /home) (S)
EOF
echo -en "${NC}"
#read -p "Please select operations with backup users. Create user(C) Delete user(D) Update user(U) Update quota(Q) Show limits for users(S):" opts
read -p "Your choose is: " opts
case "${opts}" in
C | c)
echo 'Create user'
createuser
exit
;;
D | d)
echo 'Delete user'
deleteuser
exit
;;
U | u)
echo 'Update user'
updateuser
exit
;;
Q | q)
echo 'Update quota'
updatequota
exit
;;
S | s)
repquota -us /home
exit
;;
*)
echo "Option is not correct. Please make your choose"
;;
esac