Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Backup and restore module

edited November 2013 in General Posts: 3

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
--
-- DISCLAIMER!
-- 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)
            saveProjectData("projectName",projectName)
        end
    end
    projectVersion = projectVersion or 0
    local lastSavedVersion = readProjectData("projectVersion")
    if not lastSavedVersion or "x"..projectVersion~=lastSavedVersion then
        local datestr = os.date("%Y-%m-%d %H.%M.%S")
        local fileName = projectName.."_v"..projectVersion..".txt"
        local file = os.getenv("HOME").."/Documents/Dropbox.spritepack/Backup/"..fileName
        local data = "\n-- "..projectName.."\n-- Backup created "..datestr.."\n"
        local tabList = listProjectTabs()
        if #tabList>0 then
            local a,b
            for a,b in pairs(tabList) do
                data=data..string.format("\n--"..restoreMarker.." %s\n",b)
                data=data..readProjectTab(b)
            end
            local wFd
            wFd = io.open(file,"w")
            wFd:write(data)
            wFd:close()
            saveProjectData("projectVersion","x"..projectVersion)
            backedUp = projectVersion
        end
    end
end

--
-- 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
            readProjectData("projectVersion",inVersion)
        end
        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
        end
        local rFd = io.open(file,"r")
        local totfile = rFd:read("*all")
        io.close(rFd)
        local current = 1
        local fEndOfTabName,fEndOfPage,thePage,theTabName
        local fStartOfTabName = string.find(totfile,"--"..restoreMarker)
        while fStartOfTabName do
            fStartOfTabName = fStartOfTabName+4
            fEndOfTabName = string.find(totfile,"\n",fStartOfTabName)
            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)
            saveProjectTab(restoreProjectName..":"..theTabName,thePage)
            currentTabs[theTabName] = nil
            current = fEndOfPage
            fStartOfTabName = string.find(totfile,"--"..restoreMarker,current)
        end
        for k,v in pairs(currentTabs) do
            saveProjectTab(restoreProjectName..":"..k,nil)
        end
    end
end

Sign In or Register to comment.