Serghey Rodin 11 лет назад
Родитель
Сommit
cb99788a43
5 измененных файлов с 135 добавлено и 146 удалено
  1. 14 9
      bin/v-add-backup-ftp-host
  2. 72 76
      bin/v-add-backup-sftp-host
  3. 48 59
      bin/v-backup-user
  4. 1 1
      bin/v-delete-backup-ftp-host
  5. 0 1
      bin/v-delete-backup-sftp-host

+ 14 - 9
bin/v-add-backup-ftp-host

@@ -12,20 +12,23 @@
 # Argument defenition
 # Argument defenition
 host=$1
 host=$1
 ftp_user=$2
 ftp_user=$2
-ftp_password=$3
+password=$3
 ftp_path=${4-/backup}
 ftp_path=${4-/backup}
 ftp_port=${5-21}
 ftp_port=${5-21}
-A3='******'
 
 
 # Includes
 # Includes
 source $VESTA/func/main.sh
 source $VESTA/func/main.sh
 source $VESTA/conf/vesta.conf
 source $VESTA/conf/vesta.conf
 
 
+# Hiding password
+A3='******'
+EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
+
 # Defining ftp command function
 # Defining ftp command function
 ftpc() {
 ftpc() {
     ftp -p -n $host $ftp_port <<EOF
     ftp -p -n $host $ftp_port <<EOF
     quote USER $ftp_user
     quote USER $ftp_user
-    quote PASS $ftp_password
+    quote PASS $password
     binary
     binary
     $1
     $1
     $2
     $2
@@ -40,7 +43,8 @@ EOF
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
 check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
-validate_format 'host' 'ftp_user' 'ftp_password'
+validate_format 'host' 'ftp_user'
+is_password_valid
 
 
 
 
 #----------------------------------------------------------#
 #----------------------------------------------------------#
@@ -48,10 +52,11 @@ validate_format 'host' 'ftp_user' 'ftp_password'
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 # Checking network connection
 # Checking network connection
-fconn=$(ftpc)
-ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
+fconn=$(ftpc 2>&1)
+ferror=$(echo $fconn |\
+    grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
 if [ ! -z "$ferror" ]; then
 if [ ! -z "$ferror" ]; then
-    echo "Error: can't login to ftp"
+    echo "Error: can't login to ftp server"
     log_event "$E_CONNECT" "$EVENT"
     log_event "$E_CONNECT" "$EVENT"
     exit $E_CONNECT
     exit $E_CONNECT
 fi
 fi
@@ -71,7 +76,7 @@ fi
 # Adding backup host
 # Adding backup host
 echo "HOST='$host'
 echo "HOST='$host'
 USERNAME='$ftp_user'
 USERNAME='$ftp_user'
-PASSWORD='$ftp_password'
+PASSWORD='$password'
 BPATH='$ftp_path'
 BPATH='$ftp_path'
 PORT='$ftp_port'
 PORT='$ftp_port'
 TIME='$TIME'
 TIME='$TIME'
@@ -84,7 +89,7 @@ chmod 660 $VESTA/conf/ftp.backup.conf
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 # Update vesta.conf
 # Update vesta.conf
-if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
+if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
     echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
     echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
 else
 else
     bckp=$(echo "$BACKUP_SYSTEM,ftp" |\
     bckp=$(echo "$BACKUP_SYSTEM,ftp" |\

+ 72 - 76
bin/v-add-backup-sftp-host

@@ -1,6 +1,6 @@
 #!/bin/bash
 #!/bin/bash
 # info: add backup sftp host
 # info: add backup sftp host
-# options: HOST USERNAME [PASSWORD] [PATH] [PORT]
+# options: HOST USERNAME PASSWORD [PATH] [PORT]
 #
 #
 # The function adds sftp host for system backups
 # The function adds sftp host for system backups
 
 
@@ -11,87 +11,84 @@
 # Argument defenition
 # Argument defenition
 sftp_host=$1
 sftp_host=$1
 sftp_user=$2
 sftp_user=$2
-sftp_password=${3-******}
+password=$3
 sftp_path=${4-backup}
 sftp_path=${4-backup}
 sftp_port=${5-22}
 sftp_port=${5-22}
 
 
-A3='******'
-
 # Includes
 # Includes
 source $VESTA/func/main.sh
 source $VESTA/func/main.sh
 source $VESTA/conf/vesta.conf
 source $VESTA/conf/vesta.conf
 
 
-# Replace password with ******
-if [[ $A3 != '******' ]]
-then
-	EVENT="${EVENT/$sftp_password/******}"
-fi
+# Hiding password
+A3='******'
+EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
 
 
 # sftp command function
 # sftp command function
 sftpc() {
 sftpc() {
-expect -f "-" <<EOF "$@"
-	set count 0
-	spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$sftp_port $sftp_user@$sftp_host
-	expect {
-		"password:" {
-	    	send "$sftp_password\r"
-	    	exp_continue
-	  	}
-
-	  	-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
-			set count \$argc
-			set output "Disconnected."
-			set rc $E_FTP
-			exp_continue
-		}
-
-		-re ".*denied.*(publickey|password)." {
-			set output "Permission denied, wrong publickey or password."
-			set rc $E_CONNECT
-		}
-
-	  	"sftp>" {
-	  		if {\$count < \$argc} {
-	  			set arg [lindex \$argv \$count]
-	  			send "\$arg\r"
-	  			incr count
-	  		} else {
-	  			send "exit\r"
-	  			set output "Disconnected."
-	  			if {[info exists rc] != 1} {
-	  				set rc $OK
-	  			}
-	  		}
-	  		exp_continue  	
-		}
-
-		timeout {
-			set output "Connection timeout."
-			set rc $E_CONNECT
-		}
-  	}
-
-  	if {[info exists output] == 1} {
-  		puts "\$output"
-  	}
-
-  	exit \$rc		
+    expect -f "-" <<EOF "$@"
+        set count 0
+        spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o \
+            Port=$sftp_port $sftp_user@$sftp_host
+        expect {
+            "password:" {
+                send "$password\r"
+                exp_continue
+            }
+
+            -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
+                set count \$argc
+                set output "Disconnected."
+                set rc $E_FTP
+                exp_continue
+            }
+
+            -re ".*denied.*(publickey|password)." {
+                set output "Permission denied, wrong publickey or password."
+                set rc $E_CONNECT
+            }
+
+            "sftp>" {
+                if {\$count < \$argc} {
+                    set arg [lindex \$argv \$count]
+                    send "\$arg\r"
+                    incr count
+                } else {
+                    send "exit\r"
+                    set output "Disconnected."
+                    if {[info exists rc] != 1} {
+                        set rc $OK
+                    }
+                }
+                exp_continue
+            }
+
+            timeout {
+                set output "Connection timeout."
+                set rc $E_CONNECT
+            }
+        }
+
+        if {[info exists output] == 1} {
+            puts "\$output"
+        }
+
+        exit \$rc
 EOF
 EOF
 }
 }
 
 
+
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 #                    Verifications                         #
 #                    Verifications                         #
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
-check_expect=$(which expect)
-if [[ ! -n $check_expect ]]
-then
-	echo "Error: \"expect\" utility not found"
-	log_event "$E_NOTEXIST" "$EVENT"
-	exit $E_NOTEXIST
+check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
+which expect >/dev/null 2>&1
+if [ $? -ne 0 ];then
+    echo "Error: expect utility not found"
+    log_event "$E_NOTEXIST" "$EVENT"
+    exit $E_NOTEXIST
 fi
 fi
-
-check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
+is_password_valid
 
 
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 #                       Action                             #
 #                       Action                             #
@@ -100,35 +97,35 @@ check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
 # Checking network connection and write permissions
 # Checking network connection and write permissions
 sftmpdir="$sftp_path/vst.bK76A9SUkt"
 sftmpdir="$sftp_path/vst.bK76A9SUkt"
 sftpc "mkdir $sftp_path" > /dev/null 2>&1
 sftpc "mkdir $sftp_path" > /dev/null 2>&1
-sftpc "mkdir $sftmpdir" "rmdir $sftmpdir"
+sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
 rc=$?
 rc=$?
-if [[ "$rc" != 0 ]]
-then
-	case $rc in
+if [[ "$rc" != 0 ]]; then
+    case $rc in
         $E_CONNECT) echo "Error: can't login to sftp host";;
         $E_CONNECT) echo "Error: can't login to sftp host";;
-		$E_FTP) echo "Error: can't create temp folder on the sftp host";;
-	esac
-	log_event "$rc" "$EVENT"
-	exit "$rc"
+        $E_FTP) echo "Error: can't create temp folder on the sftp host";;
+    esac
+    log_event "$rc" "$EVENT"
+    exit "$rc"
 fi
 fi
 
 
 # Adding sftp backup config file
 # Adding sftp backup config file
 echo "HOST='$sftp_host'
 echo "HOST='$sftp_host'
 USERNAME='$sftp_user'
 USERNAME='$sftp_user'
