From 08dea938b50a686a6f03de997d877c15419fcb82 Mon Sep 17 00:00:00 2001 From: Serghey Rodin Date: Mon, 14 Nov 2011 15:02:54 +0200 Subject: [PATCH] added runtime loggin in backup script + listing function --- bin/v_backup_sys_user | 233 +++++++++++++++++++++++++----------- bin/v_list_sys_user_backups | 58 +++++++++ func/shared.func | 5 +- 3 files changed, 225 insertions(+), 71 deletions(-) create mode 100755 bin/v_list_sys_user_backups diff --git a/bin/v_backup_sys_user b/bin/v_backup_sys_user index bad91216c..05c1eade3 100755 --- a/bin/v_backup_sys_user +++ b/bin/v_backup_sys_user @@ -41,7 +41,7 @@ la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.') i=0 while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do if [ -z "$output" ]; then - echo "$(date +%m-%d-%y" "%H:%m:%S) Load Average $la" + echo "$(date +%m-%d-%y" "%H:%M:%S) Load Average $la" echo fi @@ -53,7 +53,7 @@ while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do exit $E_LOAD_AVERAGE fi - i=$((i + 1)) + (( ++i)) done @@ -61,20 +61,99 @@ done # Action # #----------------------------------------------------------# +# Get current time +start_time=$(date '+%s') +sleep 131 + # Creating temporary random directory tmpdir=$(mktemp -p $V_TMP -d) # Prinitng status if [ -z "$output" ]; then - echo "$(date +%m-%d-%y" "%H:%m:%S) System backup for user $user" + echo "$(date +%m-%d-%y" "%H:%M:%S) System backup for user $user" echo "TMPDIR is $tmpdir" echo fi -# Addding vesta version -echo "1.0" >$tmpdir/backup_version +# Addding backup and vesta version +echo "1.0" > $tmpdir/backup_version echo "$VERSION" > $tmpdir/vesta_version +# Vesta +if [ -z "$output" ]; then + echo "-- VESTA --" +fi +mkdir $tmpdir/vesta + +# Backingup vesta configs +if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) user.conf" +fi +cp -r $V_USERS/$user/user.conf $tmpdir/vesta/ + +if [ -e "$V_USERS/$user/billing.log" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) billing.log" + fi + cp -r $V_USERS/$user/billing.log $tmpdir/vesta/ +fi + +if [ -e "$V_USERS/$user/history.log" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) history.log" + fi + + cp -r $V_USERS/$user/history.log $tmpdir/vesta/ +fi + +if [ -e "$V_USERS/$user/backup.excludes" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) backup.excludes" + fi + cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/ +fi + +if [ -e "$V_USERS/$user/cron.conf" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) cron.conf" + fi + cp -r $V_USERS/$user/cron.conf $tmpdir/vesta/ +fi + +if [ -e "$V_USERS/$user/db.conf" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) db.conf" + fi + cp -r $V_USERS/$user/db.conf $tmpdir/vesta/ +fi + +if [ -e "$V_USERS/$user/dns.conf" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) dns.conf" + fi + cp -r $V_USERS/$user/dns.conf $tmpdir/vesta/ +fi + +if [ -e "$V_USERS/$user/mail.conf" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) mail.conf" + fi + cp -r $V_USERS/$user/mail.conf $tmpdir/vesta/ +fi + + +if [ -e "$V_USERS/$user/web.conf" ]; then + if [ -z "$output" ]; then + echo -e "\t$(date +%H:%M:%S) web.conf" + fi + cp -r $V_USERS/$user/web.conf $tmpdir/vesta/ +fi + + +if [ -z "$output" ]; then + echo +fi + # Checking excludes OLD_IFS="$IFS" IFS=$'\n' @@ -85,7 +164,7 @@ if [ -e "$V_USERS/$user/backup.excludes" ]; then for exclude in $(cat $V_USERS/$user/backup.excludes); do if [ -z "$output" ]; then - echo -e "\t $exclude" + echo -e "\t$exclude" fi # Indirect variable references (a bit of black magic) eval ${exclude%%=*}=${exclude#*=} @@ -119,10 +198,11 @@ 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 if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) $domain" + echo -e "\t$(date +%H:%M:%S) $domain" fi # Defining domain variables @@ -223,10 +303,11 @@ 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 if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) $domain" + echo -e "\t$(date +%H:%M:%S) $domain" fi # Building directory tree @@ -271,6 +352,7 @@ if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; 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 type=$(get_db_value '$TYPE') @@ -280,7 +362,7 @@ if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then grants="$tmpdir/db/$database.$type.$db_user" if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) $database $type" + echo -e "\t$(date +%H:%M:%S) $database $type" fi case $type in @@ -310,15 +392,16 @@ then # Backingup cron recods if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) cron.conf" + echo -e "\t$(date +%H:%M:%S) cron.conf" fi cp $V_USERS/$user/cron.conf $tmpdir/cron/ if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) system cron" + echo -e "\t$(date +%H:%M:%S) system cron" fi if [ -e "/var/spool/cron/$user" ]; then + cron_list='yes' cp /var/spool/cron/$user $tmpdir/cron/ fi @@ -336,11 +419,15 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then mkdir $tmpdir/cert # Backingup ssl certificates - for cert in $(ls $V_USERS/$user/cert/); do + cert_list=$(ls $V_USERS/$user/cert/ | grep ".crt" |\ + sed -e "s/\.crt$//" |\ + tr '\n' ' ' |\ + sed -e 's/ $//' ) + for cert in $cert_list; do if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) $cert" + echo -e "\t$(date +%H:%M:%S) $cert" fi - cp $V_USERS/$user/cert/$cert $tmpdir/cert/ + cp $V_USERS/$user/cert/$cert.* $tmpdir/cert/ done if [ -z "$output" ]; then @@ -349,62 +436,42 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then fi -# Vesta -if [ -z "$output" ]; then - echo "-- VESTA --" -fi -mkdir $tmpdir/vesta +# Get backup size +size="$(du -shm $tmpdir | cut -f 1)" -# Backingup vesta configs -if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) user.conf" -fi -cp -r $V_USERS/$user/user.conf $tmpdir/vesta/ - -if [ -e "$V_USERS/$user/billing.log" ]; then - if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) billing.log" - fi - cp -r $V_USERS/$user/billing.log $tmpdir/vesta/ -fi - -if [ -e "$V_USERS/$user/history.log" ]; then - if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) history.log" - fi - - cp -r $V_USERS/$user/history.log $tmpdir/vesta/ -fi - -if [ -e "$V_USERS/$user/backup.excludes" ]; then - if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) backup.excludes" - fi - cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/ -fi - -if [ -z "$output" ]; then - echo -fi +# Get current time +end_time=$(date '+%s') # Defining local storage function local_backup(){ if [ -z "$output" ]; then echo "-- STORAGE --" - echo -e "\t$(date +%H:%m:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar" + echo -e "\t$(date +%H:%M:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar" + fi + + # Removing dublicate for this day + deprecated="$V_DATE" + if [ -e "$V_BACKUP/$user.$V_DATE.tar" ]; then + echo -e "\tDeleting old backup for $V_DATE" + rm -f $V_BACKUP/$user.$V_DATE.tar fi # Checking retention - archives=$(ls -lrt $V_BACKUP/ |awk '{print $9}' |grep "^$user\.") - archives_q=$(echo "$archives" |wc -l) - if [ "$archives_q" -ge "$backups" ]; then - archives_r=$((archives_q - backups)) - for archive in $(echo "$archives" | head -n $archives_r); do - # Removing old archives + backup_list=$(ls -lrt $V_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" if [ -z "$output" ]; then - echo -e "\tRemoving old $archive" + echo -e "\tDeleteing old backup for $backup_date" fi - rm -f $V_BACKUP/$archive + rm -f $V_BACKUP/$backup done fi @@ -466,7 +533,7 @@ ftp_backup(){ # Debug info if [ -z "$output" ]; then - echo -e "\t$(date +%H:%m:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar" + echo -e "\t$(date +%H:%M:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar" fi # Checking ftp permission @@ -481,16 +548,18 @@ ftp_backup(){ fi # Checking retention - archives=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.") - archives_q=$(echo "$archives" | wc -l) - if [ "$archives_q" -ge "$backups" ]; then + backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.") + backups_count=$(echo "$backup_list" | wc -l) + if [ "$backups_count" -ge "$BACKUPS" ]; then # Removing old backups - archives_r=$((archives_q - backups)) - for archive in $(echo "$archives" | tail -n $archives_r); do + backups_rm_number=$((backups_count - BACKUPS)) + for backup in $(echo "$backup_list" | tail -n $backups_rm_number); do + backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//") + deprecated="$deprecated $backup_date" if [ -z "$output" ]; then - echo -e "\tRemoving old $archive" + echo -e "\tDeleteing old backup for $backup_date" fi - ftpc "delete $archive" + ftpc "delete $backup" done fi @@ -523,17 +592,45 @@ done cd / rm -rf $tmpdir +# Calcilation run time +run_time=$((end_time - start_time)) +run_time=$((run_time / 60)) +current_time=$(date +'%H:%M:%S') +if [ "$run_time" -lt 1 ]; then + run_time=1 +fi + if [ -z "$output" ]; then - echo "$(date +'%m-%d-%y %H:%m:%S')" + min=miutes + if [ "$run_time" -eq 1 ]; then + min=minute + fi + echo "$(date +'%m-%d-%y %H:%M:%S') Backup took $run_time $min" echo echo fi + #----------------------------------------------------------# # Vesta # #----------------------------------------------------------# +# Deleting old backup records +for backup_record in $deprecated; do + sed -i "/DATE='$backup_record/d" $V_USERS/$user/backup.conf +done + +# Concatenating string +backup_str="DATE='$V_DATE' TIME='$current_time' RUNTIME='$run_time'" +backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'" +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 CERTIFICATES='${cert_list// /,}'" +backup_str="$backup_str CRON='$cron_list'" +echo "$backup_str" >> $V_USERS/$user/backup.conf # Logging log_event 'system' "$V_EVENT" diff --git a/bin/v_list_sys_user_backups b/bin/v_list_sys_user_backups new file mode 100755 index 000000000..890e679ad --- /dev/null +++ b/bin/v_list_sys_user_backups @@ -0,0 +1,58 @@ +#!/bin/bash +# info: listing system bakups + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +# Argument defenition +user=$1 +format=${2-shell} + +# Importing variables +source $VESTA/conf/vars.conf +source $V_FUNC/shared.func + +#----------------------------------------------------------# +# Verifications # +#----------------------------------------------------------# + +# Checking args +check_args '1' "$#" 'user [format]' + +# Checking argument format +format_validation 'user' + +# Checking user +is_user_valid + + +#----------------------------------------------------------# +# Action # +#----------------------------------------------------------# +conf=$V_USERS/$user/backup.conf +if [ ! -e "$conf" ]; then + touch $conf +fi + +# Defining fileds to select +fields="\$DATE \$TIME \$RUNTIME \$TYPE \$SIZE \$WEB \$DNS \$DB \$MAIL" +fields="$fields \$CERTIFICATES \$CRON" + +# Listing domains +case $format in + json) json_list ;; + plain) nohead=1; shell_list ;; + shell) fields='$DATE $TIME $RUNTIME $TYPE $SIZE';shell_list |column -t ;; + *) check_args '1' '0' '[format]' ;; +esac + + +#----------------------------------------------------------# +# Vesta # +#----------------------------------------------------------# + +# Logging +log_event 'system' "$V_EVENT" + +exit diff --git a/func/shared.func b/func/shared.func index d50635cbc..fd31f3aa0 100644 --- a/func/shared.func +++ b/func/shared.func @@ -957,7 +957,6 @@ json_list() { i=1 for field in $fields; do eval value=$field - if [ $i -eq 1 ]; then # Printing parrent (( ++i)) @@ -1086,8 +1085,8 @@ get_config_value() { } is_backup_enabled() { - backups=$(grep "BACKUPS='" $V_USERS/$user/user.conf |cut -f 2 -d \') - if [ -z "$backups" ] || [[ "$backups" -le '0' ]]; then + BACKUPS=$(grep "BACKUPS='" $V_USERS/$user/user.conf |cut -f 2 -d \') + if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then echo "Error: User backups are disabled" log_event 'debug' "$E_BACKUP_DISABLED $V_EVENT" exit $E_BACKUP_DISABLED