GCP Snapshot with Conditions(on the 9th snapshot delete the oldest and retain only 8)

1.


#========================================================================================
#
#Run Powershell as Administrator for the Extraction of Log files
#
#========================================================================================
 
param([switch]$Elevated)
 
function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
 
if ((Test-Admin) -eq $false)  {
    if ($elevated) {
        # tried to elevate, did not work, aborting
    } else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    exit
}
 
'running with full privileges'


 
 
2.


 
#========================================================================================
#
#Connect Powershell to Google Cloud Platform
#
#========================================================================================
 
gcloud auth login jericagra0923@gmail.com
Screenshot:



3. 


#========================================================================================
#
# Set the path and file name for PowerShell transcripts (logs) to be written to.
#
#========================================================================================
 
$LogPath = "C:\gcp-logs\"
$LogFile = Get-Date -Format FileDateTimeUniversal
$TranscriptFileName = $LogPath + $LogFile +".txt"


4.

#========================================================================================
#
# Start the transcript.
#
#========================================================================================
Start-Transcript -Path $TranscriptFileName


 
5.
#========================================================================================
#
#Set the GCP project.
#
#========================================================================================
$Project = "evident-ocean-315608"


6.
 
#========================================================================================
#
#Set the zone(s) where the disks are that you would like to take snapshots of.
#
#================================================================================
 
$Zones = "asia-northeast1-b"


Screenshots for 5 and 6


7.


#========================================================================================
#
#Record the date that the snapshots started.
#
#================================================================================
$StartTime = Get-Date
 
Screenshots:
 


8.
 
#========================================================================================
#
#Do snapshot all of the disks in the zones as well as confirm
#
#================================================================================
foreach ($Zone in $Zones) {
$DisksInZone = Get-GceDisk -Project $Project -zone $Zone | foreach { $_.Name }
 
    foreach ($Disk in $DisksInZone) {
        Write-Output "=========================================="
        Write-Output "$Zone "-" $Disk"
        Write-Output "=========================================="
        Add-GceSnapshot -project $Project -zone $Zone $Disk
        }
}
 


Screenshot:



9.
 
#========================================================================================
#
##-Check the number of snapshots generated
#
#========================================================================================
 
Write-Host "=========================================="
$snapshotM = Get-GceSnapshot | Measure-Object
Write-Host "total Number of Snapshots:" $snapshotM.Count
Write-Host "==========================================" 


Screenshot:


10.
 

#========================================================================================
#
#Delete the 9th or more snapshot generated
#
Write-Host "Delete snapshot"
#========================================================================================
       while($snapshotM.Count -gt 8){
             $SS =  Get-GceSnapshot|Select-Object Name, TimeCreated | Sort-Object  TimeCreated -Descending | select -last 1
             Remove-GceSnapshot $SS.NAME
             $CHECK = $?
 
             If (!($CHECK)){
                          Remove-GceSnapshot $SS.NAME
                    $CHECK = $?
 
                    If (!($CHECK)){
                          Write-Host "4: Abnormal termination (snapshot deletion failed twice)"
                          Write-EventLog -LogName Application -EntryType Error -Source DataBackup -EventId 4 -Message $Message4
                          exit 4
                    }
             }
             $snapshotM.Count -=1
       }
}
 
 


11.
 
#========================================================================================
#
##-Check again the number of snapshots generated
#
#========================================================================================
 
Write-Host "=========================================="
$snapshotM = Get-GceSnapshot | Measure-Object
Write-Host "total Number of Snapshots:" $snapshotM.Count
Write-Host "=========================================="
 
Screenshot:





The full script can be acquired just visit my github page:

- https://github.com/enricagra






Comments

Popular Posts