-PASSWORD='$sftp_password'
+PASSWORD='$password'
 BPATH='$sftp_path'
 BPATH='$sftp_path'
 PORT='$sftp_port'
 PORT='$sftp_port'
 TIME='$TIME'
 TIME='$TIME'
 DATE='$DATE'" > $VESTA/conf/sftp.backup.conf
 DATE='$DATE'" > $VESTA/conf/sftp.backup.conf
 chmod 660 $VESTA/conf/sftp.backup.conf
 chmod 660 $VESTA/conf/sftp.backup.conf
 
 
+
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 #                       Vesta                              #
 #                       Vesta                              #
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 # Update vesta.conf
 # Update vesta.conf
-if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
-    echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
+if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
+    echo "BACKUP_SYSTEM='sftp'" >> $VESTA/conf/vesta.conf
 else
 else
     bckp=$(echo "$BACKUP_SYSTEM,sftp" |\
     bckp=$(echo "$BACKUP_SYSTEM,sftp" |\
         sed "s/,/\n/g"|\
         sed "s/,/\n/g"|\
@@ -139,7 +136,6 @@ else
 fi
 fi
 
 
 # Logging
 # Logging
-echo "$sftp_host/$sftp_path successfully added as backup destination"
 log_event "$OK" "$EVENT"
 log_event "$OK" "$EVENT"
 
 
 exit
 exit

+ 48 - 59
bin/v-backup-user

@@ -689,70 +689,63 @@ ftp_backup() {
 
 
 # sftp command function
 # sftp command function
 sftpc() {
 sftpc() {
-expect -f "-" <<EOF "$@"
-    set timeout 60
-    set count 0
-    spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$PORT $USERNAME@$HOST
-    expect {
-        "password:" {
-            send "$PASSWORD\r"
-            exp_continue
-        }
+    expect -f "-" <<EOF "$@"
+        set timeout 60
+        set count 0
+        spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
+            -o Port=$PORT $USERNAME@$HOST
+        expect {
+            "password:" {
+                send "$PASSWORD\r"
+                exp_continue
+            }
 
 
-        -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
-            set count \$argc
-            set output "Disconnected."
-            set rc $E_FTP
-            exp_continue
-        }
+            -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
+                set count \$argc
+                set output "Disconnected."
+                set rc $E_FTP
+                exp_continue
+            }
 
 
-        -re ".*denied.*(publickey|password)." {
-            set output "Permission denied, wrong publickey or password."
-            set rc $E_CONNECT
-        }
+            -re ".*denied.*(publickey|password)." {
+                set output "Permission denied, wrong publickey or password."
+                set rc $E_CONNECT
+            }
 
 
-        -re "\[0-9]*%" {
-            exp_continue
-        }
+            -re "\[0-9]*%" {
+                exp_continue
+            }
 
 
-        "sftp>" {
-            if {\$count < \$argc} {
-                set arg [lindex \$argv \$count]
-                send "\$arg\r"
-                incr count
-            } else {
-                send "exit\r"
-                set output "Disconnected."
-                if {[info exists rc] != 1} {
-                    set rc $OK
+            "sftp>" {
+                if {\$count < \$argc} {
+                    set arg [lindex \$argv \$count]
+                    send "\$arg\r"
+                    incr count
+                } else {
+                    send "exit\r"
+                    set output "Disconnected."
+                    if {[info exists rc] != 1} {
+                        set rc $OK
+                    }
                 }
                 }
+                exp_continue
             }
             }
-            exp_continue
-        }
 
 
-        timeout {
-            set output "Connection timeout."
-            set rc $E_CONNECT
+            timeout {
+                set output "Connection timeout."
+                set rc $E_CONNECT
+            }
         }
         }
-    }
 
 
-    if {[info exists output] == 1} {
-        puts "\$output"
-    }
+        if {[info exists output] == 1} {
+            puts "\$output"
+        }
 
 
     exit \$rc
     exit \$rc
 EOF
 EOF
 }
 }
 
 
 sftp_backup() {
 sftp_backup() {
-    #Checking expect installation
-    check_expect=$(which expect)
-    if [[ ! -n $check_expect ]]
-    then
-        echo "Error: \"expect\" utility not found"
-        log_event "$E_NOTEXIST" "$EVENT"
-        exit $E_NOTEXIST
-    fi
 
 
     # Checking config
     # Checking config
     if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
     if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
@@ -788,36 +781,33 @@ sftp_backup() {
     fi
     fi
 
 
     # Debug info
     # Debug info
-    echo -e "$(date "+%F %T") Remote: $HOST/$BPATH/$user.$DATE.tar"
+    echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$DATE.tar"
 
 
     # Checking network connection and write permissions
     # Checking network connection and write permissions
-    echo -e "$(date "+%F %T") Checking network connection and write permissions ..."
     sftmpdir="$BPATH/vst.bK76A9SUkt"
     sftmpdir="$BPATH/vst.bK76A9SUkt"
     sftpc "mkdir $BPATH" > /dev/null 2>&1
     sftpc "mkdir $BPATH" > /dev/null 2>&1
     sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
     sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
     rc=$?
     rc=$?
-    if [[ "$rc" != 0 ]]
-    then
+    if [[ "$rc" != 0 ]]; then
         rm -rf $tmpdir
         rm -rf $tmpdir
         case $rc in
         case $rc in
-            $E_CONNECT) echo "Error: can't login to sftp host" | $send_mail -s "$subj" $email;;
-            $E_FTP) echo "Error: can't create temp folder on the sftp host" | $send_mail -s "$subj" $email;;
+            $E_CONNECT) echo "Error: can't login to sftp host $HOST" |\
+                $send_mail -s "$subj" $email;;
+            $E_FTP) echo "Error: can't create temp folder on sftp $HOST" |\
+                $send_mail -s "$subj" $email;;
         esac
         esac
         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
         log_event "$rc" "$EVENT"
         log_event "$rc" "$EVENT"
         exit "$rc"
         exit "$rc"
     fi
     fi
