{"id":198,"date":"2012-10-10T21:07:30","date_gmt":"2012-10-10T15:07:30","guid":{"rendered":"http:\/\/markwheeler.com\/wordpress\/?p=198"},"modified":"2012-10-10T21:07:30","modified_gmt":"2012-10-10T15:07:30","slug":"sharepoint-2010-backup-and-cleanup-scripts-with-notifications","status":"publish","type":"post","link":"https:\/\/markwheeler.com\/?p=198","title":{"rendered":"Sharepoint 2010 Backup and Cleanup Scripts with Notifications"},"content":{"rendered":"<p>Simple scripts that you can automate with the Task Scheduler to backup\/cleanup your Sharepoint 2010 farm with email notifications.<\/p>\n<h3>Backup Script<\/h3>\n<pre>try\n {\n  $today = (Get-Date -Format dd-MM-yyyy)\n #Location of the Backup Folder\n  [IO.Directory]::CreateDirectory(\"<strong>E:BackupDailyFarmBackUp$today<\/strong>\")\n # This will actually initiate the SPFarm backup. \n  Backup-SPFarm -Directory <strong>E:BackupFullFarmBackup$today -BackupMethod full\n<\/strong> # Backup-SPFarm -Directory <strong>E:BackupDailyFarmBackup$today -BackupMethod differential<\/strong><\/pre>\n<pre> # Edit the From Address as per your environment.\n  $emailFrom = \"SPADMIN@Sharepoint.com\"\n # Edit the mail address to which the Notification should be sent.\n  $emailTo = \"Admin@SharePoint.Com\"\n # Subject for the notification email. The + \u201c$today\u201d part will add the date in the subject.\n  $subject = \"The SharePoint Farm Backup was Successful for \"+\"$today\"\n # Body or the notification email. The + \u201c$today\u201d part will add the date in the subject.\n  $body = \"The SharePoint Farm Backup was Successful for \"+\"$today\"\n  # IP address of your SMTP server. Make sure relay Is enabled for the SharePoint server on your SMTP server\n  $smtpServer = \"192.168.0.0\"\n  $smtp = new-object Net.Mail.SmtpClient($smtpServer)\n  $smtp.Send($emailFrom, $emailTo, $subject, $body)\n }\nCatch\n {\n  $ErrorMessage = $_.Exception.Message\n  # Configure the below parameters as per the above.\n  $emailFrom = \"SPADMIN@Sharepoint.com\"\n  $emailTo = \"Admin@SharePoint.Com\"\n  $subject = \"The SharePoint Farm Backup Job failed on \"+\"$today\"\n  $body = \"The SharePoint Farm Backup Job failed on \"+\"$today and the reason for failure was $ErrorMessage.\"\n  $smtpServer = \"192.168.0.0\"\n  $smtp = new-object Net.Mail.SmtpClient($smtpServer)\n  $smtp.Send($emailFrom, $emailTo, $subject, $body)\n }<\/pre>\n<h3>Cleanup Script<\/h3>\n<pre># Location of spbrtoc.xml\n$spbrtoc = \"\\YOURPATHspbrtoc.xml\"<\/pre>\n<pre># Days of backup that will be remaining after backup cleanup.\n$days = 14<\/pre>\n<pre># Load Backup File\n[xml]$sp = gc $spbrtoc<\/pre>\n<pre># Find the old backups in spbrtoc.xml<\/pre>\n<pre>$old = $sp.SPBackupRestoreHistory.SPHistoryObject |? { [datetime]$_.SPStartTime -lt ((get-date).adddays(-$days)) }<\/pre>\n<pre>\n if ($old -eq $Null) { write-host \"$old and $date - No reports of backups older than $days days found in spbrtoc.xml.`nspbrtoc.xml isn't changed and no files are removed.`n\" ; break}\n \n # Delete the old backups from the Sharepoint backup report xml file \n $old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) } \n \n # Save the new Sharepoint backup report xml file \n $sp.Save($spbrtoc) \n Write-host \"Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc.\" \n \n #Check if any error has occured and send an alert email if it did.\nif ($error[0])\n{\nWrite-Output \"An error has occurred\" | Out-File -FilePath <strong>C:temp<\/strong>backupsharepointfarm.log -Append;\nfor ($i=0; $i -le $error.count; $i++) {Write-Output $error[$i] | Out-File -FilePath<strong> C:temp<\/strong>backupsharepointfarm.log -Append}<\/pre>\n<pre>###First, the administrator must change the mail message values in this section\n$FromAddress = \"backupjob@yourserver.com\"\n$ToAddress = \"you@host.com\"\n$MessageSubject = \"Sharepoint Cleanup Failed.\"\n$MessageBody = \"The Sharepoint Server Cleanup Failed. Please check.\"\n$SendingServer = \"192.168.0.0\"<\/pre>\n<pre>###Create the mail message\n$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody<\/pre>\n<pre>###Send the message\n$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer\n$SMTPClient.Send($SMTPMessage)<\/pre>\n<pre>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple scripts that you can automate with the Task Scheduler to backup\/cleanup your Sharepoint 2010 farm with email notifications. Backup Script try { $today = (Get-Date -Format dd-MM-yyyy) #Location of the Backup Folder [IO.Directory]::CreateDirectory(&#8220;E:BackupDailyFarmBackUp$today&#8221;) # This will actually initiate the SPFarm backup. Backup-SPFarm -Directory E:BackupFullFarmBackup$today -BackupMethod full # Backup-SPFarm -Directory [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2,10,11],"tags":[99],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-computer","category-sharepoint","category-software","tag-sharepoint-2010"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":0,"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"wp:attachment":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}