Retain ConfigMgr Backups

The backup maintenance task in ConfigrMgr is great. It gives you everything you need to restore your site. The only problem with it is that it only retains one copy of the backup, so you can’t go back days, weeks, or months to restore your environment. You can only go back to your most recent backup. Usually that’s OK, but what if that backup is corrupted, or it didn’t get a clean copy of the database? Then you could be left in a situation where you cannot restore your site.

Here is a simple PowerShell script that will keep (by default) three backups:

# create PowerShell drive for backup directory
new-psdrive -name backup -PSProvider FileSystem -root “<backup drive>”

# number of days to retain backups
$retain_count = 3

# get and format date
$date = get-date -format yyyyMMdd

rename-item backup:\<site code>Backup <site code>Backup-$date

# remove old backups
If ( (get-childitem -path backup:\).count -gt $retain_count ) {
Do { get-childitem backup:\ | select-object -first 1 | remove-item -recurse -force }
Until ( (get-childitem -path backup:\).count -eq $retain_count )
}

To change the retention, modify the line “$retain_count = 3” to the number of backups that you want to retain. For example, if I wanted to retain 7 backups, it would read:

$retain_count = 7

Also be sure to input the correct information where the text is red. First, the drive where you want the backup stored. This should match the backup location you specify in the Backup Site Maintenance task.

For the rename-item line, if my site code were 787, the line would be typed like this:

rename-item backup:\787Backup 787Backup-$date

Finally, to execute the PowerShell script after a backup, you need to modify the AfterBackup.bat file, located on your primary site server at “<ConfigMgr InstallDir>\inboxes\smsbkup.box”. Add this to the file:

@ECHO OFF

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noexit -command “. ‘<full path to PowerShell script>'”

Be sure to replace the part in red with the path to your PowerShell script. For example:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -noexit -command “. ‘D:\scripts\retain-sccm-backups.ps1′”

Disclaimer
All content provided on this blog is for information purposes only. Windows Management Experts, Inc makes no representation as to accuracy or completeness of any information on this site. Windows Management Experts, Inc will not be liable for any errors or omission in this information nor for the availability of this information. It is highly recommended that you consult one of our technical consultants, should you need any further assistance.

Share:

Facebook
Twitter
LinkedIn

Contact Us

On Key

More Posts

Mastering Azure AD Connect - A Comprehensive Guide by WME
Active Directory

Mastering Azure AD Connect – A Comprehensive Guide

Modern businesses are fast moving toward cloud-based infrastructure. In fact, cloud-based business is not just a trend anymore but a strategic necessity. Microsoft’s Azure Active Directory (Azure AD) has become a frontrunner in this domain. It

Read More »
Security Best Practices in SharePoint
Office 365

Security Best Practices in SharePoint

Microsoft SharePoint is an online collaboration platform that integrates with Microsoft Office. You can use it to store, organize, share, and access information online. SharePoint enables collaboration and content management and ultimately allows your teams to

Read More »
The Ultimate Guide to Microsoft Intune - Article by WME
Active Directory

The Ultimate Guide to Microsoft Intune

The corporate world is evolving fast. And with that, mobile devices are spreading everywhere. As we venture into the year 2024, they have already claimed a substantial 55% share of the total corporate device ecosystem. You

Read More »
Protecting Microsoft 365 from on-Premises Attacks
Cloud Security

How to Protect Microsoft 365 from On-Premises Attacks?

Microsoft 365 is diverse enough to enrich the capabilities of many types of private businesses. It complements users, applications, networks, devices, and whatnot. However, Microsoft 365 cybersecurity is often compromised and there are countless ways that

Read More »
Be assured of everything

Get WME Services

Stay ahead of the competition with our Professional IT offerings.