Skip to content

Instantly share code, notes, and snippets.

@dqduc
Forked from allenk1/BackuptoS3_Snapshots
Created October 28, 2015 03:53
Show Gist options
  • Save dqduc/85c2ac80af07fa770f67 to your computer and use it in GitHub Desktop.
Save dqduc/85c2ac80af07fa770f67 to your computer and use it in GitHub Desktop.
Powershell script to backup flat files to S3 and then create EBS snapshots
# +---------------------------------------------------------------------------
# | File : BackuptoS3_Snapshots.ps1
# | Version : 1.0
# | Purpose : Backs up to S3 & creates EBS snapshots
# | Synopsis:
# | Usage : .\BackuptoS3_Snapshots.ps1
# +----------------------------------------------------------------------------
# |
# | File Requirements:
# | Must have AWS S3 CLI installed & Powershell tools
# | CLI - https://s3.amazonaws.com/aws-cli/AWSCLI64.msi
# | PS Tools - http://aws.amazon.com/powershell/
# +----------------------------------------------------------------------------
# | Maintenance History
# | View GitHub notes: https://github.com/allenk1/ISO-Scripts/commits/master/BackuptoS3_Snapshots.ps1
# ********************************************************************************
# Default input params
$access = "AKIAJGXXXXXXXXXXXXXX"
$private = "ABC123123ABC123123ABC123123ABC123123ABCD"
$vol_id = @("vol-XXXXXXXX", "vol-XXXXXXXX")
$servername = "Server_NAME"
$region = "us-west-2" # Regions: us-east-1, us-west-2, us-west-1, eu-west-1, ap-southeast-1
# ap-southeast-2, ap-northeast-1, sa-east-1
# $a = Get-Date
# $date = $a.Year + "_" + $a.Month + "_" + $a.Day #YYYYMMDD
$date = Get-Date -format s
import-module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
# Clear any saved credentials
# Clear-AWSCredentials -StoredCredentials
# Set credentials
Set-AWSCredentials -AccessKey $access -SecretKey $private
Set-DefaultAWSRegion $region
# Loop through all volumes and create snapshots
# Naming Scheme ServerName_VOLID
foreach ($vol in $vol_id) {
# snapshot the EBS store
$snapshot_name = $servername + "_" + $vol + "_" + $date
New-EC2Snapshot -VolumeId $vol -Description $snapshot_name
}
# Now flat file copy to S3
# Enable Bucket versioning in order to keep mulitple version of the file
# TODO: Version with Script
$copy_dirs = @('C:\Path\to\Backup\Directory')
$bucket = "bucketname"
foreach ($dir in $copy_dirs){
# Key setup by ServerName_DATE
$key = $servername + '_' + $date
Write-S3Object -Folder $dir -BucketName $bucket -KeyPrefix / -Recurse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment