From 7ba0758c4ef5119931a783e02cfac41a46d3cdd3 Mon Sep 17 00:00:00 2001 From: Serghey Rodin Date: Wed, 23 Nov 2011 16:04:27 +0200 Subject: [PATCH] added upd backup function --- bin/v_backup_sys_user | 65 ++++---- bin/v_list_sys_user_backups | 6 +- bin/v_upd_sys_user_backups | 286 ++++++++++++++++++++++++++++++++++++ 3 files changed, 320 insertions(+), 37 deletions(-) create mode 100755 bin/v_upd_sys_user_backups diff --git a/bin/v_backup_sys_user b/bin/v_backup_sys_user index d1d9de55..3c7e474a 100755 --- a/bin/v_backup_sys_user +++ b/bin/v_backup_sys_user @@ -78,6 +78,7 @@ fi # Addding backup and vesta version echo "1.0" > $tmpdir/backup_version echo "$VERSION" > $tmpdir/vesta_version +vst='yes' # Vesta if [ -z "$output" ]; then @@ -205,14 +206,14 @@ then echo -e "\t$(date +%H:%M:%S) $domain" fi + # Building directory tree + mkdir -p $tmpdir/web/$domain/conf + mkdir -p $tmpdir/web/$domain/ssl + # Defining domain variables domain_idn=$(idn -t --quiet -a "$domain") - tpl_name=$(get_web_domain_value '$TPL') - ssl_cert=$(get_web_domain_value '$SSL_CERT') - nginx=$(get_web_domain_value '$NGINX') + get_web_domain_values - # Building directory tree - mkdir -p $tmpdir/web/$domain/conf $tmpdir/web/$domain/cert # Packing data folders cd $V_HOME/$user/web/$domain @@ -227,14 +228,14 @@ then # Apache config if [ "$WEB_SYSTEM" = 'apache' ]; then # Parsing httpd.conf - tpl_file="$V_WEBTPL/apache_$tpl_name.tpl" + tpl_file="$V_WEBTPL/apache_$TPL.tpl" conf="$V_HOME/$user/conf/httpd.conf" get_web_config_brds sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf # SSL check - if [ ! -z "$ssl_cert" ]; then - tpl_file="$V_WEBTPL/apache_$tpl_name.stpl" + if [ ! -z "$SSL" ]; then + tpl_file="$V_WEBTPL/apache_$TPL.stpl" conf="$V_HOME/$user/conf/shttpd.conf" get_web_config_brds sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf @@ -242,15 +243,15 @@ then fi # Nginx config - if [ ! -z "$nginx" ] ; then - tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.tpl" + if [ ! -z "$NGINX" ] ; then + tpl_file="$V_WEBTPL/ngingx_vhost_$NGINX.tpl" conf="$V_HOME/$user/conf/nginx.conf" get_web_config_brds sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf # SSL check - if [ ! -z "$ssl_cert" ] ; then - tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.stpl" + if [ ! -z "$SSL" ] ; then + tpl_file="$V_WEBTPL/ngingx_vhost_$NGINX.stpl" conf="$V_HOME/$user/conf/snginx.conf" get_web_config_brds sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf @@ -263,11 +264,11 @@ then done # SSL Certificates - if [ ! -z "$ssl_cert" ] ; then - cp $V_HOME/$user/conf/$ssl_cert.* cert/ + if [ ! -z "$SSL" ] ; then + cp $V_HOME/$user/conf/$SSL.* ssl/ fi - tar -rf $tmpdir/web/$domain/$domain.tar conf cert + tar -rf $tmpdir/web/$domain/$domain.tar conf ssl mv $tmpdir/web/$domain/$domain.tar $tmpdir/web/ rm -rf $tmpdir/web/$domain @@ -319,7 +320,7 @@ then grep "DOMAIN='$domain'" $conf > dns.conf # Backingup dns recods - cp $V_USERS/$user/zones/$domain $domain + cp $V_USERS/$user/dns/$domain $domain cp /etc/namedb/$domain.db $domain.db done @@ -379,7 +380,6 @@ if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then if [ -z "$output" ]; then echo fi - fi # Cron jobs @@ -414,12 +414,12 @@ fi # SSL CERTIFICATES if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then if [ -z "$output" ]; then - echo "-- CERTIFICATES --" + echo "-- SSL --" fi - mkdir $tmpdir/cert + mkdir $tmpdir/ssl # Backingup ssl certificates - cert_list=$(ls $V_USERS/$user/cert/ | grep ".crt" |\ + cert_list=$(ls $V_USERS/$user/ssl/ | grep ".crt" |\ sed -e "s/\.crt$//" |\ tr '\n' ' ' |\ sed -e 's/ $//' ) @@ -427,7 +427,7 @@ if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then if [ -z "$output" ]; then echo -e "\t$(date +%H:%M:%S) $cert" fi - cp $V_USERS/$user/cert/$cert.* $tmpdir/cert/ + cp $V_USERS/$user/ssl/$cert.* $tmpdir/ssl/ done if [ -z "$output" ]; then @@ -500,7 +500,7 @@ local_backup(){ # Defining ftp command function ftpc() { ftp -n $HOST $PORT <> $V_USERS/$user/backup.conf diff --git a/bin/v_list_sys_user_backups b/bin/v_list_sys_user_backups index 9252ea3e..f1ac3397 100755 --- a/bin/v_list_sys_user_backups +++ b/bin/v_list_sys_user_backups @@ -30,20 +30,22 @@ is_user_valid #----------------------------------------------------------# # Action # #----------------------------------------------------------# + +# Checking config 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="\$DATE \$TIME \$RUNTIME \$TYPE \$SIZE \$VESTA \$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 ;; + shell) fields='$DATE $TIME $RUNTIME $TYPE $SIZE'; shell_list |column -t;; *) check_args '1' '0' '[format]' ;; esac diff --git a/bin/v_upd_sys_user_backups b/bin/v_upd_sys_user_backups new file mode 100755 index 00000000..26cb5d99 --- /dev/null +++ b/bin/v_upd_sys_user_backups @@ -0,0 +1,286 @@ +#!/bin/bash +# info: updating current backups for + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +# Importing variables +source $VESTA/conf/vars.conf +source $V_CONF/vesta.conf +source $V_FUNC/shared.func + +# Argument defenition +user=$1 +type="${2-$BACKUP_SYSTEM}" + +# Defining tar parser function +get_backup_info() { + backup=$1 # full path to backup + backup_type="$2" # backup type + + backup_name=$(basename $backup ) + backup_date=$(echo $backup_name |cut -f 2 -d '.' ) + backup_time=$(stat --printf=%y $backup |cut -f 2 -d ' ' |cut -f 1 -d '.') + backup_size=$(du -sm $backup |cut -f 1 ) + backup_data=$(tar -tf $backup) + + # Checking system data + vst_data=$(echo "$backup_data" |grep "^./vesta/" |grep -v "^./vesta/$") + if [ -z "$vst_data" ]; then + vst='' + else + vst='yes' + fi + + # Checking web data + web_data=$(echo "$backup_data" |grep "^./web/" |grep -v "^./web/$" |sort) + if [ -z "$web_data" ]; then + web='' + else + i=1 + for domain in $web_data; do + domain="$(basename $domain|sed -e 's/.tar.gz$//')" + if [ "$i" -eq 1 ]; then + web="$domain" + i=2 + else + web="$web,$domain" + fi + done + fi + + # Checking dns data + dns_data=$(echo "$backup_data" |grep "^./dns/" |grep ".db$" |sort) + if [ -z "$dns_data" ]; then + dns='' + else + i=1 + for domain in $dns_data; do + domain="$(basename $domain|sed -e 's/.db$//')" + if [ "$i" -eq 1 ]; then + dns="$domain" + i=2 + else + dns="$dns,$domain" + fi + done + fi + + # Checking mail + mail_data=$(echo "$backup_data"|grep "^./mail/"|grep -v "^./mail/$"|sort) + if [ -z "$mail_data" ]; then + ml='' + else + i=1 + for domain in $mail_data; do + domain="$(basename $domain|sed -e 's/.tar.gz$//')" + if [ "$i" -eq 1 ]; then + ml="$domain" + i=2 + else + ml="$ml,$domain" + fi + done + fi + + # Checking databases + db_data=$(echo "$backup_data"|grep "^./db/"|grep ".sql.gz$"|sort) + if [ -z "$db_data" ]; then + db='' + else + i=1 + for dbase in $db_data; do + dbase=$(basename $dbase|sed -e "s/.\(my\|pg\)sql.sql.gz$//") + if [ "$i" -eq 1 ]; then + db="$dbase" + i=2 + else + db="$db,$dbase" + fi + done + fi + + # Checking certificates + ssl_data=$(echo "$backup_data"|grep "^./ssl/"|grep ".crt$"|sort) + if [ -z "$ssl_data" ]; then + ssl='' + else + i=1 + for certificate in $ssl_data; do + certificate="$(basename $certificate|sed -e "s/.crt$//")" + if [ "$i" -eq 1 ]; then + ssl="$certificate" + i=2 + else + ssl="$ssl,$certificate" + fi + done + fi + + + # Checking cron data + cron_data=$(echo "$backup_data" |grep "^./cron/" |grep -v "^./cron/$") + if [ -z "$cron_data" ]; then + cron='' + else + cron='yes' + fi + + str="DATE='$backup_date' TIME='$backup_time' RUNTIME='0'" + str="$str SIZE='$backup_size' TYPE='$backup_type' VESTA='$vst' WEB='$web'" + str="$str DNS='$dns' MAIL='$ml' DB='$db' SSL='$ssl' CRON='$cron'" + + echo "$str" +} + +# Defining ftp command function +ftpc() { + ftp -n $HOST $PORT <> $tmp_file + done +fi + +# Checking ftp +if [ "$type" == 'ftp' ]; then + tmpdir=$(mktemp -p $V_TMP -d) + ftmpdir=$(basename $tmpdir) + init_ftp_variables + check_ftp_connection + backups=$(ftpc ls|awk '{print $9}'|grep "^$user.") + + for backup in $backups; do + cd $tmpdir + if [ ! -z "$(ftpc "get $backup")" ]; then + echo "Error: FTP transfer error" + log_event 'debug' "$E_FTP_ERROR $V_EVENT" + exit $E_FTP_ERROR + fi + get_backup_info $tmpdir/$backup $type >> $tmp_file + rm -f $tmpdir/$backup + done + rm -rf $tmpdir +fi + +# Checking both local and ftp +if [ "$type" == 'ftp,local' ] || [ "$type" == 'local,ftp' ]; then + + tmpdir=$(mktemp -p $V_TMP -d) + ftmpdir=$(basename $tmpdir) + init_ftp_variables + check_ftp_connection + ftp_backups=$(ftpc ls|awk '{print $9}'|grep "^$user.") + local_backups=$(ls $V_BACKUP/ |grep "^$user." |sort) + backups=$(echo -e "$local_backups\n$ftp_backups" |\ + sort |uniq -c | awk '{print $1" "$2}') + + for backup in $(echo "$backups"|grep "^1 "|cut -f 2 -d ' '); do + check_ftp=$(echo $ftp_backups|grep -w $backup) + if [ ! -z "$check_ftp" ]; then + cd $tmpdir + if [ ! -z "$(ftpc "get $backup")" ]; then + echo "Error: FTP transfer error" + log_event 'debug' "$E_FTP_ERROR $V_EVENT" + exit $E_FTP_ERROR + fi + get_backup_info $tmpdir/$backup ftp >> $tmp_file + rm -f $tmpdir/$backup + else + get_backup_info $V_BACKUP/$backup local >> $tmp_file + fi + done + + for backup in $(echo "$backups"|grep "^2 "|cut -f 2 -d ' '); do + get_backup_info $V_BACKUP/$backup $type >> $tmp_file + done + rm -rf $tmpdir +fi + +# Checking if there was any output +if [ -e "$tmp_file" ]; then + cat $tmp_file | sort > $V_USERS/$user/backup.conf + rm $tmp_file +fi + + +#----------------------------------------------------------# +# Vesta # +#----------------------------------------------------------# + +# Logging +log_event 'system' "$V_EVENT" + +exit