Auto-Downloading Salesforce Backups & Metadata

 In admin, crm, Salesforce, salesforce.com, tips & tricks

Salesforce can automatically generate full data backups on a weekly or monthly basis. Once created the requesting user receives an email that the files are ready and will be downloadable for 48 hours, after which they’re deleted.

The solution described below automates the download process so you can set and forget both parts – the backup and download of the export files.

It also makes a copy of your org’s metadata (configuration) for safe keeping.

Note: I recommend scheduling the downloads to run at 24 hours after the time scheduled in Salesforce to ensure the data is ready since they don’t always happen immediately

Steps:

  1. Schedule data exports in Salesforce
  2. Setup your Windows environment which will be running the backups
  3. Install FuseIT SFDC Explorer   (docs)
  4. Install Force.com CLI   (docs)
  5. Create a Windows PowerShell script based on the template below
  6. Schedule the PowerShell script to run, ideally more than 12 hours after the Salesforce data export

 

Template PowerShell Script:

I recommend using the Windows PowerShell ISE to tune and test your script.

# salesforce login
$u = "emailAddress@whatever.com

# salesforce password 
$p = "Salesforce password

# salesforce security token 
$t = "Salesforce security token"

# folder where backups should be stored
$f = "C:\Backups\Backup Folder Name


# following configure sending status emails.  
# the script assumes outside email - may be different for local mail server

# server name ex: smtp.gmail.com
$SMTPServer = "smtp.gmail.com"

# login id for smtp server
$SMTPlogin="emailAddress@whatever.com"

# password for smtp server
$SMTPpass="email password"

# following are where the two software tools are installed on your system

# path to FuseIT software
$appPath = "C:\Program Files (x86)\FuseIT SFDC Explorer\FuseIT.SFDC.DataExportConsole.exe"

# path to Force.com CLI tool
$forcePath = "C:\Program Files\Force CLI\force.exe"


# ------------------------------------------------------
# Configuration is above; execution below.
# ------------------------------------------------------ 

$dir = $f + "\Backup " + (get-date).ToString("yyyy-MM-dd")
$metaDir = $dir + "\temp"
$zipFile = $dir + "\Metadata"
$errMsg = $null

New-Item "$dir" -ItemType Directory -Force

## --- Following downloads the backup from Salesforce. 
& $appPath  /u:$u /p:$p /t:$t /e:Production $dir
if ($lastExitCode -eq 6) { $errMsg += "Downloader:  Error Logging In`n" }
if ($lastExitCode -eq 8) { $errMsg += "Downloader:  Backup Not Available`n" }

## --- Following downloads the metadata and zips it.
& $forcePath login -u="$u" -p="$($p)$($t)"

if ($lastExitCode -ne $null -and $lastExitCode -ne 0) { $errMsg += "Force.com CLI:  Error Logging In" }
else {
    # Following creates a zip from the downloaded metadata files
    & $forcePath export "$metaDir"
    & $forcePath fetch -t Aura -d "$metaDir"
    & $forcePath logout -u="$u"
    Compress-Archive -Path $metaDir -DestinationPath $zipFile -Force
    Remove-Item -LiteralPath $metaDir -Force -Recurse
}

if ($errMsg -eq $null -or $errMsg -eq "") { $errMsg = "Successful backup"}

#-- Send an email notifying of the job"s final status
if ($errMsg -ne $null -and $errMsg -ne "") {
    $EmailTo = $u
    $EmailFrom = $u
    $Subject = "SFDC Backup Report: "+ $f

    $Body = "`n`n" + $u
    $Body += "`n " + $f
    $Body += "`n " + $errMsg

    $SMTPMessage = New-Object     System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPlogin,     $SMTPpass);
    $SMTPClient.Send($SMTPMessage)
}

Leave a Comment