-
Notifications
You must be signed in to change notification settings - Fork 2
Use Case – Scheduled Data Backup
In this advanced example scenario, we want to schedule a script, that regularly does an organization data export for backup purposes.
This is not to be confused with the Salesforce standard feature Export Backup Data from Salesforce: With the standard feature, the scheduled backup task (i.e. the actual data export) runs on the Salesforce backend and produces a set of .zip files with the exported .csv files. Once ready, an Admin gehts notified and needs to download those .zip files manually. (Or use any means of automation for these downloads.)
As opposed, the approach below will schedule our Powershell script that will run scripted Dataloder to export data as configured in the script.
Prerequisites
- You have created a key file
MyKeyfile.key
with theNew-SfEncryptionKeyFile
and your password + security token for the backup user have been prepared viaConvertTo-SfEncryptedString
commands. - Based on this, we can generate an authentication token for the target org in the $MyOrg variable via
Get-SfAuthToken
- The user has assigned all necessary read permission for the data to be backed up, esp. "View All" permissions and proper field level security on the objects to be included in the backup.
Approach
- The list of Objects will be held in an Powershell array variable and can easily be extended as needed.
- For each Object, there will be a plain text file
<Object>.txt
that lists all the fields to be included in the backup. This can be configured as needed. - The main script loops through the list, compiles the SELECT statement for the desired object and runs an export task via Bulk API.
- For easier demonstration, all files are placed in the current directory.
In the example, we will backup Accounts, Contacts and a custom object Invoice__c that is a child record to the Account object.
The field list for Accounts in Account.txt
:
Id
ErpCustomerNumber__c
BillingCity
BillingCountry
The field list for Contacts in Contact.txt
:
Id
AccountId
Account.ErpCustomerNumber__c
FirstName
LastName
MailingCountry
MailingCity
MailingPostalCode
The field list for Invoices in Invoice__c.txt
:
Id
ErpInvoiceNumber__c
CustomerAccount__c
CustomerAccount__r.ErpCustomerNumber__c
InvoiceDate__c
InvoiceAmount__c
The main script for the backup task:
$MyEncryptedPassword = 'EncryptionResultFromPreviousStep'
$MyOrg = Get-SfAuthToken -Username MyUser@MyCompany.com -EncryptedString $MyEncryptedPassword -KeyFile MyKeyfile.key -InstanceUrl https://test.salesforce.com
$MyObjectsToBackupList = @(
'Account',
'Contact',
'Invoice__c'
)
foreach($Object in $MyObjectsToBackupList){
# Create a comma-separated list of field names from the .txt file
$FieldList = ConvertTo-SfFieldList (Get-SfFieldNames ".\$Object.txt")
# Put together our SELECT statement
$Soql = "SELECT $FieldList FROM $Object ORDER BY CreatedDate ASC"
# Run the export to the default target file name <Object>.csv
Export-SfRecords $MyOrg $Object $Soql -Bulk Serial
}