[quote=“akbooer, post:11, topic:189397”]Using AltUI to back up your user_data.json file
The latest build of AltUI (1608) adds a ‘backup’ button to the Controllers page. On Vera, this backs up your configuration to a file on your browsing machine by running a CGI file. Under openLuup, you can make this do anything you want by writing the appropriate WSAPI CGI Lua script.
The script file should go into [tt]/etc/cmh-ludl/cgi-bin/cmh/backup.sh[/tt] and here’s an example which simply backs up to a file in [tt]/etc/cmh-ludl/backups/[/tt]. The filename has the format [tt]backup.openLuup-00000000-2016-05-01[/tt], so only unique once a day. The “00000000” is replaced by whatever [tt]PK_AccessPoint[/tt] you’ve given your machine.
#!/usr/bin/env wsapi.cgi
module(..., package.seeall)
ABOUT = {
NAME = "backup.sh",
VERSION = "2016.04.30",
DESCRIPTION = "user_data backup script /etc/cmh-ludl/cgi-bin/cmh/backup.sh",
AUTHOR = "@akbooer",
DOCUMENTATION = "https://github.com/akbooer/openLuup/tree/master/Documentation",
}
local DIRECTORY = "backup" -- change this if you want to backup elsewhere
-- WSAPI Lua implementation of backup.sh
-- backup written to ./backups/backup.openLuup-00000000-2015-01-21
local userdata = require "openLuup.userdata"
local lfs = require "lfs"
local _log -- defined from WSAPI environment as wsapi.error:write(...) in run() method.
-- global entry point called by WSAPI connector
--[[
The environment is a Lua table containing the CGI metavariables (at minimum the RFC3875 ones) plus any
server-specific metainformation. It also contains an input field, a stream for the request's data,
and an error field, a stream for the server's error log.
The input field answers to the read([n]) method, where n is the number
of bytes you want to read (or nil if you want the whole input).
The error field answers to the write(...) method.
return values: the HTTP status code, a table with headers, and the output iterator.
--]]
function run (wsapi_env)
_log = function (...) wsapi_env.error:write(...) end -- set up the log output, note colon syntax -- 2016.02.26
lfs.mkdir (DIRECTORY)
local PK = userdata.attributes.PK_AccessPoint or "XXXXXXXX"
local DATE = os.date "%Y-%m-%d" or "0000-00-00"
local fmt = "%s/backup.openLuup-%s-%s"
local fname = fmt: format (DIRECTORY, PK, DATE)
_log ("Backing up user_data to " .. fname)
local ok, msg = userdata.save (nil, fname) -- save current luup environment
local status, return_content
if ok then
status, return_content = 200, "backup completed"
else
status, return_content = 500, "backup failed: " .. msg
end
local headers = {["Content-Type"] = "text/plain"}
local function iterator () -- one-shot iterator, returns content, then nil
local x = return_content
return_content = nil
return x
end
return status, headers, iterator
end
-----
You’ll also need the latest commit from the openLuup GitHub development branch to make this work.[/quote]
What needs to be done make this run daily?