Browse Source

Added check login conditions, check list devices

pmacko 1 week ago
parent
commit
a6e5e45199
1 changed files with 25 additions and 7 deletions
  1. 25 7
      gbackup.sh

+ 25 - 7
gbackup.sh

@@ -2,7 +2,7 @@
 #########################################################################
 #         Script for global backup main folders of ARM devices          #
 #                         Created by PM-DoIT                            #
-#                               ver 0.1                                 #
+#                               ver 0.2                                 #
 #########################################################################
 
 #----------------------------- VARIABLES -------------------------------#
@@ -12,12 +12,13 @@ DAT=$(date +%y%m%d)
 SRV=/etc/hosts
 TMP=/tmp/hosts
 BCK=/opt/backup
+clear
 
 #---------------------------- CREDENTIALS ------------------------------#
 source /root/.credentials
 
-#------------------------------ CHECK ----------------------------------#
-check(){
+#---------------------------- CHECK-PING -------------------------------#
+checkping(){
 test -f $TMP && rm -rf $TMP
 cat $SRV | awk '{print $2}' | egrep "$CAT" | grep "$READSITE" | sort | uniq | while read output
 do
@@ -28,17 +29,33 @@ do
                         echo -e "- $output is \e[1;31mDOWN\e[0m"
                 fi
 done
+test -f $TMP || echo -e "! Devices \e[1;31mNOT\e[0m exist in LIST definition"
+test -f $TMP || exit 1
+}
+
+#---------------------------- CHECK-CRED -------------------------------#
+checkcred(){
+echo -e "Starting \e[1;32mcheck\e[0m [\e[1;37mlogin\e[0m] to devices:"
+for i in `cat $TMP`
+do
+status=$(sshpass -p $password timeout 10 ssh -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no -o ConnectTimeout=5 $localuser@${i} 2> /dev/null || echo NO)
+        if [[ "$status" == "NO" ]]; then
+                echo -e "* \e[1;32m$i\e[0m auth \e[1;31mFAIL\e[0m"
+		sed -i "/$i/d" $TMP
+        else
+                echo -e "* \e[1;32m$i\e[0m auth \e[1;32mOK\e[0m"
+        fi
+done
 }
 
 #------------------------------ BACKUP ---------------------------------#
 backup(){
 test -d $BCK || mkdir $BCK
-echo -e "Starting backup [\e[1;33mmain\e[0m] folders:"
+echo -e "Starting \e[1;32mbackup\e[0m [\e[1;33mmain\e[0m] folders:"
 for i in `cat $TMP`
 do
 echo -e "- \e[1;32m$i\e[0m start backup [\e[1;33metc\e[0m]"
 sshpass -p $password ssh -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no $localuser@${i} 2> /dev/null "sudo -S tar cf /opt/etc.tar /etc > /dev/null 2>&1"
-
 echo -e "- \e[1;32m$i\e[0m download backup [\e[1;33metc\e[0m]"
 sshpass -p $password scp -r -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no -o LogLevel=quiet $localuser@${i}:/opt/etc.tar /opt/backup/${i}_etc_$DAT.tar > /dev/null 2>&1
 echo -e "- \e[1;32m$i\e[0m start backup [\e[1;34mhome\e[0m]"
@@ -56,12 +73,13 @@ done
 #------------------------------ BACKUP ---------------------------------#
 status(){
 SZ=$(du -h -d1 /opt/ | grep backup | awk '{print $1}')
-echo -e "- Removing [\e[1;33m$DAY\e[0m] old days backups"
+echo -e "- Starting \e[1;32mremove\e[0m [\e[1;33m$DAY\e[0m] old days backups"
 find $BCK/* -mtime +$DAY -exec rm {} \;
 echo -e "- Backups saved to [\e[1;36m/opt/backup\e[0m] size [\e[1;33m$SZ\e[0m]"
 }
 
 #------------------------------- CORE ----------------------------------#
-check
+checkping
+checkcred
 backup
 status