SHARE
TWEET

Untitled

a guest Mar 20th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2. set -eu
  3.  
  4. projects=$(gcloud projects list --format='value(projectId)')
  5. projectsCount=0
  6. instancesCount=0
  7. backupsCount=0
  8.  
  9. projectWithNoInstancesCount=0
  10. instancesWithNobackupsCount=0
  11.  
  12. backupsFailedCount=0
  13. backupsFailedCount=0
  14. backupsSuccessCount=0
  15.  
  16. now="$(date +%s)"
  17.  
  18. for project in $projects
  19. do
  20.   projectsCount=$(( projectsCount + 1 ))
  21.   echo "$projectsCount PROJECT $project"
  22.   instances=$(gcloud sql instances list --project $project --format='value(name)')
  23.   if [ "$instances" != "" ]
  24.   then
  25.     instanceNumber=0
  26.     for instance in $instances
  27.     do
  28.       instanceNumber=$(( instanceNumber + 1 ))
  29.       instancesCount=$(( instancesCount + 1 ))
  30.       echo "    $projectsCount-$instanceNumber INSTANCE $instance"
  31.       backups=$(gcloud sql backups list --project $project --instance $instance --format='value(id)')
  32.  
  33.       if [ "$backups" != "" ]
  34.       then
  35.         backupNumber=0
  36.         minHoursAgo=1000
  37.         hoursAgo=-1
  38.         for backup in $backups
  39.         do
  40.           backupNumber=$(( backupNumber + 1 ))
  41.           backupsCount=$(( backupsCount + 1 ))
  42.           details=$(gcloud sql backups describe $backup --project $project --instance $instance --format json)
  43.           status="$(echo $details | jq -r .status)"
  44.           endTime="$(echo $details | jq -r .endTime)"
  45.  
  46.           timeStamp="$(date -j -f '%Y-%m-%dT%H:%M:%S' ${endTime:0:19} '+%s')"
  47.           hoursAgo="$(( ($now - $timeStamp) / 3600 ))"
  48.  
  49.           if [ "$status" != "SUCCESSFUL" ]
  50.           then
  51.             backupsFailedCount=$(( backupsFailedCount + 1 ))
  52.             echo -e "        $projectsCount-$instanceNumber-$backupNumber BackupId $backup \033[31;1m$status\033[0m"
  53.             echo -e "        ==> FAILURE $project:$instance:$backup <=="
  54.             exit 1
  55.             continue
  56.           fi
  57.  
  58.           if [ $hoursAgo -lt $minHoursAgo ]
  59.           then
  60.             minHoursAgo=$hoursAgo
  61.           fi
  62.  
  63.           echo -e "        $projectsCount-$instanceNumber-$backupNumber BackupId $backup \033[32;1m$status\033[0m ($hoursAgo hr ago)"
  64.           backupsSuccessCount=$(( backupsSuccessCount + 1 ))
  65.         done
  66.  
  67.         if [ $minHoursAgo -gt 48 ]
  68.         then
  69.           echo -e "        \033[31;1mINSTANCE $instance has not backuped for at least $minHoursAgo\033[0m"
  70.           exit 2
  71.         else
  72.           echo -e "        \033[32;1m✔\033[0m  Last backup: $minHoursAgo hrs"
  73.         fi
  74.       else
  75.         echo "        ==> INSTANCE $instance has not backups"
  76.         instancesWithNobackupsCount=$(( instancesWithNobackupsCount + 1 ))
  77.       fi
  78.       echo
  79.     done
  80.   else
  81.     echo "    ==> PROJECT $project has not SQL instances"
  82.     projectWithNoInstancesCount=$(( projectWithNoInstancesCount + 1 ))
  83.   fi
  84.   echo
  85. done
  86.  
  87. echo "Projects   : $projectsCount"
  88. echo "Instances  : $instancesCount"
  89. echo "Backups    : $backupsCount"
  90. echo "  Success  : $backupsSuccessCount"
  91. echo "  Failed   : $backupsFailedCount"
  92. echo
  93. echo "projectWithNoInstancesCount: $projectWithNoInstancesCount"
  94. echo "instancesWithNobackupsCount: $instancesWithNobackupsCount"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top