Backup Folders to Amazon S3 using CloudBerry S3 and Windows PowerShell

My backups procedures on my computer are mostly automated. I have scripts that collect my data from across my computer and the internet (IM Logs, Emails, SMS etc) and copies them into one centralised location on my machine. This folder is then kept mirrored across all available local HDD’s. However, I also take the smaller files (i.e. excluding photos and videos) and make daily differential backups. This allows me to upload this data off-site to Amazon S3 (where I subsequently keep data mirrored across three buckets in three geographical regions – US, Europe and Asia Pacific). However, I would normally have to manually initiate the upload of the differential backups to Amazon S3. In order to better automate this procedure, I took advantage of CloudBerry S3 Explorer and its PowerShell extensibility.

Configuring PowerShell
Running unsigned scripts is disallowed with PowerShell by default. To fix this, run the following command in PowerShell: (Note: Please read up on what unrestricted means before doing this.)

Set-ExecutionPolicy Unrestricted

Configuring the Cloudberry SnapIn
You will need to register the Cloudberry SnapIn with Windows PowerShell. Open PowerShell, navigate to your CloudBerry folder and run one of the following commands depending on your OS:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil.exe CloudBerryLab.Explorer.PSSnapIn.dll (x86)
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\installutil.exe CloudBerryLab.Explorer.PSSnapIn.dll (x64)

The Folder Backup Script
You can now use the following code in a PowerShell script (.ps1 file) to initiate folder backups to Amazon S3: (Remember to customise the target bucket, source folder location, and S3 AccessID/Secret Keys)

#Register the CloudBerry Extension.
Add-PSSnapin CloudBerryLab.Explorer.PSSnapIn

#Copy ChatLogs_S3
$local = Get-CloudFileSystemConnection
$source = $local | Select-CloudFolder C:\Logs
$s3 = Get-CloudS3Connection YourS3KeyHere YourS3SecretHere
$target = $s3 | Select-CloudFolder -Path yourbucket/logs
$source | Copy-CloudSyncFolders $target

Calling PowerShell From Batch/CMD
You can call a PowerShell script from a batch script or command prompt as follows:

powershell.exe C:\Logs\s3_upload.ps1 (Absolute Path)
powershell.exe .\s3_upload.ps1 (Relative Path)

References: [1], [2]

Leave a Reply

Your email address will not be published. Required fields are marked *