Thursday, 17 March 2011

Powershell: Copy those backups

I blogged earlier this year that i'd endeavour to make more use of Powershell and here is my first implementation of a powershell script.

I wanted to copy my SQL Backups to an offsite location and thought i'd harness the power of Powershell to achieve this. I have a simple SQL Agent Job with a step which backs up my files to a drive. Once completed, the second step (a powershell step) copies the files to the remote server and a final step to remove files older than 5 days.

Step 1:

A SQL maintenance plan which backups the files but I won't be showing whats involved there. I really want to get down in text the content of my Powershell scripts:

Step 2:

copy-item "F:\SQLBackup\*" "\\MyRemoteServer\SQLBackups" -Force -Recurse

Step 3:

$files = gci \\MyRemoteServer\SQL
foreach ($file in $files)
= ((Get-Date) - $file.CreationTime).Days
if ($datediff -gt 5 -and $file.PsISContainer -ne $True)

No comments:

Post a Comment

/* add this crazy stuff in so i can use syntax highlighter