added runtime loggin in backup script + listing function

This commit is contained in:
Serghey Rodin 2011-11-14 15:02:54 +02:00
commit 08dea938b5
3 changed files with 225 additions and 71 deletions

View file

@ -41,7 +41,7 @@ la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
i=0 i=0
while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
if [ -z "$output" ]; then 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 echo
fi fi
@ -53,7 +53,7 @@ while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
exit $E_LOAD_AVERAGE exit $E_LOAD_AVERAGE
fi fi
i=$((i + 1)) (( ++i))
done done
@ -61,20 +61,99 @@ done
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Get current time
start_time=$(date '+%s')
sleep 131
# Creating temporary random directory # Creating temporary random directory
tmpdir=$(mktemp -p $V_TMP -d) tmpdir=$(mktemp -p $V_TMP -d)
# Prinitng status # Prinitng status
if [ -z "$output" ]; then 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 "TMPDIR is $tmpdir"
echo echo
fi fi
# Addding vesta version # Addding backup and vesta version
echo "1.0" >$tmpdir/backup_version echo "1.0" > $tmpdir/backup_version
echo "$VERSION" > $tmpdir/vesta_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 # Checking excludes
OLD_IFS="$IFS" OLD_IFS="$IFS"
IFS=$'\n' IFS=$'\n'
@ -85,7 +164,7 @@ if [ -e "$V_USERS/$user/backup.excludes" ]; then
for exclude in $(cat $V_USERS/$user/backup.excludes); do for exclude in $(cat $V_USERS/$user/backup.excludes); do
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo -e "\t $exclude" echo -e "\t$exclude"
fi fi
# Indirect variable references (a bit of black magic) # Indirect variable references (a bit of black magic)
eval ${exclude%%=*}=${exclude#*=} eval ${exclude%%=*}=${exclude#*=}
@ -119,10 +198,11 @@ then
web_list="$web_list $domain" web_list="$web_list $domain"
fi fi
done done
web_list=$(echo "$web_list" | sed -e "s/ */\ /g" -e "s/^ //")
for domain in $web_list; do for domain in $web_list; do
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo -e "\t$(date +%H:%m:%S) $domain" echo -e "\t$(date +%H:%M:%S) $domain"
fi fi
# Defining domain variables # Defining domain variables
@ -223,10 +303,11 @@ then
dns_list="$dns_list $domain" dns_list="$dns_list $domain"
fi fi
done done
dns_list=$(echo "$dns_list" | sed -e "s/ */\ /g" -e "s/^ //")
for domain in $dns_list; do for domain in $dns_list; do
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo -e "\t$(date +%H:%m:%S) $domain" echo -e "\t$(date +%H:%M:%S) $domain"
fi fi
# Building directory tree # Building directory tree
@ -271,6 +352,7 @@ if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
db_list="$db_list $database" db_list="$db_list $database"
fi fi
done done
db_list=$(echo "$db_list" | sed -e "s/ */\ /g" -e "s/^ //")
for database in $db_list; do for database in $db_list; do
type=$(get_db_value '$TYPE') 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" grants="$tmpdir/db/$database.$type.$db_user"
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo -e "\t$(date +%H:%m:%S) $database $type" echo -e "\t$(date +%H:%M:%S) $database $type"
fi fi
case $type in case $type in
@ -310,15 +392,16 @@ then
# Backingup cron recods # Backingup cron recods
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo -e "\t$(date +%H:%m:%S) cron.conf" echo -e "\t$(date +%H:%M:%S) cron.conf"
fi fi
cp $V_USERS/$user/cron.conf $tmpdir/cron/ cp $V_USERS/$user/cron.conf $tmpdir/cron/
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo -e "\t$(date +%H:%m:%S) system cron" echo -e "\t$(date +%H:%M:%S) system cron"
fi fi
if [ -e "/var/spool/cron/$user" ]; then if [ -e "/var/spool/cron/$user" ]; then
cron_list='yes'
cp /var/spool/cron/$user $tmpdir/cron/ cp /var/spool/cron/$user $tmpdir/cron/
fi fi
@ -336,11 +419,15 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
mkdir $tmpdir/cert mkdir $tmpdir/cert
# Backingup ssl certificates # 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 if [ -z "$output" ]; then
echo -e "\t$(date +%H:%m:%S) $cert" echo -e "\t$(date +%H:%M:%S) $cert"
fi fi
cp $V_USERS/$user/cert/$cert $tmpdir/cert/ cp $V_USERS/$user/cert/$cert.* $tmpdir/cert/
done done
if [ -z "$output" ]; then if [ -z "$output" ]; then
@ -349,62 +436,42 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
fi fi
# Vesta # Get backup size
if [ -z "$output" ]; then size="$(du -shm $tmpdir | cut -f 1)"
echo "-- VESTA --"
fi
mkdir $tmpdir/vesta
# Backingup vesta configs # Get current time
if [ -z "$output" ]; then end_time=$(date '+%s')
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
# Defining local storage function # Defining local storage function
local_backup(){ local_backup(){
if [ -z "$output" ]; then if [ -z "$output" ]; then
echo "-- STORAGE --" 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 fi
# Checking retention # Checking retention
archives=$(ls -lrt $V_BACKUP/ |awk '{print $9}' |grep "^$user\.") backup_list=$(ls -lrt $V_BACKUP/ | awk '{print $9}' |grep "^$user\.")
archives_q=$(echo "$archives" |wc -l) backups_count=$(echo "$backup_list" | wc -l)
if [ "$archives_q" -ge "$backups" ]; then
archives_r=$((archives_q - backups)) if [ "$BACKUPS" -le "$backups_count" ]; then
for archive in $(echo "$archives" | head -n $archives_r); do backups_rm_number=$((backups_count - BACKUPS))
# Removing old archives (( ++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 if [ -z "$output" ]; then
echo -e "\tRemoving old $archive" echo -e "\tDeleteing old backup for $backup_date"
fi fi
rm -f $V_BACKUP/$archive rm -f $V_BACKUP/$backup
done done
fi fi
@ -466,7 +533,7 @@ ftp_backup(){
# Debug info # Debug info
if [ -z "$output" ]; then 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 fi
# Checking ftp permission # Checking ftp permission
@ -481,16 +548,18 @@ ftp_backup(){
fi fi
# Checking retention # Checking retention
archives=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.") backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
archives_q=$(echo "$archives" | wc -l) backups_count=$(echo "$backup_list" | wc -l)
if [ "$archives_q" -ge "$backups" ]; then if [ "$backups_count" -ge "$BACKUPS" ]; then
# Removing old backups # Removing old backups
archives_r=$((archives_q - backups)) backups_rm_number=$((backups_count - BACKUPS))
for archive in $(echo "$archives" | tail -n $archives_r); do 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 if [ -z "$output" ]; then
echo -e "\tRemoving old $archive" echo -e "\tDeleteing old backup for $backup_date"
fi fi
ftpc "delete $archive" ftpc "delete $backup"
done done
fi fi
@ -523,17 +592,45 @@ done
cd / cd /
rm -rf $tmpdir 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 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
echo echo
fi fi
#----------------------------------------------------------# #----------------------------------------------------------#
# Vesta # # 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 # Logging
log_event 'system' "$V_EVENT" log_event 'system' "$V_EVENT"

58
bin/v_list_sys_user_backups Executable file
View file

@ -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

View file

@ -957,7 +957,6 @@ json_list() {
i=1 i=1
for field in $fields; do for field in $fields; do
eval value=$field eval value=$field
if [ $i -eq 1 ]; then if [ $i -eq 1 ]; then
# Printing parrent # Printing parrent
(( ++i)) (( ++i))
@ -1086,8 +1085,8 @@ get_config_value() {
} }
is_backup_enabled() { is_backup_enabled() {
backups=$(grep "BACKUPS='" $V_USERS/$user/user.conf |cut -f 2 -d \') BACKUPS=$(grep "BACKUPS='" $V_USERS/$user/user.conf |cut -f 2 -d \')
if [ -z "$backups" ] || [[ "$backups" -le '0' ]]; then if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then
echo "Error: User backups are disabled" echo "Error: User backups are disabled"
log_event 'debug' "$E_BACKUP_DISABLED $V_EVENT" log_event 'debug' "$E_BACKUP_DISABLED $V_EVENT"
exit $E_BACKUP_DISABLED exit $E_BACKUP_DISABLED