forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv_backup_user
More file actions
executable file
·519 lines (434 loc) · 14.2 KB
/
v_backup_user
File metadata and controls
executable file
·519 lines (434 loc) · 14.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/bin/bash
# info: backup system user with all its objects
# options: user
#
# The call is used for backing up user with all its domains and databases.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/db.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'user'
validate_format 'user'
is_system_enabled "$BACKUP_SYSTEM"
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_backup_enabled
la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
i=0
while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
echo "$(date "+%F %T") Load Average $la"
echo
sleep 60
if [ "$i" -ge "15" ]; then
echo "Error: LA is too high"
log_event "$E_LA" "$EVENT"
exit $E_LA
fi
(( ++i))
done
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Get current time
start_time=$(date '+%s')
# Creating temporary random directory
tmpdir=$(mktemp -p $BACKUP -d)
# Prinitng status
echo "$(date "+%F %T") System backup for user $user"
echo "TMPDIR is $tmpdir"
echo
# Addding backup and vesta version
echo "1.0" > $tmpdir/backup_version
echo "$VERSION" > $tmpdir/vesta_version
# Vesta
echo "-- VESTA --"
vst='yes'
mkdir $tmpdir/vesta
# Backingup vesta configs
echo -e "$(date "+%F %T") user.conf"
cp -r $USER_DATA/user.conf $tmpdir/vesta/
if [ -e "$USER_DATA/stats.log" ]; then
echo -e "$(date "+%F %T") stats.log"
cp -r $USER_DATA/stats.log $tmpdir/vesta/
fi
if [ -e "$USER_DATA/history.log" ]; then
echo -e "$(date "+%F %T") history.log"
cp -r $USER_DATA/history.log $tmpdir/vesta/
fi
if [ -e "$USER_DATA/backup.excludes" ]; then
echo -e "$(date "+%F %T") backup.excludes"
cp -r $USER_DATA/backup.excludes $tmpdir/vesta/
fi
echo
# PAM
echo "-- PAM --"
mkdir $tmpdir/pam
echo -e "$(date "+%F %T") passwd / shadow /group"
grep "^$user:" /etc/passwd > $tmpdir/pam/passwd
grep "^$user:" /etc/shadow > $tmpdir/pam/shadow
grep "^$user:" /etc/group > $tmpdir/pam/group
echo
# Checking excludes
OLD_IFS="$IFS"
IFS=$'\n'
if [ -e "$USER_DATA/backup.excludes" ]; then
echo "-- Excludes --"
for exclude in $(cat $USER_DATA/backup.excludes); do
echo -e "$exclude"
# Indirect variable references (a bit of black magic)
eval ${exclude%%=*}=${exclude#*=}
done
echo
fi
IFS="$OLD_IFS"
# WEB domains
if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ] && [ "$WEB" != '*' ]
then
echo "-- WEB --"
mkdir $tmpdir/web/
# Parsing unsuspeneded domains
conf="$USER_DATA/web.conf"
for domain in $(search_objects 'web' 'SUSPENDED' "*" 'DOMAIN'); do
check_exl=$(echo "$WEB"|grep -w $domain)
if [ -z "$check_exl" ]; then
web_list="$web_list $domain"
fi
done
web_list=$(echo "$web_list" | sed -e "s/ */\ /g" -e "s/^ //")
for domain in $web_list; do
echo -e "$(date "+%F %T") $domain"
mkdir -p $tmpdir/web/$domain/conf
mkdir -p $tmpdir/web/$domain/vesta
# Defining domain variables
domain_idn=$(idn -t --quiet -a "$domain")
get_domain_values 'web'
# Creating web.config
cd $tmpdir/web/$domain/
conf="$USER_DATA/web.conf"
grep "DOMAIN='$domain'" $conf > vesta/web.conf
# Apache config
if [ "$WEB_SYSTEM" = 'apache' ]; then
# Parsing httpd.conf
tpl_file="$WEBTPL/apache_$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/httpd.conf"
get_web_config_brds
sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf
# SSL check
if [ "$SSL" = 'yes' ]; then
tpl_file="$WEBTPL/apache_$TPL.stpl"
conf="$HOMEDIR/$user/conf/web/shttpd.conf"
get_web_config_brds
sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf
fi
fi
# Nginx config
if [ ! -z "$NGINX" ] ; then
tpl_file="$WEBTPL/ngingx_vhost_$NGINX.tpl"
conf="$HOMEDIR/$user/conf/web/nginx.conf"
get_web_config_brds
sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf
# SSL check
if [ "$SSL" = 'yes' ] ; then
tpl_file="$WEBTPL/ngingx_vhost_$NGINX.stpl"
conf="$HOMEDIR/$user/conf/web/snginx.conf"
get_web_config_brds
sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
fi
fi
# Suplemental configs
for sconfig in $(ls $HOMEDIR/$user/conf/web/|grep ".$domain.conf"); do
cp $HOMEDIR/$user/conf/web/$sconfig conf/
done
# SSL Certificates
if [ "$SSL" = 'yes' ] ; then
cp $HOMEDIR/$user/conf/web/ssl.$domain.* conf/
cp $USER_DATA/ssl/$domain.* vesta/
fi
# Packing data folders
cd $HOMEDIR/$user/web/$domain
domain_direcotries=$(ls |grep -v logs)
tar -cf $tmpdir/web/$domain/domain_data.tar $domain_direcotries
if [ ! -z "$BACKUP_GZIP" ]; then
gzip -$BACKUP_GZIP $tmpdir/web/$domain/domain_data.tar
fi
done
echo
fi
# DNS domains
if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ] && [ "$DNS" != '*' ]
then
echo "-- DNS --"
mkdir $tmpdir/dns/
# Parsing unsuspeneded domains
for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
check_exl=$(echo "$DNS"|grep -w $domain)
if [ -z "$check_exl" ]; then
dns_list="$dns_list $domain"
fi
done
dns_list=$(echo "$dns_list" | sed -e "s/ */\ /g" -e "s/^ //")
for domain in $dns_list; do
echo -e "$(date "+%F %T") $domain"
# Building directory tree
mkdir -p $tmpdir/dns/$domain/conf
mkdir -p $tmpdir/dns/$domain/vesta
# Creating dns_domains config
cd $tmpdir/dns/$domain/
conf="$USER_DATA/dns.conf"
grep "DOMAIN='$domain'" $conf > vesta/dns.conf
# Backingup dns recods
cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
done
echo
fi
# Mail domains
if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL_SYSTEM" != 'no' ] && [ "$MAIL" != '*' ]
then
echo "-- MAIL --"
mkdir $tmpdir/mail/
# Parsing unsuspeneded domains
conf="$USER_DATA/mail.conf"
for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
check_exl=$(echo "$MAIL"|grep -w $domain)
if [ -z "$check_exl" ]; then
mail_list="$mail_list $domain"
fi
done
mail_list=$(echo "$mail_list" | sed -e "s/ */\ /g" -e "s/^ //")
for domain in $mail_list; do
echo -e "$(date "+%F %T") $domain"
#mkdir -p $tmpdir/mail/$domain/accounts
mkdir -p $tmpdir/mail/$domain/conf
mkdir -p $tmpdir/mail/$domain/vesta
# Creating exim config
cd $tmpdir/mail/$domain/
cp $HOMEDIR/$user/conf/mail/$domain/* conf/
# Creating vesta config
conf="$USER_DATA/mail.conf"
grep "DOMAIN='$domain'" $conf > vesta/mail.conf
cp $USER_DATA/mail/$domain.* vesta/
if [ ! -z "$(ls $USER_DATA/mail/|grep *@$domain)" ]; then
cp $USER_DATA/mail/*@$domain.* vesta/
fi
# Packing mailboxes
cd $HOMEDIR/$user/mail/$domain
accounts=$(ls)
if [ ! -z "$accounts" ]; then
tar -cf $tmpdir/mail/$domain/accounts.tar $accounts
fi
if [ ! -z "$BACKUP_GZIP" ] && [ ! -z $accounts ]; then
gzip -$BACKUP_GZIP $tmpdir/mail/$domain/accounts.tar
fi
done
echo
fi
# DatbaBases
if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
echo "-- DB --"
mkdir $tmpdir/db/
# Parsing unsuspeneded domains
for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
check_exl=$(echo "$DB"|grep -w $database)
if [ -z "$check_exl" ]; then
db_list="$db_list $database"
fi
done
db_list=$(echo "$db_list" | sed -e "s/ */\ /g" -e "s/^ //")
for database in $db_list; do
mkdir -p $tmpdir/db/$database/conf
mkdir -p $tmpdir/db/$database/vesta
cd $tmpdir/db/$database/
conf="$USER_DATA/db.conf"
grep "DB='$database'" $conf > vesta/db.conf
get_database_values
dump="$tmpdir/db/$database/$database.$TYPE.sql"
grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
echo -e "$(date "+%F %T") $database $TYPE"
case $TYPE in
mysql) dump_mysql_database ;;
pgsql) dump_pgsql_database ;;
esac
if [ ! -z "$BACKUP_GZIP" ]; then
gzip -$BACKUP_GZIP $dump
fi
done
echo
fi
# Cron jobs
if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON_SYSTEM" != 'no' ] && [ "$CRON" != '*' ]
then
echo "-- CRON --"
mkdir $tmpdir/cron/
# Backingup cron recods
echo -e "$(date "+%F %T") cron.conf"
cp $USER_DATA/cron.conf $tmpdir/cron/
echo -e "$(date "+%F %T") system cron"
if [ -e "/var/spool/cron/$user" ]; then
cron_list='yes'
cp /var/spool/cron/$user $tmpdir/cron/
fi
echo
fi
# Get backup size
size="$(du -shm $tmpdir | cut -f 1)"
# Get current time
end_time=$(date '+%s')
DATE=$(date +%F)
TIME=$(date +%T)
# Defining local storage function
local_backup(){
echo "-- STORAGE --"
echo -e "$(date "+%F %T") ARCHIVE $BACKUP/$user.$DATE.tar"
# Removing dublicate for this day
if [ -e "$BACKUP/$user.$DATE.tar" ]; then
deprecated="$DATE"
echo -e "Deleting old backup for $DATE"
rm -f $BACKUP/$user.$DATE.tar
fi
# Checking retention
backup_list=$(ls -lrt $BACKUP/ | awk '{print $9}' |grep "^$user\.")
backups_count=$(echo "$backup_list" | wc -l)
if [ "$BACKUPS" -le "$backups_count" ]; then
backups_rm_number=$((backups_count - BACKUPS))
(( ++backups_rm_number))
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
# Removing old backup
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
deprecated="$deprecated $backup_date"
echo -e "\tDeleteing old backup for $backup_date"
rm -f $BACKUP/$backup
done
fi
# Checking disk space
disk_usage=$(df $BACKUP | awk '{print $5}'|tail -n1|cut -f 1 -d '%')
if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
echo "Error: Not enough disk space"
log_event "$E_DISK" "$EVENT"
exit $E_DISK
fi
# Creating final tarball
cd $tmpdir
tar -cf $BACKUP/$user.$DATE.tar .
chmod 640 $BACKUP/$user.$DATE.tar
chown nginx:$user $BACKUP/$user.$DATE.tar
localbackup='yes'
echo
}
# Defining ftp command function
ftpc() {
ftp -n $HOST $PORT <<EOF
quote USER $USERNAME
quote PASS $PASSWORD
binary
cd $BPATH
$1
quit
EOF
}
# Defining ftp storage function
ftp_backup(){
echo "-- FTP --"
# Checking config
source $VESTA/conf/ftp.backup.conf
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] ||\
[ -z "$BPATH" ]; then
echo "Error: Parsing error"
log_event "$E_PARSING" "$EVENT"
exit $E_PARSING
fi
# Debug info
echo -ne "$(date "+%F %T") "
echo -e "ftp://$USERNAME@$HOST$BPATH/$user.$DATE.tar"
# Checking ftp permission
ftmpdir=$(mktemp -u -p $BPATH)
command="mkdir $ftmpdir
ls $ftmpdir
rm $ftmpdir"
if [ ! -z "$(ftpc "$command")" ] ; then
echo "Error: FTP error"
log_event "$E_FTP" "$EVENT"
exit $E_FTP
fi
# Checking retention
backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
backups_count=$(echo "$backup_list" | wc -l)
if [ "$backups_count" -ge "$BACKUPS" ]; then
backups_rm_number=$((backups_count - BACKUPS + 1))
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
deprecated="$deprecated $backup"
echo -e "\tDeleteing old backup for $backup_date"
ftpc "delete $backup"
done
fi
# Uploading backup archive
if [ "$localbackup" = 'yes' ]; then
cd $BACKUP
ftpc "put $user.$DATE.tar"
else
cd $tmpdir
tar -cf $BACKUP/$user.$DATE.tar .
cd $BACKUP/
ftpc "put $user.$DATE.tar"
rm -f $user.$DATE.tar
fi
echo
}
# Switching on backup system types
for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\n}"); do
case $backup_type in
local) local_backup ;;
ftp) ftp_backup ;;
esac
done
# Removing tmpdir
cd /
rm -rf $tmpdir
# Calculation run time
run_time=$((end_time - start_time))
run_time=$((run_time / 60))
current_time=$(date "+%T")
if [ "$run_time" -lt 1 ]; then
run_time=1
fi
min=miutes
if [ "$run_time" -eq 1 ]; then
min=minute
fi
echo "$(date "+%F %T") Backup took $run_time $min"
echo
echo
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Deleting old backup records
for backup_record in $deprecated; do
if [ -e "$USER_DATA/backup.conf" ]; then
sed -i "/DATE='$backup_record/d" $USER_DATA/backup.conf
fi
done
# Concatenating string
backup_str="BACKUP='$user.$DATE.tar'"
backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
backup_str="$backup_str VESTA='$vst'"
backup_str="$backup_str WEB='${web_list// /,}'"
backup_str="$backup_str DNS='${dns_list// /,}'"
backup_str="$backup_str MAIL='${mail_list// /,}'"
backup_str="$backup_str DB='${db_list// /,}'"
backup_str="$backup_str CRON='$cron_list'"
backup_str="$backup_str RUNTIME='$run_time' TIME='$TIME' DATE='$DATE'"
echo "$backup_str" >> $USER_DATA/backup.conf
chmod 660 $USER_DATA/backup.conf
# Logging
log_event "$OK" "$EVENT"
exit