Monday, April 2, 2012

Powershell SiteCollection Backup script,Move files, Delete Files and Send Emails

We needed a powershell script for a SiteCollection backup job that does the following:

- Backup the Site Collection to a local drive

- Move the locally copied .bak file to a network file share backuped up to tape

- Delete the last file copied over a certain number of days ago, so that only a certain number of files were retained

- Send an email about success/failure of the job

The following powershell script that does all of the above:

1: Add-PsSnapin Microsoft.SharePoint.Powershell– ?ErrorAction SilentlyContinue
2:  
3: Start-SPAssignment -Global            # This cmdlet takes care of the disposable objects to prevent memory leak.
4:  
5: $srcSite=""                   # Replace with your site collection URL

6: $localBkUpLocation=""         # Replace with your local backup location
7: $destBkupLocation=""          # Replace with your destination backup location
8: 
9: $today=Get-Date -format "MM-dd-yyyy HH.mm.ss"     # Get current date and format it to avoid invalid characters such as "/" and ":"
10: $filename = ""  + $today
11: 
12: write-Host Start backing up $srcSite to $localBkUpLocation
13: try 
14: {
15:         #BackUp Site Collection
16:         Backup-SPSite -Identity $srcSite -Path $localBkUpLocation\\$filename.bak -force -ea Stop
17:  
18:         # move all backup files from the localBkUpLocation to the destBkupLocation
19:         Write-Host "Moving backups from $localBkUpLocation to $destBkupLocation" 
20:         Move-Item ($localBkUpLocation + "\\*.bak" ) $destBkupLocation
21:                 
22:         #Delete backups from Fileshare older then 10 days
23:         $Now = Get-Date
24:         $Days = "10"  
25:         $LastWrite = $Now.AddDays(-$days)
26:         $Files = get-childitem $destBkupLocation -include *.bak -recurse | Where {$_.LastWriteTime -le "$LastWrite" } 
27:         
28:         if  (!$Files) 
29:         { 
30:             Write-Host "variable is null"  
31:         }
32:         else 
33:         {
34:       
35:         foreach  ($File in  $Files)
36:         {
37:          write-host "Deleting File $File"  -foregroundcolor "Red" ;  Remove-Item $File | out -null 
38:         }
39:         }
40: 
41:         write-Host Backup succeeded.
42: 
43:          # Edit the From Address as per your environment.
44:           $emailFrom = "" 
45:          # Edit the mail address to which the Notification should be sent.
46:           $emailTo = "" 
47:          # Subject for the notification email. The +“” ? part will add the date in the subject.
48:           $subject = "The  Site Collection Backup file was created with name:  " +"$filename"  + "$result" 
49:          # Body or the notification email. The +“” ? part will add the date in the subject.
50:           $body = "The  Site Collection Backup file was created with name: " +"$filename" 
51:           # IP address of your SMTP server. Make sure relay Is enabled for the SharePoint server on your SMTP server
52:           $smtpServer = "" 
53:           $smtp = new -object  Net.Mail.SmtpClient($smtpServer)
54:           $smtp.Send($emailFrom, $emailTo, $subject, $body)
55: }
56: catch         # If the process failed
57: {
58:           $ErrorMessage = $_.Exception.Message
59:           # Configure the below parameters as per the above.
60:           $emailFrom = "" 
61:           $emailTo = "" 
62:           $subject = "The Site Collection Backup Job failed on " +"$today" 
63:           $body = "The Site Collection Job failed on " +"$today and the reason for failure was $ErrorMessage." 
64:           $smtpServer = "" 
65:           $smtp = new -object  Net.Mail.SmtpClient($smtpServer)
66:           $smtp.Send($emailFrom, $emailTo, $subject, $body)
67:   
68: }    
69:  
70: Stop-SPAssignment -Global
71:  
72: Remove-PsSnapin Microsoft.SharePoint.PowerShell
73:  
74: write-Host "Finished script."
75: 

1 comment:

Isabella Jackson said...

The standard casino cannot be played anywhere except the places where they're situated. Author is an expert of best sportsbook malaysia, visit here for more interesting information.