Updating Rolling Artifact Snapshot Backups Groovy Script for Groovy Engine Update
- Sarah Ansell
- 17 hours ago
- 3 min read
In the November EPM Planning patch update a new version of the Groovy engine, which has stricter validation rules, is being released.
The Groovy Script Validator indicated that a rule I wrote about in a previous blog post needed updating so I thought I would add a post to show what needed to change and to provide an updated example.
When I ran the validator the results showed that the date functions I had used were being deprecated and were not validating:

Here are the parts it didn't like - highlighted in bold font:
// Retrieve today's date. //
import java.text.SimpleDateFormat
def date = new Date()
def sdf = new SimpleDateFormat("yyyy-MM-dd")
// Check dates, if backup older than deleteBeforeDate (today.minus(x)) then delete backup //
def deleteBeforeDate = date.minus(15)
def autoSnapshotDeleteList = (List) []
def tempList3 = ((List) autoSnapshotList).each{ entry ->
if(Date.parse("yyyy-MM-dd", ((String)entry).substring(0,10)).before(deleteBeforeDate)) {
autoSnapshotDeleteList.add(entry)
}
}
Here is my upgraded solution which uses the Calendar function:
// Retrieve today's date. //
def date = Calendar.getInstance()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
// Check dates, if backup older than deleteBeforeDate then delete backup //
Calendar deleteBeforeDate = Calendar.getInstance()
deleteBeforeDate.add(Calendar.DAY_OF_YEAR, -15) // Currently set to delete if more than 15 days old //
def autoSnapshotDeleteList = (List) []
def tempList3 = ((List) autoSnapshotList).each{ entry ->
// Extract date from Snapshot Name and format as date //
def SnapshotDate = sdf.parse(((String)entry).substring(0,10))
Calendar SnapshotDate2 = Calendar.getInstance()
SnapshotDate2.setTime(SnapshotDate)
// Compare dates and add to list for deletion //
if(SnapshotDate2.before(deleteBeforeDate)) {
autoSnapshotDeleteList.add(entry)
}
}
Here is the latest full rename backup script - currently set to retain 15 days worth of backups (providing this does not exceed 150GB):
// Purpose: Rename Maintenance Snapshot, preventing daily overwrite. Delete backups older than 15 days (Data Retention). //
/* RTPS: */
// Global - Get Connection and App Name //
Connection conn = operation.application.getConnection("Local_EPM")
Application application = operation.getApplication()
String AppName = application.getName()
//******* RENAME SNAPSHOT ********///
// Decide if TEST or PROD Application for Snapshot Naming //
HttpResponse<String> jsonResponseR1 = conn.get("/interop/rest/")
.header("Content-Type", "application/json")
.asString();
if(!(200..299).contains(jsonResponseR1.status)){
throwVetoException("Error occured: $jsonResponseR1.statusText")}
def object = new JsonSlurper().parseText(jsonResponseR1.body) as Map
//println "$object.links"
if("$object.links".toLowerCase().contains('-test')){
AppName = AppName + ' - Test'
}
println "Application is $AppName"
// Retrieve today's date. //
def date = Calendar.getInstance()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
// Configuring the new backup name. //
String SnapshotName = sdf.format(date.getTime()) + " - " + AppName + " - Artifact Snapshot"
println "Renaming Artifact Snapshot to " + SnapshotName
// Posts the rename job. This sends the rename request to the server. Prints success or error message to Job Console //
HttpResponse<String> jsonResponseR2 = conn.put("/interop/rest/v2/snapshots/rename")
.header("Content-Type", "application/json")
.body(json(["snapshotName": "Artifact Snapshot","newSnapshotName":SnapshotName]))
.asString();
def object2 = new JsonSlurper().parseText(jsonResponseR2.body) as Map
println "Rename ${object2.status == 0 ? "successful" : "failed"}.\nDetails: $object2.details"
if (object2.status != 0){
throwVetoException("Job interrupted after failure to rename snapshot. See log messages.")
}
// ***** DELETE OLD SNAPSHOTS - (If rename has been successful) *****//
// Create a list of existing backup names //
HttpResponse<String> jsonResponseR3 = conn.get("/interop/rest/11.1.2.3.600/applicationsnapshots")
.header("Content-Type", "application/json")
.asString();
def snapshotList = (List) []
def jsonMap = (Map) new JsonSlurper().parseText(jsonResponseR3.body)
def tempList = ((List) jsonMap.get("items")).each{ item ->
if( (((Map) (item)).get("type")) == "LCM"){
snapshotList.add((((Map) (item)).get("name")))
}
}
println "Automated Snapshots Available:"
def autoSnapshotList = (List) []
def pattern = /....-..-.. - $AppName - Artifact Snapshot\.zip/
def tempList2 = ((List) snapshotList).each{ entry ->
if(((String) entry) ==~ pattern) {
autoSnapshotList.add(entry)
println entry
}
}
// Check dates, if backup older than deleteBeforeDate then delete backup //
Calendar deleteBeforeDate = Calendar.getInstance()
deleteBeforeDate.add(Calendar.DAY_OF_YEAR, -15) // Currently set to delete if more than 15 days old //
def autoSnapshotDeleteList = (List) []
def tempList3 = ((List) autoSnapshotList).each{ entry ->
// Extract date from Snapshot Name and format as date //
def SnapshotDate = sdf.parse(((String)entry).substring(0,10))
Calendar SnapshotDate2 = Calendar.getInstance()
SnapshotDate2.setTime(SnapshotDate)
// Compare dates and add to list for deletion //
if(SnapshotDate2.before(deleteBeforeDate)) {
autoSnapshotDeleteList.add(entry)
}
}
// Delete Old Auto Snapshots (Snapshot Name Must be Encoded) //
String snapshotName = []
if(autoSnapshotDeleteList.size() > 0){
println "Deleting Older Snapshots"
def tempList4 = autoSnapshotDeleteList.each{ entry ->
//Encoding//
snapshotName = entry.toString().replaceAll(" ","%20")
HttpResponse<String> jsonResponseR4 = conn.delete("/interop/rest/11.1.2.3.600/applicationsnapshots/$snapshotName")
.header("Content-Type", "application/json")
.asString();
object2 = new JsonSlurper().parseText(jsonResponseR4.body) as Map
println "Delete ${object2.status == 0 ? "successful" : "failed"}.\nDetails: $object2.details"
}
}
Please visit the original post if you'd like to see more information regarding setting up a rolling archive of backups in the Migration Snapshot's area within EPM Planning: 7 Day Rolling Artifact Snapshot Backups in the Cloud.
Comments