Skip to content

Commit 08dea93

Browse files
committed
added runtime loggin in backup script + listing function
1 parent d2fe194 commit 08dea93

File tree

3 files changed

+225
-71
lines changed

3 files changed

+225
-71
lines changed

bin/v_backup_sys_user

Lines changed: 165 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
4141
i=0
4242
while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
4343
if [ -z "$output" ]; then
44-
echo "$(date +%m-%d-%y" "%H:%m:%S) Load Average $la"
44+
echo "$(date +%m-%d-%y" "%H:%M:%S) Load Average $la"
4545
echo
4646
fi
4747

@@ -53,28 +53,107 @@ while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
5353
exit $E_LOAD_AVERAGE
5454
fi
5555

56-
i=$((i + 1))
56+
(( ++i))
5757
done
5858

5959

6060
#----------------------------------------------------------#
6161
# Action #
6262
#----------------------------------------------------------#
6363

64+
# Get current time
65+
start_time=$(date '+%s')
66+
sleep 131
67+
6468
# Creating temporary random directory
6569
tmpdir=$(mktemp -p $V_TMP -d)
6670

6771
# Prinitng status
6872
if [ -z "$output" ]; then
69-
echo "$(date +%m-%d-%y" "%H:%m:%S) System backup for user $user"
73+
echo "$(date +%m-%d-%y" "%H:%M:%S) System backup for user $user"
7074
echo "TMPDIR is $tmpdir"
7175
echo
7276
fi
7377

74-
# Addding vesta version
75-
echo "1.0" >$tmpdir/backup_version
78+
# Addding backup and vesta version
79+
echo "1.0" > $tmpdir/backup_version
7680
echo "$VERSION" > $tmpdir/vesta_version
7781

82+
# Vesta
83+
if [ -z "$output" ]; then
84+
echo "-- VESTA --"
85+
fi
86+
mkdir $tmpdir/vesta
87+
88+
# Backingup vesta configs
89+
if [ -z "$output" ]; then
90+
echo -e "\t$(date +%H:%M:%S) user.conf"
91+
fi
92+
cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
93+
94+
if [ -e "$V_USERS/$user/billing.log" ]; then
95+
if [ -z "$output" ]; then
96+
echo -e "\t$(date +%H:%M:%S) billing.log"
97+
fi
98+
cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
99+
fi
100+
101+
if [ -e "$V_USERS/$user/history.log" ]; then
102+
if [ -z "$output" ]; then
103+
echo -e "\t$(date +%H:%M:%S) history.log"
104+
fi
105+
106+
cp -r $V_USERS/$user/history.log $tmpdir/vesta/
107+
fi
108+
109+
if [ -e "$V_USERS/$user/backup.excludes" ]; then
110+
if [ -z "$output" ]; then
111+
echo -e "\t$(date +%H:%M:%S) backup.excludes"
112+
fi
113+
cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/
114+
fi
115+
116+
if [ -e "$V_USERS/$user/cron.conf" ]; then
117+
if [ -z "$output" ]; then
118+
echo -e "\t$(date +%H:%M:%S) cron.conf"
119+
fi
120+
cp -r $V_USERS/$user/cron.conf $tmpdir/vesta/
121+
fi
122+
123+
if [ -e "$V_USERS/$user/db.conf" ]; then
124+
if [ -z "$output" ]; then
125+
echo -e "\t$(date +%H:%M:%S) db.conf"
126+
fi
127+
cp -r $V_USERS/$user/db.conf $tmpdir/vesta/
128+
fi
129+
130+
if [ -e "$V_USERS/$user/dns.conf" ]; then
131+
if [ -z "$output" ]; then
132+
echo -e "\t$(date +%H:%M:%S) dns.conf"
133+
fi
134+
cp -r $V_USERS/$user/dns.conf $tmpdir/vesta/
135+
fi
136+
137+
if [ -e "$V_USERS/$user/mail.conf" ]; then
138+
if [ -z "$output" ]; then
139+
echo -e "\t$(date +%H:%M:%S) mail.conf"
140+
fi
141+
cp -r $V_USERS/$user/mail.conf $tmpdir/vesta/
142+
fi
143+
144+
145+
if [ -e "$V_USERS/$user/web.conf" ]; then
146+
if [ -z "$output" ]; then
147+
echo -e "\t$(date +%H:%M:%S) web.conf"
148+
fi
149+
cp -r $V_USERS/$user/web.conf $tmpdir/vesta/
150+
fi
151+
152+
153+
if [ -z "$output" ]; then
154+
echo
155+
fi
156+
78157
# Checking excludes
79158
OLD_IFS="$IFS"
80159
IFS=$'\n'
@@ -85,7 +164,7 @@ if [ -e "$V_USERS/$user/backup.excludes" ]; then
85164

