Skip to content

Instantly share code, notes, and snippets.

@fraser73
Created July 1, 2016 09:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fraser73/a275b56b419df8714641538fb2ad2f8a to your computer and use it in GitHub Desktop.
Save fraser73/a275b56b419df8714641538fb2ad2f8a to your computer and use it in GitHub Desktop.
Backup to tar.gz files each night

Scheduled copying/backup of multiple locations to tar.gz files. This allows multiple locations to be backed up each night of the week with "mon..sun" added to each filename.

Currently this flow is setup to copy everything required to restore a node-red installation.

[{"id":"31e30b0d.1b8ea4","type":"exec","z":"74066dce.c8160c","command":"tar ","addpay":true,"append":"","useSpawn":"","timer":"","name":"Execute TAR commands","x":490,"y":3400,"wires":[["9a3f23ff.1ffdc8"],["e7ad4c64.15236"],["41875532.141754","a6aa81a5.fe43d8"]]},{"id":"51873b5d.3a8ce4","type":"inject","z":"74066dce.c8160c","name":"Trigger Nightly Backups","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 02 * * *","once":false,"x":210,"y":3300,"wires":[["820f6967.8f1e"]]},{"id":"9a3f23ff.1ffdc8","type":"debug","z":"74066dce.c8160c","name":"std out","active":true,"console":"false","complete":"payload","x":730,"y":3360,"wires":[]},{"id":"e7ad4c64.15236","type":"debug","z":"74066dce.c8160c","name":"std error","active":true,"console":"false","complete":"payload","x":740,"y":3400,"wires":[]},{"id":"41875532.141754","type":"debug","z":"74066dce.c8160c","name":"return code","active":true,"console":"false","complete":"payload","x":750,"y":3440,"wires":[]},{"id":"a6aa81a5.fe43d8","type":"function","z":"74066dce.c8160c","name":"Detect Error","func":"if (msg.payload==\"Error\") {\n var returnMsg={topic: \"Error in nightly TAR backups\", payload:\"Error in nightly TAR backup operations\"};\n return (returnMsg);\n } else {\n return (nul);\n }","outputs":1,"noerr":0,"x":570,"y":3500,"wires":[["fd1f6abb.fcbe2"]]},{"id":"fd1f6abb.fcbe2","type":"e-mail","z":"74066dce.c8160c","server":"server.address.com","port":"25","name":"user@address.com","dname":"Email errors","x":750,"y":3500,"wires":[]},{"id":"820f6967.8f1e","type":"function","z":"74066dce.c8160c","name":"Generate TAR commands","func":"// First off, generate the day name\nvar d = new Date();\nvar day = new Array(7);\nday[0]= \"Sun\";\nday[1] = \"Mon\";\nday[2] = \"Tue\";\nday[3] = \"Wed\";\nday[4] = \"Thu\";\nday[5] = \"Fri\";\nday[6] = \"Sat\";\n\n//Root Backup\nvar returnMsg1={payload: \"-zcvf /backups/root-\"+(day[d.getDay()])+\".tar.gz /root\"};\n\n//HTML Backup\nvar returnMsg2={payload: \"-zcvf /backups/var-www-\"+(day[d.getDay()])+\".tar.gz /var/www\"};\n\n//Node Red Config Backup\nvar returnMsg3={payload: \"-zcvf /backups/var-node-red-\"+(day[d.getDay()])+\".tar.gz /var/node-red\"};\n\n//Node Red Modules Backup\nvar returnMsg4={payload: \"-zcvf /backups/usr-lib-node_modules-\"+(day[d.getDay()])+\".tar.gz /usr/lib/node_modules\"};\n\n//Kick off the TAR commands!\nreturn [returnMsg1,returnMsg2,returnMsg3,returnMsg4];\n\n\n","outputs":"4","noerr":0,"x":480,"y":3300,"wires":[["31e30b0d.1b8ea4"],["31e30b0d.1b8ea4"],["31e30b0d.1b8ea4"],["31e30b0d.1b8ea4"]]},{"id":"e3d3efe1.53dd2","type":"comment","z":"74066dce.c8160c","name":"Nightly Backup to /backups as TAR.GZ","info":"Trigger kicks off the flow at 0200 every morning.\n\ntar commands are built, less the actual \"tar \" at the beginning\n(note the space!) because the exec command node requires a\ncommand to be entered to it, it can't just accept everything\nfrom the incoming message.\n\nThe system is designed to make one backup each day of the week,\nso inserts three letter day names into the output filename.\n\nOutput files are sent to /backups. You can then copy these off\nthe node-red system with whatever means you chose. I use samba.\n\nMultiple tar commands can be issued in parallel by configuring\nmultiple outputs from the function which creates the commands.\n\nThis example makes four tar.gz files:\n\nRoot ID (this is where I keep loads of stuff, bad practice, I know!)\nApache html files (/var/www)\nNode-Modules (/usr/lib/node_modules)\nNode Red Settings (which I have configured to /var/node-red)\n\nNB: The exec command \"tar \" must have a trailing space!\n\nEmails are sent to alert of error, if any of the commands issued\nreturn the string \"Error\".\n","x":230,"y":3220,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment