SharePoint Farm Backup using Server Side Object Model

So this is old school stuff but maybe it helps somebody someday. I know that SharePoint 2010 and it’s Server Side Object Model are pretty much out of fashion.
In one of our client’s SharePoint 2010 farm, we had to schedule the SharePoint Farm backup script. The purpose of the script was to take the full farm
backup weekly and the incremental farm backup on a daily basis. As this was a big farm, so we got a shared path with huge space and that can be accessed
through every machine on the farm. The purpose of the shared path for farm backup is to run the backup process without any issue. We needed to create
a PowerShell script with command Backup-SPFarm and provide the required parameters. So far so good. When we ran the PowerShell script through the console,
it ran without any issue. We scheduled the job in windows scheduler and ran the job again. After some time, scheduler told us that job ran successfully without any issue but nothing happened (No backup files were created). We tried several times but the same thing happened again and again. After struggling couple of hours, we figured out that this problem only occurs when you use network drive paths in PowerShell script and schedule the script with Windows task scheduler (Especially in Windows Server 2012). Unfortunately, we couldn’t find any concrete solution to fix the issue.

So there was the only way left, Create a console application and using SharePoint 2010 Server Side Object Model to achieve the objective. So below is the
code snippet for same:

using System;
using Microsoft.SharePoint.Administration.Backup;

namespace Farm_Backup
{
class Program
{
static void Main(string[] args)
{

// Please replace Machine-Name with valid path.
string backupLocation = @”\\Machine-Name\SPBackup\Farm-Backup\”;

string backupType = SPBackupMethodType.Full.ToString();//For differential backup use, SPBackupMethodType.Differential.ToString();

// This creates a usable set of backup settings needed to get a backup operation
// going. The resultant settings object is a combination of the assigned
// values for location and method plus default values for backup thread
// count, content + configuration backup, and so on.
SPBackupSettings backupSettings = SPBackupRestoreSettings.GetBackupSettings(backupLocation, backupType);
// Create (but don’t start) the backup job. This registers the job with
// the SPBackupRestoreConsole. Future references to the job are done using the
// GUID that is returned.
Guid backupGuid = SPBackupRestoreConsole.CreateBackupRestore(backupSettings);
try
{
// The SPBackupRestoreConsole can execute only one job at a time, so
// this call is a way of bringing the SPBackupRestoreConsole to focus
// on the new backup job. If another job is running, the call fails
// and an exception is thrown.
if (!SPBackupRestoreConsole.SetActive(backupGuid))
{
throw new Exception(“Backup or restore already in progress.”);
}
// Execute a full-farm backup synchronously. The call won’t
// return until the backup is complete.
if (!SPBackupRestoreConsole.Run(backupGuid, null))
{
throw new Exception(“Full farm backup failed. Check spbackup.log.”);
}
}
finally
{
// After completing the backup job, remove the associated job GUID.
SPBackupRestoreConsole.Remove(backupGuid);
}
}
}
}

Happy SharePointing 🙂