Backup and Restore TinyDB to/from

Text Files on Google Drive


An alternative to the many, many methods that are available for backing up the contents of a tinyDB database on AI2. In this instance, we use a google apps script web app to create individual files of each tag, with the content of the file being the tag value. This is obviously more suitable for larger tag value contents! The benefits of this method is that it stores the data off the app, provides a backup facility for the tags in a tinyDB individually, and individual tags and their values can be shared with others, or downloaded elsewhere. Any previous versions of a tag and its value is stored in a separate file in the Google Drive Bin for 30 days. All tag values are stored as plain text.


Please follow the originating demo for this HERE, to get an understanding of how things work.

You can create a folder called TINYDB, and within it a subfolder called TagsAndValues on your Google Drive.

Here I used a standalone Google Apps Script project which was created in the TINYDB folder.


Script Code

var data = [];

function doGet(e) {

var message = '';

if (e.parameter.FN == 'DataList') {

message = getFolderContents(e.parameter.folderName, e.parameter.folderId);


else if (e.parameter.FN == 'Store') {

createTagFile(e.parameter.folderId, e.parameter.filename, e.parameter.content)

message = 'File written for tag: ' + e.parameter.filename;


else if (e.parameter.FN == "Restore") {

var gfile = DriveApp.getFileById(e.parameter.fileId);

var docContent = gfile.getBlob().getDataAsString();

message = docContent;


return ContentService.createTextOutput(message);

} //end of doGet(e)

function createTagFile(folderId, filename, content) {

var folder;

folder = DriveApp.getFolderById(folderId);

var files = folder.getFilesByName(filename);

while (files.hasNext()) {;




function getFolderContents(folderName,folderId) {

data = [];

var myFolder = folderId;

var folder = DriveApp.getFolderById(myFolder);

return traverseFolders(folder, folderName, folder.getName());


function traverseFolders(folder, folderName, path) {

if ( folder.getName() != folderName ) {

var files = folder.getFiles(), file, fileName;

while (files.hasNext())


file =;

fileName = file.getName();

access = DriveApp.getFileById(file.getId()).getSharingAccess()

data.push([folder.getName(), folder.getId(), fileName, niceFileType(file.getMimeType()), file.getId(), "" +file.getId(), access])



var folders = folder.getFolders(), childFolder;

while (folders.hasNext())


childFolder =;

traverseFolders(childFolder, path + ", " + childFolder.getName());


return JSON.stringify(data);


function niceFileType( mimeType ) {

if (typeof this.fileType === 'undefined') {

this.fileType = {};

this.fileType[MimeType.FOLDER] = "Folder";

this.fileType[MimeType.GOOGLE_APPS_SCRIPT] = "Google Apps Script";

this.fileType[MimeType.GOOGLE_DOCS] = "Google Doc";

this.fileType[MimeType.GOOGLE_DRAWINGS] = "Google Drawing";

this.fileType[MimeType.GOOGLE_FORMS] = "Google Form";

this.fileType[MimeType.GOOGLE_SHEETS] = "Google Sheet";

this.fileType[MimeType.GOOGLE_SLIDES] = "Google Slides";

this.fileType[MimeType.JPEG] = "jpg";

this.fileType[MimeType.PNG] = "png";

this.fileType[MimeType.BMP] = "bmp";

this.fileType[MimeType.GIF] = "gif";

this.fileType[MimeType.SVG] = "svg";

this.fileType[MimeType.PDF] = "pdf";

this.fileType[MimeType.CSV] = "csv";

this.fileType[MimeType.PLAIN_TEXT] = "txt";

this.fileType[MimeType.HTML] = "html";


return (this.fileType.hasOwnProperty(mimeType)) ? this.fileType[mimeType] : "Other";











To use the above aia project file, you will need to setup folders on your google drive: TINYDB then TagsAndValues as a subfolder, then create a standalone apps script project in the TINYDB folder, Add the script code above to the script project, then publish as a web app, getting the script url. In the aia project you will need to add the script url and the folder names and IDs.