Tuesday 18 September 2012

CloudBerry PowerShell S3 Scripting


Here is a quick tutorial on how to use the CloudBerry PowerShell snap in to upload files to S3. As well as simple file uploading we also look at a few other nifty options which might make life easier for you:

 - Uploading only the most recent file in a directory (useful for uploading last nights backup)
 - Appending a time stamp to your file name
 - Using chucks to upload files greater than 5GB


REM Be sure to add the snap in
Add-PSSnapin CloudBerryLab.Explorer.PSSnapIn

REM Your S3 key and secret go in here
$key = "your_key"
$secret = "your_secret"

REM Nominate the directory containing the file you want to upload
$Dir = get-childitem "C:\backups\" 

REM Sort by LastWriteTime to ensure the most recently modified file is at the top of the list
$List = $Dir | where {$_.extension -eq ".zip"} | Sort LastWriteTime -descending | select -first 1 | select name

REM A little (and perhaps unnecessary) loop to get the name of the file
$backup_name = ""
foreach ($file in $List) { 
$backup_name = $file.name 
}

REM Create the date string and append to the backup file name
#$date_string = Get-Date -uformat "%Y_%m_%d"
#$backup_name = "My_Backup" + "_" + $date_string + ".zip"

echo $backup_name

REM Set to UseChunks, without this you cannot upload files over 5GB
Set-CloudOption -UseChunks 1

REM Now just upload it!
$s3 = Get-CloudS3Connection -Key $key -Secret $secret
$destination = $s3 | Select-CloudFolder -path "s3.bucket.name/backups"
$src = Get-CloudFilesystemConnection | Select-CloudFolder " C:\backups\"
$src | Copy-CloudItem $destination -filter $backup_name

1 comment:

  1. Hi!,

    Excuse me, do you have a real example for this line?:
    $destination = $s3 | Select-CloudFolder -path "s3.bucket.name/backups"

    I want to know how I must use my s3 bucket name. Thanks!

    ReplyDelete