Last active
December 30, 2015 21:29
-
-
Save breiter/7887782 to your computer and use it in GitHub Desktop.
Script to back up a Windows EBS volume on EC2 to S3 by snapshot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#backup a disk by snapshot to S3. | |
param | |
( | |
[string] $path, #directry to clean of old files | |
[int] $diskNumber, | |
[string] $ec2VolumeId, #volumn arn in ec2 | |
[string] $description, | |
[int] $days = 2 | |
); | |
function aws { & 'C:\Program Files\Amazon\AWSCLI\aws.exe' $args; } | |
function Snapshot-Disk([int] $diskNumber, [string] $ec2VolumeId, [string] $description) | |
{ | |
#un-mount drive before snapshot | |
UnMount-Disk $diskNumber; | |
Write-Host "aws ec2 create-snapshot --volume-id $ec2VolumeId --description $description" | |
$result = (aws ec2 create-snapshot --volume-id $ec2VolumeId --description $description) -join "`n" | ConvertFrom-Json; | |
#poll until job is compmleted | |
while((Get-SnapshotProgress $result.snapshotId) -lt 100) | |
{ | |
Write-Host "Sleep 60 seconds."; | |
Start-Sleep 60; #wait 1 minute | |
} | |
#mount drive again | |
Mount-Disk $diskNumber; | |
} | |
function Get-SnapshotProgress([string] $snapshotId) | |
{ | |
Write-Host "Get-SnapshotProgress $snapshotId." | |
#result is in JSON format. It gets automatically split into a string[] by powerhsell on newlines | |
#when it retrieves the return value from the aws cli process. | |
#convert array back to string in order to pass to ConvertFrom-Json | |
$result = (aws ec2 describe-snapshots --snapshot-ids $snapshotId) -join "`n" | ConvertFrom-Json; | |
$progress = 0; | |
if($result.Snapshots.Progress) { | |
$progress = [int]$result.Snapshots.Progress.TrimEnd('%'); | |
} | |
Write-Host "Progress: $progress%."; | |
return $progress; | |
} | |
function UnMount-Disk([int] $diskNumber) | |
{ | |
Write-Host "Unmount disk $diskNumber."; | |
Get-Disk -Number $diskNumber | Set-Disk -IsOffline:$true; | |
} | |
function Mount-Disk([int] $diskNumber) | |
{ | |
Write-Host "Mount disk $diskNumber."; | |
Get-Disk -Number $diskNumber | Set-Disk -IsOffline:$false; | |
Get-Disk -Number $diskNumber | Set-Disk -IsReadONly:$false; | |
} | |
function Remove-OldFiles( | |
[string] $Path = ".", # directory to clean | |
[int] $Days = 2, #days of grace. files older than $Days will be deleted | |
[Switch] $Whatif | |
) | |
{ | |
Write-Host "Clean files from $Path over $Days days old." | |
$now = [DateTime]::Now; | |
Get-ChildItem $Path -Recurse | Foreach-Object { | |
if( !$_.PsIsContainer -and ($now-$_.LastWriteTime).Days -ge $Days ){ | |
if( $Whatif ) { | |
Write-Output "Remove-Item $($_.FullName)"; | |
} else { | |
Remove-Item $_.FullName; | |
} | |
} | |
} | |
} | |
& { | |
Import-Module Microsoft.PowerShell.Utility | |
Write-Host "Begin: $(Get-Date)"; | |
Snapshot-Disk $diskNumber $ec2VolumeId $description; | |
Remove-OldFiles $path $days; | |
Write-Host "End: $(Get-Date)"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Use
Script Execution
Run as a scheduled job like this (where -path is the Windows filesystem path, -diskNubmer is the mount number in Windows and -ec2VolumneId is the EBS volume in EC2):