Reputation: 525
Is there a way to do a nightly backup of an App Maker database? Just in case a user accidentally deletes any data?
Even just having an outputted spreadsheet would be acceptable.
Upvotes: 0
Views: 241
Reputation: 71
You can create an clock based Installable Trigger that will execute once a day in the morning before office hours.
This piece of code will be on a Server side script and will look something like this:
function createInstallableTrigger() {
// Runs at 5am in the timezone of the script
ScriptApp.newTrigger("backUp")
.timeBased()
.atHour(5)
.everyDays(1) // Frequency is required if you are using atHour() or nearMinute()
.create();
}
function backUp() {
try {
var spreadSheet = SpreadsheetApp.openById("").getActiveSheet(),
dataToBackUp = [],
globalKeys = {
model: ["first_name", "last_name", "email"],
label: ["First Name", "Last Name", "Email"]
},
var records = app.models.requests.newQuery().run();
if(records.length >= 1) {
for (var i = 0; i < records.length; i++) {
var newLine = [];
for (var x = 0; x < globalKeys.model.length; x++) {
newLine.push(records[i][globalKeys.model[x]]);
}
dataToBackUp.push(newLine);
// at the end, push it all on the spreadsheet
if(i === records.length - 1) {
// check if there is any entry at all
if(dataToBackUp.length >= 1) {
// append column titles first
spreadSheet.appendRow(globalKeys.label);
//
spreadSheet.getRange(2, 1, dataToBackUp.length, globalKeys.model.length).setValues(dataToBackUp);
}
}
}
}
} catch(e) {
console.log(e);
}
}
Upvotes: 2