-    echo -e "$(date "+%F %T") Connection established"
 
 
     # Checking retention
     # Checking retention
-    echo -e "$(date "+%F %T") Checking retention ..."
     backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}' |grep "^$user\.")
     backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}' |grep "^$user\.")
     backups_count=$(echo "$backup_list" | wc -l)
     backups_count=$(echo "$backup_list" | wc -l)
     if [ "$backups_count" -ge "$BACKUPS" ]; then
     if [ "$backups_count" -ge "$BACKUPS" ]; then
         backups_rm_number=$((backups_count - BACKUPS + 1))
         backups_rm_number=$((backups_count - BACKUPS + 1))
         for backup in $(echo "$backup_list" | head -n $backups_rm_number); do 
         for backup in $(echo "$backup_list" | head -n $backups_rm_number); do 
             backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar.*$//")
             backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar.*$//")
-            if [ -z $deprecated ]; then deprecated="$backup_date"; else deprecated="$deprecated $backup_date"; fi
             echo -e "$(date "+%F %T") Roated sftp backup: $backup_date"
             echo -e "$(date "+%F %T") Roated sftp backup: $backup_date"
             msg="$msg\n$(date "+%F %T") Roated sftp backup: $backup_date"
             msg="$msg\n$(date "+%F %T") Roated sftp backup: $backup_date"
             sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
             sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
