zoukankan      html  css  js  c++  java
  • BackupAllSPSites

    $backupLocation = "D:\SPBACKUP\SiteCols"
    $logFile = join-path $backupLocation "log.txt"

    cd $backupLocation

    function Write-Log($message)
    {
     $message
     $message = (get-date).ToString("yyyy/MM/dd HH:mm:ss") + " - " + $message
     $message >> $logFile
    }

    $source = @"
    using System;

    public class HistoryBackup
    {
     public DateTime CreatedDate;
     public DayOfWeek WeekDay;
     public int DaysOld;
     public int WeeksOld;
     public string FullFileName;
     
     public HistoryBackup(string fileName)
     {
      FullFileName = fileName;
      int idx = System.Text.RegularExpressions.Regex.Match(fileName, @"_\d{12}\.").Index; //xxx_201107272300.bak 3 4, 12
      CreatedDate = DateTime.ParseExact(fileName.Substring(idx + 1, 8), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
      WeekDay = CreatedDate.DayOfWeek;
      DaysOld = (int) DateTime.Today.Subtract(CreatedDate).TotalDays;
      WeeksOld = GetWeekDiff(DateTime.Today, CreatedDate);
     }
     
     private int GetWeekDiff(DateTime today, DateTime adate)
     {
      int todayWeekNum = (int) today.DayOfWeek;
      DateTime firstDayofThisWeek = today.AddDays(todayWeekNum * -1);
      int x = (int) firstDayofThisWeek.Subtract(adate).TotalDays;
      if (x <= 0)
      {
       return 0;
      }
      if (x % 7 > 0)
      {  
       return (x / 7) + 1;
      }
      else
      {
       return x / 7;
      }
     }
     
     public bool Dispose()
     {
      if (WeeksOld == 1 && (WeekDay == DayOfWeek.Friday || WeekDay == DayOfWeek.Wednesday))
      {
       return false;
      }
      System.IO.File.Delete(FullFileName);
      return true;
     }
    }
    "@
        
    Add-Type -TypeDefinition $source

    function BackupSiteCollection($siteCol)
    {
     $datePart = (get-date).tostring("yyyyMMddHHmm")
     $namePart = $siteCol.Url.ToLower().replace("https://", "").replace("http://", "").replace(":", "-").replace("/", "-").replace(".", "-")
     $fileName = $namePart + "_" + $datePart + ".bak"
     $fullName = join-path $backupLocation $fileName
     
     $url = $siteCol.Url
     
     Write-Log "Start to backup $url to $fullName"
     Backup-SPSite -identity $siteCol.Url -path $fullName -force
     Write-Log "Finish backup of $url"
     
     dir | ? { $_.Name -like "$namePart" + "_????????????.bak" -and $_.Name -ne $fileName} |
      % {
           $hisBackup = new-object HistoryBackup -ArgumentList $_.FullName
           $deleted = $hisBackup.Dispose()
           if ($deleted)
           {
            Write-Log "$_.Name is deleted."
           }
        }
    }

    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null) {

        Write-Host "Loading SharePoint Powershell Snapin"
        Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }


    get-spsite | % { BackupSiteCollection $_ }

  • 相关阅读:
    二次剩余
    【2020.9.29 NOIP模拟赛 T3】寻梦(fantasy)
    Graph and Queries
    势函数和鞅的停时定理学习笔记
    毒瘤计数题汇总
    2-SAT
    CF559E Gerald and Path
    [SDOI2019]世界地图
    CF1349D Slime and Biscuits
    AT4928 [AGC033E] Go around a Circle
  • 原文地址:https://www.cnblogs.com/teamleader/p/2115628.html
Copyright © 2011-2022 走看看