Backup and restore module

Based on input from this forum I created a backup and restore module which I include in my project while developing. It needs 2 lines of code in the main program, a variable called versionNumber and a call in the setup() function to backupProject(). Everytime the version is changed, the code is exported to a new file in the folder Backup in Dropbox. It would be nice if after it saved it to the Dropbox folder, you could request Dropbox to sync instead of going to sprites.

If anyone has any suggestions to improve the codeā€¦

-- Backup module
-- Generic

restoreProjectName = "Restore"
restoreMarker = "#"

-- function backupProject ()
-- The backup module is assumed to be in a project and will save the current Project
-- to Dropbox when called and the version number is different than the last version
-- saved.
-- Global variables expected to be set:
--   projectName, the name of the Project. Part of the filename in Dropbox
--                if it is not set, a random number is created
--   projectVersion, the current version of the Project. If this number is different
--                   than the last saved version, the data is saved
-- Data used:
--   projectName, if the variable is not set, the projectName is created randomly
--                and then saved in this ProjectData
--   projectVersion, the version of the last backup
-- Global variables changed:
--   backedUp, it will contain the projectVersion if the backup was successful
--   projectName, it will contain the random number created projectName if it
--                is not set beforehand
-- NOTE!
-- A subfolder is used in the Dropbox folder, Backup. It has to exist beforehand.
--  It cannot be created within Codea.
-- Remember the backup is only really successful if you sync your Dropbox folder
-- Please copy your project first, before trying the restore function if you wish to restore in-place

function backupProject ()
    if not projectName then
        projectName = readProjectData("projectName")
        if not projectName then
            projectName = "CodeaProject-"..math.random(1,10000)
    projectVersion = projectVersion or 0
    local lastSavedVersion = readProjectData("projectVersion")
    if not lastSavedVersion or "x"..projectVersion~=lastSavedVersion then
        local datestr ="%Y-%m-%d %H.%M.%S")
        local fileName = projectName.."_v"..projectVersion..".txt"
        local file = os.getenv("HOME").."/Documents/Dropbox.spritepack/Backup/"..fileName
        local data = "\
-- "..projectName.."\
-- Backup created "..datestr.."\
        local tabList = listProjectTabs()
        if #tabList>0 then
            local a,b
            for a,b in pairs(tabList) do
--"..restoreMarker.." %s\
            local wFd
            wFd =,"w")
            backedUp = projectVersion

-- function restoreProject (inVersion)
-- This function will restore a saved project from Dropbox
-- Global variables expected to be set:
--   projectName, the name of the Project. Part of the filename in Dropbox
-- Incoming variables:
--   inVersion, the version number you wish to restore. If this is not provided
--              the last saved version will be restored.
-- Output:
-- In the project with the name given in restoreProjectName, you will find the restore
-- of your project

function restoreProject (inVersion)
    if restoreProjectName then
        if not inVersion then
        local fileName=projectName.."_v"..inVersion..".txt"
        local file = os.getenv("HOME").."/Documents/Dropbox.spritepack/"..filename
        local temp = listProjectTabs(restoreProjectName)
        local currentTabs = {}
        for k,v in pairs(temp) do
            currentTabs[v] = v
        local rFd =,"r")
        local totfile = rFd:read("*all")
        local current = 1
        local fEndOfTabName,fEndOfPage,thePage,theTabName
        local fStartOfTabName = string.find(totfile,"--"..restoreMarker)
        while fStartOfTabName do
            fStartOfTabName = fStartOfTabName+4
            fEndOfTabName = string.find(totfile,"\
            fEndOfPage = string.find(totfile,"--"..restoreMarker,fEndOfTabName) or string.len(totfile)
            theTabName = string.sub(totfile,fStartOfTabName,fEndOfTabName-1)
            thePage = string.sub(totfile,fEndOfTabName+1,fEndOfPage-1)
            currentTabs[theTabName] = nil
            current = fEndOfPage
            fStartOfTabName = string.find(totfile,"--"..restoreMarker,current)
        for k,v in pairs(currentTabs) do