Tuesday, March 13, 2012

Backup the SCCM Backup with AfterBackup.bat

It's always better and a preventive thought to Backup the Daily SMS/SCCM Backup, (keeping a daily copy on the server & mirroring the backups to an alternate server). I have seen people trying to add custom scripts in Scheduled tasks to copy/move the Backup data to a Remote server. But in the process of SCCM's daily backup (visible in smsbkup.log), there's an 'AfterBackup.bat' file used to perform post-backup actions automatically after the Backup Site Server maintenance task runs successfully. By default, the AfterBackup.bat batch file does not exist. To use it, you must first create the batch file and add commands.

To create the AfterBackup.bat batch file:
  1. Prepare an ASCII file with commands that archive your backup snapshot, run a third-party archive tool, or perform any other post-backup tasks your site requires.
  2. Name the file AfterBackup.bat, and save it in the *ConfigMgrInstallPath*\inboxes\smsbkup.box folder
Here is a simple script for 'AfterBackup.bat' written by 'Garth Jones', which saves 7 days backup to a folder named the day of the week, and also deletes/overwrites the older ones.

************************************************************************************************** 
REM @echo off
Setlocal enabledelayedexpansion
Set target=\\Destination_Servername\E$\Primary_SCCM_Backup\%date:~0,3%
If not exist %target% goto datacopy
RD %target% /s /q
:datacopy
xcopy "\\Source_Servername\SCCM_BackupPath\*" "%target%\" /E /-Y
***************************************************************************************************
where, *Source_Server is the Primary Server from which the backup has to be copied,
and *Destination_Server is the Remote Server where you want to copy the SiteBackup.

* You can also replace the word 'copy' to 'move' if you want to move the files instead of copy.
* You can also remove the part "\%date:~0,3%" from the script if you only require present day's backup. In my case we were running out of space on one backup server to save 7 days stuff, hence i removed that date part on the script.


[Although the intended use of AfterBackup.bat is to archive SMS backup snapshots, you can use that file for other tasks that you need to perform at the end of every back up operation, such as: 
  • Run a SQL Server DBCC test to verify that there are no integrity problems with the SMS site database.
  • Run a site health tool, or other health tools.]
Update: I have created a simple document with snapshots on how to implement the AfterBackup.bat script in your SCCM infrastructure. Click the below link to download the document for reference.

Backing up SCCM Backup - Download Link - https://sites.google.com/site/ucrajee/welcome/attachments/Backing%20up%20SCCM%20Backup.pdf?attredirects=0&d=1


Process Ref: http://technet.microsoft.com/en-us/library/cc180424.aspx
Script Ref: http://smsug.ca/blogs/garth_jones/archive/2011/01/13/afterbackup-bat.aspx

5 comments:

  1. You've got Source_Server and Destination_Server backwards!

    ReplyDelete
    Replies
    1. Oops i didnt notice that. Thanks for intimating that. I have corrected the script. Keep visiting, I value your comments.

      Delete
  2. @ECHO OFF
    CLS

    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :: ::
    :: Creating Alternate "Son" BackupSet Monday through Sunday every week ::
    :: Iteration: Monday through Sunday ::
    :: Backup Horisont: 1 Week/Last backup ::
    :: ::
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    SetLocal
    Set SourceDir1="D:\Drift\Backup\ConfigMgr_Site_Backup"
    Set TargetDir1="\\CPH-SCCM01\d$\Drift\Backup\_ConfigMgrDB_Backup"
    REM Set TargetDir1="D:\Drift\Backup\_ConfigMgrDB_Backup"

    Set SourceDir2="\\CPH-SCCM01\Soft$\Apps"
    Set TargetDir2="\\CPH-SCCM01\d$\Drift\Backup\_ConfigMgr_Software_Backup"

    Set Archiver=D:\Drift\Tools\7-Zip\7z.exe

    :: Getting the current day of the week for the Iteration TimeStamp
    FOR /F "tokens=1 delims= " %%a IN ('echo %date%') DO set Iteration=%%a

    ::------------------------------------------------------------------------------------------------------------

    :: Delete DB Backups older than 3 days.
    NET USE X: \\CPH-SCCM01\D$
    Forfiles -p X:\Drift\Backup\_ConfigMgrDB_Backup\ -s -m *.* -d -3 -c "cmd /C del @PATH" 1>NUL 2>&1
    NET USE X: /d /y

    :: Creating Backuparchive with ireration - SiteDB
    "%Archiver%" a -mx9 -t7z -r "%TargetDir1%\ConfigMgr DB Backup %Iteration%.7z" "%SourceDir1%\LABBackup"

    ::------------------------------------------------------------------------------------------------------------

    :: Delete Software Backups older than 3 days.
    NET USE X: \\CPH-SCCM01\D$
    Forfiles -p X:\Drift\Backup\_ConfigMgr_Software_Backup\ -s -m *.* -d -3 -c "cmd /C del @PATH" 1>NUL 2>&1
    NET USE X: /d /y

    :: Creating Backuparchive with ireration - Software
    "%Archiver%" a -mx9 -t7z -r "%TargetDir2%\ConfigMgr Software Backup %Iteration%.7z" "%SourceDir2%"

    ::------------------------------------------------------------------------------------------------------------

    EndLocal

    GOTO EOF

    :EOF

    ReplyDelete
  3. Jeg har set en masse af disse AfterBackup.bat, men manglede en der havde iteration. det her script er mit bud på en fornuftig løsning :o)

    ReplyDelete
    Replies
    1. Thanks for your valuable comment with the script. I'll try your script in my lab and let you know my feedback here.

      Delete