@@ -836,7 +826,6 @@ sftp_backup() {
         sftpc "cd $BPATH" "put $user.$DATE.tar" > /dev/null 2>&1
         sftpc "cd $BPATH" "put $user.$DATE.tar" > /dev/null 2>&1
         rm -f $user.$DATE.tar
         rm -f $user.$DATE.tar
     fi
     fi
-    echo -e "$(date "+%F %T") Upload complete"
 }
 }
 
 
 echo "-- SUMMARY --"
 echo "-- SUMMARY --"

+ 1 - 1
bin/v-delete-backup-ftp-host

@@ -34,7 +34,7 @@ rm -f $VESTA/conf/ftp.backup.conf
 # Update vesta.conf
 # Update vesta.conf
 bckp=$(echo "$BACKUP_SYSTEM" |\
 bckp=$(echo "$BACKUP_SYSTEM" |\
     sed "s/,/\n/g"|\
     sed "s/,/\n/g"|\
-    sed "s/ftp//" |\
+    sed "s/^ftp$//" |\
     sed "/^$/d"|\
     sed "/^$/d"|\
     sed ':a;N;$!ba;s/\n/,/g')
     sed ':a;N;$!ba;s/\n/,/g')
 sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
 sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf

+ 0 - 1
bin/v-delete-backup-sftp-host

@@ -40,7 +40,6 @@ bckp=$(echo "$BACKUP_SYSTEM" |\
 sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
 sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
 
 
 # Logging
 # Logging
-echo "sftp host successfulle removed from configuration"
 log_event "$OK" "$EVENT"
 log_event "$OK" "$EVENT"
 
 
 exit
 exit