• 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 $_ }

  • 相关阅读:
    postgresql查询栅格数据范围(extent)
    raster导入postgres Windows命令
    Python使用XML操作mapnik,实现复杂标注(Multi line text symbolizer)
    Leaflet使用vector tiles 标注label设置
    Leaflet使用vector tiles样式设置
    Leaflet调用geoserver发布的矢量切片
    java 生成透明背景图片
    java 用RGB生成图片动态命名
    POI拆分单元格,并设置拆分后第一个cell的值为空cell的值
    洛谷 P1003 铺地毯 题解
  • 原文地址:https://www.cnblogs.com/teamleader/p/2115628.html
Copyright © 2020-2023  润新知