86165
for exclude in $(cat $V_USERS/$user/backup.excludes); do
87166
if [ -z "$output" ]; then
88-
echo -e "\t $exclude"
167+
echo -e "\t$exclude"
89168
fi
90169
# Indirect variable references (a bit of black magic)
91170
eval ${exclude%%=*}=${exclude#*=}
@@ -119,10 +198,11 @@ then
119198
web_list="$web_list $domain"
120199
fi
121200
done
201+
web_list=$(echo "$web_list" | sed -e "s/ */\ /g" -e "s/^ //")
122202

123203
for domain in $web_list; do
124204
if [ -z "$output" ]; then
125-
echo -e "\t$(date +%H:%m:%S) $domain"
205+
echo -e "\t$(date +%H:%M:%S) $domain"
126206
fi
127207

128208
# Defining domain variables
@@ -223,10 +303,11 @@ then
223303
dns_list="$dns_list $domain"
224304
fi
225305
done
306+
dns_list=$(echo "$dns_list" | sed -e "s/ */\ /g" -e "s/^ //")
226307

227308
for domain in $dns_list; do
228309
if [ -z "$output" ]; then
229-
echo -e "\t$(date +%H:%m:%S) $domain"
310+
echo -e "\t$(date +%H:%M:%S) $domain"
230311
fi
231312

232313
# Building directory tree
@@ -271,6 +352,7 @@ if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
271352
db_list="$db_list $database"
272353
fi
273354
done
355+
db_list=$(echo "$db_list" | sed -e "s/ */\ /g" -e "s/^ //")
274356

275357
for database in $db_list; do
276358
type=$(get_db_value '$TYPE')
@@ -280,7 +362,7 @@ if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
280362
grants="$tmpdir/db/$database.$type.$db_user"
281363

282364
if [ -z "$output" ]; then
283-
echo -e "\t$(date +%H:%m:%S) $database $type"
365+
echo -e "\t$(date +%H:%M:%S) $database $type"
284366
fi
285367

286368
case $type in
@@ -310,15 +392,16 @@ then
310392

311393
# Backingup cron recods
312394
if [ -z "$output" ]; then
313-
echo -e "\t$(date +%H:%m:%S) cron.conf"
395+
echo -e "\t$(date +%H:%M:%S) cron.conf"
314396
fi
315397
cp $V_USERS/$user/cron.conf $tmpdir/cron/
316398

317399
if [ -z "$output" ]; then
318-
echo -e "\t$(date +%H:%m:%S) system cron"
400+
echo -e "\t$(date +%H:%M:%S) system cron"
319401
fi
320402

321403
if [ -e "/var/spool/cron/$user" ]; then
404+
cron_list='yes'
322405
cp /var/spool/cron/$user $tmpdir/cron/
323406
fi
324407

@@ -336,11 +419,15 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
336419
mkdir $tmpdir/cert
337420

338421
# Backingup ssl certificates
339-
for cert in $(ls $V_USERS/$user/cert/); do
422+
cert_list=$(ls $V_USERS/$user/cert/ | grep ".crt" |\
423+
sed -e "s/\.crt$//" |\
424+
tr '\n' ' ' |\
425+
sed -e 's/ $//' )
426+
for cert in $cert_list; do
340427
if [ -z "$output" ]; then
341-
echo -e "\t$(date +%H:%m:%S) $cert"
428+
echo -e "\t$(date +%H:%M:%S) $cert"
342429
fi
343-
cp $V_USERS/$user/cert/$cert $tmpdir/cert/
430+
cp $V_USERS/$user/cert/$cert.* $tmpdir/cert/
344431
done
345432

346433
if [ -z "$output" ]; then
@@ -349,62 +436,42 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
349436

350437
fi
351438

352-
# Vesta
353-
if [ -z "$output" ]; then
354-
echo "-- VESTA --"
355-
fi
356-
mkdir $tmpdir/vesta
439+
# Get backup size
440+
size="$(du -shm $tmpdir | cut -f 1)"
357441

358-
# Backingup vesta configs
359-
if [ -z "$output" ]; then
360-
echo -e "\t$(date +%H:%m:%S) user.conf"
361-
fi
362-
cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
363-
364-
if [ -e "$V_USERS/$user/billing.log" ]; then
365-
if [ -z "$output" ]; then
366-
echo -e "\t$(date +%H:%m:%S) billing.log"
367-
fi
368-
cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
369-
fi
370-
371-
if [ -e "$V_USERS/$user/history.log" ]; then
372-
if [ -z "$output" ]; then
373-
echo -e "\t$(date +%H:%m:%S) history.log"
374-
fi
375-
376-
cp -r $V_USERS/$user/history.log $tmpdir/vesta/
377-
fi
378-
379-
if [ -e "$V_USERS/$user/backup.excludes" ]; then
380-
if [ -z "$output" ]; then
381-
echo -e "\t$(date +%H:%m:%S) backup.excludes"
382-
fi
383-
cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/
384-
fi
385-
386-
if [ -z "$output" ]; then
387-
echo
388-
fi
442+
# Get current time
443+
end_time=$(date '+%s')
389444

390445
# Defining local storage function
391446
local_backup(){
392447
if [ -z "$output" ]; then
393448
echo "-- STORAGE --"
394-
echo -e "\t$(date +%H:%m:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
449+
echo -e "\t$(date +%H:%M:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
450+
fi
451+
452+
# Removing dublicate for this day
453+
deprecated="$V_DATE"
454+
if [ -e "$V_BACKUP/$user.$V_DATE.tar" ]; then
455+
echo -e "\tDeleting old backup for $V_DATE"
456+
rm -f $V_BACKUP/$user.$V_DATE.tar
395457
fi
396458

397459
# Checking retention
398-
archives=$(ls -lrt $V_BACKUP/ |awk '{print $9}' |grep "^$user\.")
399-
archives_q=$(echo "$archives" |wc -l)
400-
if [ "$archives_q" -ge "$backups" ]; then
401-
archives_r=$((archives_q - backups))
402-
for archive in $(echo "$archives" | head -n $archives_r); do
403-
# Removing old archives
460+
backup_list=$(ls -lrt $V_BACKUP/ | awk '{print $9}' |grep "^$user\.")
461+
backups_count=$(echo "$backup_list" | wc -l)
462+
463+
if [ "$BACKUPS" -le "$backups_count" ]; then
464+
backups_rm_number=$((backups_count - BACKUPS))
465+
(( ++backups_rm_number))
466+
467+
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
468+
# Removing old backup
469+
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
470+
deprecated="$deprecated $backup_date"
404471
if [ -z "$output" ]; then
405-
echo -e "\tRemoving old $archive"
472+
echo -e "\tDeleteing old backup for $backup_date"
406473
fi
407-
rm -f $V_BACKUP/$archive
474+
rm -f $V_BACKUP/$backup
408475
done
409476
fi
410477

@@ -466,7 +533,7 @@ ftp_backup(){
466533

467534
# Debug info
468535
if [ -z "$output" ]; then
469-
echo -e "\t$(date +%H:%m:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar"
536+
echo -e "\t$(date +%H:%M:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar"
470537
fi
471538

472539
# Checking ftp permission
@@ -481,16 +548,18 @@ ftp_backup(){
481548
fi
482549

483550
# Checking retention
484-
archives=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
485-
archives_q=$(echo "$archives" | wc -l)
486-
if [ "$archives_q" -ge "$backups" ]; then
551+
backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
552+
backups_count=$(echo "$backup_list" | wc -l)
553+
if [ "$backups_count" -ge "$BACKUPS" ]; then
487554
# Removing old backups
488-
archives_r=$((archives_q - backups))
489-
for archive in $(echo "$archives" | tail -n $archives_r); do
555+
backups_rm_number=$((backups_count - BACKUPS))
556+
for backup in $(echo "$backup_list" | tail -n $backups_rm_number); do
557+
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
558+
deprecated="$deprecated $backup_date"
490559
if [ -z "$output" ]; then
491-
echo -e "\tRemoving old $archive"
560+
echo -e "\tDeleteing old backup for $backup_date"
492561
fi
493-
ftpc "delete $archive"
562+
ftpc "delete $backup"
494563
done
495564
fi
496565

@@ -523,17 +592,45 @@ done
523592
cd /
524593
rm -rf $tmpdir
525594

595+
# Calcilation run time
596+
run_time=$((end_time - start_time))
597+
run_time=$((run_time / 60))
598+
current_time=$(date +'%H:%M:%S')
599+
if [ "$run_time" -lt 1 ]; then
600+
run_time=1
601+
fi
602+
526603
if [ -z "$output" ]; then
527-
echo "$(date +'%m-%d-%y %H:%m:%S')"
604+
min=miutes
605+
if [ "$run_time" -eq 1 ]; then
606+
min=minute
607+
fi
608+
echo "$(date +'%m-%d-%y %H:%M:%S') Backup took $run_time $min"
528609
echo
529610
echo
530611
fi
531612

532613

614+
533615
#----------------------------------------------------------#
534616
# Vesta #
535617
#----------------------------------------------------------#
536618

619+
# Deleting old backup records
620+
for backup_record in $deprecated; do
621+
sed -i "/DATE='$backup_record/d" $V_USERS/$user/backup.conf
622+
done
623+
624+
# Concatenating string
625+
backup_str="DATE='$V_DATE' TIME='$current_time' RUNTIME='$run_time'"
626+
backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
627+
backup_str="$backup_str WEB='${web_list// /,}'"
628+
backup_str="$backup_str DNS='${dns_list// /,}'"
629+
backup_str="$backup_str MAIL='${mail_list// /,}'"
630+
backup_str="$backup_str DB='${db_list// /,}'"
631+
backup_str="$backup_str CERTIFICATES='${cert_list// /,}'"
632+
backup_str="$backup_str CRON='$cron_list'"
633+
echo "$backup_str" >> $V_USERS/$user/backup.conf
537634

538635
# Logging
539636
log_event 'system' "$V_EVENT"

0 commit comments

Comments
 (0)