Tools: String Manipulations Find Dupes My IP Json Formatter Lorem Ipsum More...
Discussion Options
ajax animation

Windows Powershell to Automate Backup of AWS EBS Volumes

5/1/2017 6:13:00 PM by: CodeLocker

The following Powershell script can be run or put on a schedule to regularly backup AWS EBS volumes.

Another option is to use an online service such as Stratus Tools (https://www.stratustools.net/)

View Copy: Content Copied
$AWS_AccessKey = '############'
$AWS_SecretKey = '####################################################################'
$AWS_DefaultRegion = 'us-east-1'


Set-AWSCredentials -AccessKey $AWS_AccessKey -SecretKey $AWS_SecretKey
Set-DefaultAWSRegion $AWS_DefaultRegion

# base description
$description = 'BACKUP: ' + (Get-Date).ToString('yyyy-MM-dd')

$dayOfTheWeek = (Get-Date).DayOfWeek
#$dayOfTheWeek

# an arry of volume Ids to backup
$volumeIds = 
@(    
    "vol-########" #SQL Data      
)

# only backup on Mondays
if ($dayOfTheWeek -eq "Monday")
{
	$volumeIds += "vol-########" #SQL Logs
	$volumeIds += "vol-################" #SQL Backups    
}


# loop through all volume Ids
for ($volumeIdIndex = 0; $volumeIdIndex -lt $volumeIds.Length; $volumeIdIndex++)
{
    #"This is line number " + $volumeIdIndex

    $volumeId = $volumeIds[$volumeIdIndex]

    #set base snapshot description
    $snapshotDescription = $description + " - " + $volumeId
	
    #try to get volume details to get tag name
    $volumeDetails = Get-EC2Volume -VolumeId $volumeId

    #if ($volumeDetails.Tags.Length not null)
    #{
        for ($tagIndex = 0; $tagIndex -lt $volumeDetails.Tags.Length; $tagIndex++)
        {
            #echo $volumeDetails.Tags[$tagIndex];
            if ($volumeDetails.Tags[$tagIndex].Key -eq "Name")
            {
                $snapshotDescription = $snapshotDescription + " - " + $volumeDetails.Tags[$tagIndex].Value
            }
        }
    #}

    #$volumeId
    #$snapshotDescription    

    New-EC2Snapshot -VolumeId $volumeId -Description $snapshotDescription
}
Copy: Content Copied
ajax animation
×

Get Coding

Store your first code or add more code to your collection.
You must login to report abuse. ×