95 lines
1.7 KiB
PowerShell
95 lines
1.7 KiB
PowerShell
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$url
|
|
)
|
|
|
|
function Write-Log($msg)
|
|
{
|
|
Write-Host $msg -foregroundcolor green
|
|
($msg) | Out-File "log-backup-doclibs.txt" -Append
|
|
}
|
|
|
|
function Get-Web($webs, $title)
|
|
{
|
|
foreach ($web in $webs)
|
|
{
|
|
if ($web.Title -eq $title)
|
|
{
|
|
return $web
|
|
}
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Get-Doclib($lists, $title)
|
|
{
|
|
foreach ($list in $lists)
|
|
{
|
|
if ($list.Title -eq $title)
|
|
{
|
|
return $list
|
|
}
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Backup-Doclib($web, $doclib)
|
|
{
|
|
Write-Log (" " + $doclib.Title)
|
|
$dirName = $web.Url.Replace("https://", "").Replace(".", "_").Replace("/", "_")
|
|
$dirName = $PSScriptRoot + "/backups/" + $dirName + "/" + $doclib.Title
|
|
[System.IO.Directory]::CreateDirectory($dirName) | Out-Null
|
|
foreach ($item in $doclib.Items)
|
|
{
|
|
$file = $item.File
|
|
Write-Log (" " + $file.Name)
|
|
$fileBytes = $file.OpenBinary()
|
|
$fileTo = $dirName + "/" + $file.Name
|
|
$fileStream = New-Object System.IO.FileStream($fileTo, "Create")
|
|
$binaryWriter = New-Object System.IO.BinaryWriter($fileStream)
|
|
$binaryWriter.Write($fileBytes)
|
|
$binaryWriter.Close()
|
|
}
|
|
}
|
|
|
|
function Backup-Web-Impl($web)
|
|
{
|
|
Write-Log ($web.Url)
|
|
|
|
foreach ($doclib in $web.Lists)
|
|
{
|
|
if ($doclib.Hidden -or $doclib.BaseType -ne 1 -or $doclib.Title -eq "Sivut")
|
|
{
|
|
continue
|
|
}
|
|
Backup-Doclib $web $doclib
|
|
}
|
|
}
|
|
|
|
function Backup-Web($web)
|
|
{
|
|
Write-Log ($web.Url)
|
|
|
|
Backup-Web-Impl $web
|
|
|
|
foreach ($subWeb in $web.Webs)
|
|
{
|
|
Backup-Web-Impl $subWeb
|
|
}
|
|
}
|
|
|
|
function Backup-Pmc($site)
|
|
{
|
|
foreach ($web in $site.RootWeb.Webs)
|
|
{
|
|
Backup-Web $web
|
|
}
|
|
}
|
|
|
|
<#$wa = Get-SPWebApplication $url
|
|
foreach ($site in $wa.Sites)
|
|
{
|
|
Backup-Pmc $site
|
|
}#>
|
|
$site = Get-SPSite $url
|
|
Backup-Pmc $site |