ChrisPIGuy

Improving AF Backups

Blog Post created by ChrisPIGuy on Aug 10, 2020

As PI Administrators, we all know how critical it is to have a solid backup plan. During my time working with PI AF, I found an opportunity to improve the backup and restoration strategy, especially for mission critical AF Databases.

 

Currently, a PI AF server is backed up server-wide by backing up the SQL Server database. However, what if you just need to restore a single AF Database when there are multiple AF Databases on a single server? A global restore will set all AF Databases back. This could be detrimental if there are multiple individuals building objects on the same AF server.

 

Therefore, I developed a simple solution that leverages the Export capabilities of PI AF. I created a PowerShell script that would create an XML export of each AF Database on a server and store the XML files in a specified backup folder.

 

The script also removes older files, so you do not keep an indefinite number of XML files. I usually set my backup retention to 3 days. The advantage is that you can import any XML you need to restore just a single AF Database.

 

Here’s a snippet of the PowerShell script that performs the export:

    

    [System.Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK") | Out-Null

    $PISystems = New-Object OSIsoft.AF.PISystems

    $PISystem = $PISystems[$AFServer]

    foreach($afdb in $PISystem.Databases)

    {

        $exportfilename = [string]::Concat($afdb.Name,"_",(Get-Date -Format "yyyy_MM_dd_HH_mm_ss"),".xml")

        $exportfilepath = [string]::Concat($BackupFolder,"\",$exportfilename)        

        $modes = @([OSIsoft.AF.PIExportMode]::AllReferences,[OSIsoft.AF.PIExportMode]::DefaultValues,[OSIsoft.AF.PIExportMode]::NoUniqueID)

        $PISystem.ExportXml($afdb,$modes,$exportfilepath,$null,$null,$null)  

    }

 

To run the script, I simply created a Scheduled Task using a service account that has read access to all AF Databases on the server. I used the following Action settings to run the PowerShell script in the Task.

 

Program/script: powershell.exe
Arguments:

-Command “& ‘C:\<path to ps1>\Backup-AFDatabases.ps1’ -AFServer <your AF Server> -BackupFolder <your backup folder> -RetentionDays <number of days to keep backups>

 

Here’s an example of the arguments for the Tasks Scheduler:

-Command “& ‘C:\Scripts\Backup-AFDatabases.ps1’ -AFServer myAF.myDomain.com -BackupFolder E:\AFBackups -RetentionDays 3

 

If the AF server is running on the same server as the PI Data Archive, then it could be simpler to add the commands to the %PISERVER%\adm\pisitebackup.bat file so it will run each time PI is backed up.

 

I hope this will help you improve your own AF backup and restoration strategy. Does anybody have any ideas to improve this?

 

Improvement ideas aside, if you or your company have a use for this solution, don’t hesitate to reply to this blog requesting that we connect.

Outcomes