Skip to content
cflurin edited this page Nov 3, 2019 · 12 revisions

Flow to backup the node-red-files from your node-red user directory. The dsm will go through 3 steps:

  1. delete the old zip file
  2. zip all the files to a zip-file, filename yyy-mm-ddThhmmss_nr.zip
  3. output a msg ready to feed a dropbox node

backup

Configuration

{
    "currentState": "step1",
    "states": {
        "step1": {
            "start": "step2",
            "reset": "step1"
        },
        "step2": {
            "zip": "step3",
            "reset": "step1"
        },
        "step3": {
            "upload": "step1",
            "reset": "step1"
        }
    },
    "data": {
    },
    "methods": {
        "init": [
            "sm.udir = RED.settings.userDir + '/';",
            "sm.exec = require('child_process').exec;",
            "sm.hostname = require('os').hostname();",
            "sm.unlink = require('fs').unlink;"
        ],
        "start": [
            "/* delete old zip file */",
            "var zipfile = sm.udir+'node-red.zip';",
            
            "sm.fill = 'grey';",
            "sm.text = 'deleting';",
            "output = false;",

            "sm.unlink(zipfile, function (err) {",
            "   if (err) {",
            "       node.warn('no file '+zipfile);",
            "   }",
            "   resume('zip', msg);",
            "});"
        ],
        "zip": [
            "var pre = ' ' + sm.udir;",
            "var cmd = 'zip -r';",
            "cmd += pre + 'node-red.zip';",
            "cmd += pre + 'flows_'+sm.hostname+'.json';",
            "cmd += pre + 'flows_'+sm.hostname+'_cred.json';",
            "cmd += pre + '.config.json';",
            "cmd += pre + '.sessions.json';",
            "cmd += pre + 'settings.js';",
            "cmd += pre + 'package.json';",
            "cmd += pre + 'package-lock.json';",
            "cmd += pre + 'context/*';",
            
            "sm.fill = 'grey';",
            "sm.text = 'zipping';",
            "output = false;",
            
            "sm.exec(cmd, function(error, stdout, stderr) {",
            "   if (error) {",
            "       node.warn(error);",
            "   } else {",
            "       resume('upload', msg);",
            "   }",
            "});"
        ],
        "upload": [
            "sm.ts = timestamp().slice(0,-4).replace(/:/g,'');",
            "msg.filename = sm.hostname+'/' + sm.ts + '_nr.zip';",
            "msg.localFilename = sm.udir+'node-red.zip';",
            "sm.fill = 'green';",
            "sm.text = sm.ts;"
        ],
        "reset": [
            "sm.fill = 'grey';",
            "sm.text = 'reset';",
            "output = false;"
        ],
        "status": {
            "fill": {
                "get": "sm.fill"
            },
            "shape": "dot",
            "text": {
                "get": "sm.text"
            }
        }
    }
}

Flow

[{"id":"81c26dba.ab1a3","type":"dsm","z":"6ff0723.8c6b78c","name":"backup","sm_config":"{\n    \"currentState\": \"step1\",\n    \"states\": {\n        \"step1\": {\n            \"start\": \"step2\",\n            \"reset\": \"step1\"\n        },\n        \"step2\": {\n            \"zip\": \"step3\",\n            \"reset\": \"step1\"\n        },\n        \"step3\": {\n            \"upload\": \"step1\",\n            \"reset\": \"step1\"\n        }\n    },\n    \"data\": {\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.udir = RED.settings.userDir + '/';\",\n            \"sm.exec = require('child_process').exec;\",\n            \"sm.hostname = require('os').hostname();\",\n            \"sm.unlink = require('fs').unlink;\"\n        ],\n        \"start\": [\n            \"/* delete old zip file */\",\n            \"var zipfile = sm.udir+'node-red.zip';\",\n            \n            \"sm.fill = 'grey';\",\n            \"sm.text = 'deleting';\",\n            \"output = false;\",\n\n            \"sm.unlink(zipfile, function (err) {\",\n            \"   if (err) {\",\n            \"       node.warn('no file '+zipfile);\",\n            \"   }\",\n            \"   resume('zip', msg);\",\n            \"});\"\n        ],\n        \"zip\": [\n            \"var pre = ' ' + sm.udir;\",\n            \"var cmd = 'zip -r';\",\n            \"cmd += pre + 'node-red.zip';\",\n            \"cmd += pre + 'flows_'+sm.hostname+'.json';\",\n            \"cmd += pre + 'flows_'+sm.hostname+'_cred.json';\",\n            \"cmd += pre + '.config.json';\",\n            \"cmd += pre + '.sessions.json';\",\n            \"cmd += pre + 'settings.js';\",\n            \"cmd += pre + 'package.json';\",\n            \"cmd += pre + 'package-lock.json';\",\n            \"cmd += pre + 'context/*';\",\n            \n            \"sm.fill = 'grey';\",\n            \"sm.text = 'zipping';\",\n            \"output = false;\",\n            \n            \"sm.exec(cmd, function(error, stdout, stderr) {\",\n            \"   if (error) {\",\n            \"       node.warn(error);\",\n            \"   } else {\",\n            \"       resume('upload', msg);\",\n            \"   }\",\n            \"});\"\n        ],\n        \"upload\": [\n            \"sm.ts = timestamp().slice(0,-4).replace(/:/g,'');\",\n            \"msg.filename = sm.hostname+'/' + sm.ts + '_nr.zip';\",\n            \"msg.localFilename = sm.udir+'node-red.zip';\",\n            \"sm.fill = 'green';\",\n            \"sm.text = sm.ts;\"\n        ],\n        \"reset\": [\n            \"sm.fill = 'grey';\",\n            \"sm.text = 'reset';\",\n            \"output = false;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.fill\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.text\"\n            }\n        }\n    }\n}","x":300,"y":740,"wires":[["e078cd3b.aacdd"]]},{"id":"fc7f3d34.e3e3b","type":"inject","z":"6ff0723.8c6b78c","name":"backup","topic":"start","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":740,"wires":[["81c26dba.ab1a3"]]},{"id":"e078cd3b.aacdd","type":"debug","z":"6ff0723.8c6b78c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":470,"y":680,"wires":[]},{"id":"50ad789a.045fd8","type":"inject","z":"6ff0723.8c6b78c","name":"reset","topic":"reset","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":780,"wires":[["81c26dba.ab1a3"]]},{"id":"aba61037.3c55c","type":"comment","z":"6ff0723.8c6b78c","name":"dropbox node","info":"","x":490,"y":740,"wires":[]}]
Clone this wiki locally