Home > Storage > PowerFlex > White Papers > PowerFlex REST API Introduction > File system snapshot rule and protection policy
This use case demonstrates how to create a snapshot rule and a snapshot policy consisting of multiple rules. For example, we can create a rule that generates a snapshot once per day, and a second that creates a snapshot every week. When creating the snapshot policy, we include the snapshot rule identifiers for the two snapshots.
We need the following information to create each snapshot rule:
The parameters required for the json payload will vary based on how the snapshot rule is defined. This example shows the creation of two snapshot rules. The first rule definition is for daily snapshots; the second rule definition is for weekly snapshots.
#Import modules
import json
import requests
import urllib3
from pfauth import authenticate
#Create snapshot rule code block
def createSnapshotrule(pfxm, accessToken, payload):
#Create snapshot rule
url = f'https://{pfxm}/rest/v1/nas-snapshot-rules'
payload
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
}
response = requests.post(url, headers = headers, data = payload, verify = False)
responseJson = response.json()
ruleid = (responseJson['id'])
return ruleid
#PFxM host
pfxm = 'pfmp.powerflex.lab'
#PFxM user
username = 'admin'
password = 'P@ssword123'
#Pass variables to the authenticate() function.
accessToken = authenticate(username, password)
#Daily file system snapshot
ruleName = "Daily"
interval = "ONE_DAY"
retention = 168
ruleType = "PROTOCOL_READ_ONLY"
payload = json.dumps({
"name": f"{ruleName}",
"interval": f"{interval}",
"retention": retention,
"type": f"{ruleType}"
})
#Create Daily snapshot rule
ruleid1 = createSnapshotrule(pfxm, accessToken, payload)
#Weekly file system snapshot
ruleName = "Weekly"
timeDay = "05:00"
daysWeek = "SATURDAY"
retention = 168
ruleType = "PROTOCOL_READ_ONLY"
payload = json.dumps({
"name": f"{ruleName}",
"interval": None,
"time_of_day": f"{timeDay}",
"days_of_week": [f"{daysWeek}"],
"retention": retention,
"type": f"{ruleType}"
})
#Create Weekly snapshot rule
ruleid2 = createSnapshotrule(pfxm, accessToken, payload)
We now need to create the snapshot policy and add the two rules we created in the previous step.
#File System snapshot policy code block
def createPolicy(pfxm, accessToken, payload):
#Create snapshot rule
url = f'https://{pfxm}/rest/v1/nas-protection-policies'
payload
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
}
response = requests.post(url, headers = headers, data = payload, verify = False)
responseJson = response.json()
policyid = (responseJson['id'])
return policyid
policyName = "ProdFS"
#Snapshot policy payload
payload = json.dumps({
"name": f"{policyName}",
"snapshot_rule_ids": [
f"{ruleid1}",
f"{ruleid2}"
]
})
Now we can assign the file system snapshot policy to a NAS file system.
#Assign protection policy to file system code block
def setPolicy(pfxm, accessToken, payload):
#Set protection policy
url = f'https://{pfxm}/rest/v1/file-systems/63b499a8-598a-e997-d311-ea32dfd5becd/set-protection-policy'
payload
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
}
response = requests.post(url, headers = headers, data = payload, verify = False)
print (f"The response is {response.text}")
responseJson = response.json()
policyid = (responseJson['id'])
return assignPolicy
#Payload for assigning protection policy to file system
payload = json.dumps({
"protection_policy_id": f"{prodfsPolicy}"
})
#Assign the protection policy
assignPolicy = setPolicy(pfxm, accessToken, payload)