Tuesday, 17 March 2015

PowerShell Script to Schedule daily Site collections backup and retention policy for backup files

PowerShell Script to Schedule daily Sites collection backup and retention policy for backup files


Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
{
$today = (Get-Date -Format dd-MM-yyyy)
 [IO.Directory]::CreateDirectory("D:\SharePointBackups\Production_$today")

Backup-SPSite -Identity http://sitea.domain.com/ -Path D:\SharePointBackups\Production_$today\SiteA.bak
Backup-SPSite -Identity http://siteb.domain.com/ -Path D:\SharePointBackups\Production_$today\SiteB.bak
Backup-SPSite -Identity http://sitec.domain.com/ -Path D:\SharePointBackups\Production_$today\SiteC.bak
Backup-SPSite -Identity http://sited.domain.com/ -Path D:\SharePointBackups\Production_$today\SiteD.bak
}
catch
{
"No sites found!!!!"
}
#Retention policy for old backup files
try
{
# set folder path
$dump_path = "D:\SharePointBackups"

# set min age of files
$max_days = "-5"

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# delete the files
Get-ChildItem $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item  -force -Confirm:$false -ErrorAction:SilentlyContinue;

}
catch
{
"No path found!!!"
}

No comments:

Post a Comment