We all know that ConfigMgr 2012 (aswell for previous version of ConfigMgr) comes with a site maintenance task that lets you backup the site database of either a Primary Site or a CAS. Once you have enabled this tasks, the database and all the necessary files to restore the site will be backed up to a chosen location. What’s important to point out is that the next time this tasks run, it will over write the previous data in the selected location unless it’s moved manually by an administrator. So in order to prevent that the backed up data gets over written, I’ve created a small rotating (or perhaps archiving) script that will move the backed up site data to a specified archive folder and place it accordingly into sub folders named by date. You can implement this by creating a scheduled task that runs some time after the site backup task has completed.

PowerShell code

$DailyBackupFolder = "\\fileshare\ConfigMgrBackups$\Daily"
$ArchiveFolder = "\\fileshare\ConfigMgrBackups$\Archive"
$Date = (Get-Date).ToShortDateString()
$DateKeep = (Get-Date).AddDays(-2).ToShortDateString()
$DailyFolder = "$($ArchiveFolder)\$($Date)"

if (-not(Test-Path -Path $DailyFolder)) {
	New-Item -Type Directory $DailyFolder | Out-Null
}

if ((Get-ChildItem -Path $DailyBackupFolder).Count -eq 1) {
	$BackupFolderPath = Get-ChildItem -Path $DailyBackupFolder | Select-Object -ExpandProperty FullName
	Move-Item -Path $BackupFolderPath -Destination $DailyFolder -Force
}

$ArchiveFolders = Get-ChildItem -Path $ArchiveFolder | Select-Object -ExpandProperty Name
$ArchiveFolders | ForEach-Object {
	if ($_ -lt $DateKeep) {
		Remove-Item -Path ($ArchiveFolder + "\" + $_) -Recurse -Force | Out-Null
	}
}

How to use it

It’s really simple to use this script, but keep in mind that it assumes that you’re backing up the site data every day. All you need to do is to configure the site backup maintenance task to backup to the folder specified in the script above, more specifically in the $DailyBackupFolder variable. In this case it’s:

\\fileshare\ConfigMgrBackups$\Daily

You’d also need to specify a folder where the script will move the site backup data to. That path is being stored in the $ArchiveFolder variable. In this case:

\\fileshare\ConfigMgrBackups$\Archive

Once you’ve specified the two variables and configured the site backup maintenance task, decide on how many backups (daily folders containing the site backup data) you’d like to keep. In the sample script above, the variable $DateKeep is set to 2 days. Change what’s highlighted in red below to change the number of daily backups that will be kept in the Archive folder:

$DateKeep = (Get-Date).AddDays(-2).ToShortDateString()

You could of course also just rely on your backup solutions that backups the data on your file servers, but for those who doesn’t have such a system in place this is a really good substitute for at least making sure that your site data is backed up.

Nickolaj Andersen
Principal Consultant and Enterprise Mobility MVP. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows deployments and Automation. In 2015 Nickolaj was awarded as PowerShell Hero by the community for his script and tools contributions. Author of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService and a frequent speaker at user groups.

(66)

comments
  • Torsten
    Posted at 12:39 November 3, 2013
    Torsten
    Reply
    Author

    Why not using afterbackup.bat instead of a schedulded task?

    • Nickolaj
      Posted at 13:54 November 3, 2013
      Nickolaj
      Reply
      Author

      That’s one way of doing it, yes. I like to create scheduled tasks though so that my colleagues without any ConfigMgr knowledge easily can get an overview of what’s being started and when.

      Regards,
      Nickolaj

  • Leave a Reply