Node-RED RPi Automated Backup to Email

#1

Hi all,

This would be my first post on the forum and I am trying to gather my knowledge by taking bits and pieces of the forum and internet.

After installing Node-RED (The Script by Peter Scargill) on a Raspberry Pi and I was happily playing and testing with it until it crashed.
I couldn't recover any of my created flows and I was lost and started all over again.

Checking the internet their are two files of importance which would or should contain everything.

My Raspberry Pi is called "Pikea". (YT Andreas Spiess Ikea Tradfri hack)

/home/pi/.node-red/flows_Pikea.json
/home/pi/.node-red/flows_Pikea_cred.json

The first thing I am trying to create now is a automation to backup up two files and send them through email.
Creating the files works ,but I am stuck sending them as an attachment.

[{"id":"4f338c61.443ef4","type":"file in","z":"3a945cb8.53d5b4","name":"flows_pikea.json","filename":"/home/pi/.node-red/flows_Pikea.json","format":"utf8","sendError":true,"x":500,"y":120,"wires":[["3c598934.1b9876"]]},{"id":"eac1ae94.77aee","type":"file","z":"3a945cb8.53d5b4","name":"","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":870,"y":120,"wires":[["aa600982.a86638"]]},{"id":"3c598934.1b9876","type":"function","z":"3a945cb8.53d5b4","name":"","func":"msg.filename = \"/home/pi/.node-red/backup/backup_\"+(new Date().toISOString().replace(':', '_').replace(':', '_').replace(/\\..+/, ''))+\"_flows_Pikea.json\";\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":120,"wires":[["eac1ae94.77aee"]]},{"id":"12b08589.3ee24a","type":"file in","z":"3a945cb8.53d5b4","name":"flows_pikea_cred.json","filename":"/home/pi/.node-red/flows_Pikea_cred.json","format":"utf8","sendError":true,"x":520,"y":200,"wires":[["a87f56f4.4ae548"]]},{"id":"c33609db.faee18","type":"file","z":"3a945cb8.53d5b4","name":"","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":870,"y":200,"wires":[["aa600982.a86638"]]},{"id":"a87f56f4.4ae548","type":"function","z":"3a945cb8.53d5b4","name":"","func":"msg.filename = \"/home/pi/.node-red/backup/backup_\"+(new Date().toISOString().replace(':', '_').replace(':', '_').replace(/\\..+/, ''))+\"_flows_Pikea_cred.json\";\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":200,"wires":[["c33609db.faee18"]]},{"id":"aa600982.a86638","type":"e-mail","z":"3a945cb8.53d5b4","server":"smtp.gmail.com","port":"465","secure":true,"name":"email@gmail.com","dname":"email@gmail.com","x":1190,"y":180,"wires":[]},{"id":"992b5b36.cedf68","type":"inject","z":"3a945cb8.53d5b4","name":"","topic":"Inject 1 sec after deploy then every 24hrs","payload":"","payloadType":"date","repeat":"86400","crontab":"","once":true,"onceDelay":"1","x":190,"y":160,"wires":[["4f338c61.443ef4","12b08589.3ee24a"]]}]

p.s., send failed due to removed email for privacy

After deploying it it makes a copy of both files placing them in the backup folder ,but two emails only contains the contents of the files.

I just want a single email with both files attached with a proper subject from which Node-RED and date modified when backup was created.

Any help or guidance would be greatly appreciated.

Thanks.

0 Likes

#2

Firstly add some debug nodes to allow you to see what messages your script passes.
Secondly read the info panel for the nodes that you are using. For example the file node will tell you what the message is passed on as. The Email node will tell you what part of the message object any attachments need to be.
You will then find that you need to move things around between node which you can do with the change node (configured correctly)

The debug nodes should also allow you to see that nodes don’t have any knowledge of nodes around them. So the email node will receive two messages one per input wire and do two things. If you want the email node to work once you will need to use the join node.

0 Likes

#3

Hi @ukmoose,

You have set me off in the right direction with adding the debug again I just realized I was pushing a string into the email node.

I can now send both files in separate emails trying to combine them into one email with two attachments atm.

[{"id":"3a945cb8.53d5b4","type":"tab","label":"Public fo forum","disabled":false,"info":""},{"id":"4f338c61.443ef4","type":"file in","z":"3a945cb8.53d5b4","name":"flows_pikea.json","filename":"/home/pi/.node-red/flows_Pikea.json","format":"utf8","sendError":true,"x":500,"y":120,"wires":[["3c598934.1b9876"]]},{"id":"eac1ae94.77aee","type":"file","z":"3a945cb8.53d5b4","name":"","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":910,"y":120,"wires":[[]]},{"id":"3c598934.1b9876","type":"function","z":"3a945cb8.53d5b4","name":"","func":"msg.filename = \"/home/pi/.node-red/backup/backup_\"+(new Date().toISOString().replace(':', '_').replace(':', '_').replace(/\\..+/, ''))+\"_flows_Pikea.json\";\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":120,"wires":[["eac1ae94.77aee","ba1d8648.94c958"]]},{"id":"12b08589.3ee24a","type":"file in","z":"3a945cb8.53d5b4","name":"flows_pikea_cred.json","filename":"/home/pi/.node-red/flows_Pikea_cred.json","format":"utf8","sendError":true,"x":520,"y":200,"wires":[["a87f56f4.4ae548"]]},{"id":"c33609db.faee18","type":"file","z":"3a945cb8.53d5b4","name":"","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":910,"y":200,"wires":[[]]},{"id":"a87f56f4.4ae548","type":"function","z":"3a945cb8.53d5b4","name":"","func":"msg.filename = \"/home/pi/.node-red/backup/backup_\"+(new Date().toISOString().replace(':', '_').replace(':', '_').replace(/\\..+/, ''))+\"_flows_Pikea_cred.json\";\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":200,"wires":[["c33609db.faee18","37521ddb.330732"]]},{"id":"aa600982.a86638","type":"e-mail","z":"3a945cb8.53d5b4","server":"smtp.gmail.com","port":"465","secure":true,"name":"email@gmail.com","dname":"email@gmail.com","x":1190,"y":320,"wires":[]},{"id":"992b5b36.cedf68","type":"inject","z":"3a945cb8.53d5b4","name":"","topic":"Inject 1 sec after deploy then every 24hrs","payload":"","payloadType":"date","repeat":"86400","crontab":"","once":true,"onceDelay":"1","x":190,"y":160,"wires":[["4f338c61.443ef4","12b08589.3ee24a"]]},{"id":"ba1d8648.94c958","type":"function","z":"3a945cb8.53d5b4","name":"1. With Attachment","func":"msg.topic = \"1. With Attachment\";\n\nmsg.payload = \"Dear,<br><br>With attachment<br><br>Kind regards,<br>Your Node-Red flow\";\n\nmsg.attachments = [{\n    filename: msg.filename ,\n    path: msg.filename ,\n    content: msg.payload\n}];\n        \nreturn msg;","outputs":1,"noerr":0,"x":930,"y":280,"wires":[["aa600982.a86638"]]},{"id":"37521ddb.330732","type":"function","z":"3a945cb8.53d5b4","name":"2. With Attachment","func":"msg.topic = \"2. With attachment\";\n\nmsg.payload = \"Dear,<br><br>With attachment<br><br>Kind regards,<br>Your Node-Red flow\";\n\nmsg.attachments = [{\n    filename: msg.filename ,\n    path: msg.filename ,\n    content: msg.payload\n}];\n        \nreturn msg;","outputs":1,"noerr":0,"x":930,"y":380,"wires":[["aa600982.a86638"]]}]

Thanks for your support

0 Likes

#4

read the last 2 lines of my last post…

0 Likes

#5

Investigating join function :smiley:

I think I need to figure out how to combine both strings to an array with Join.


Support for attachment processing. Attachments should appear as an array in msg.attachments.

0 Likes

#6

@Leroxy,
As an alternative have a look at: Backup
I use it to zip all my files and backup the zip file to dropbox.

0 Likes

#7

@cflurin Wow I just realized it's your github page Thanks for sharing !

Still trying to get the join to work and I am actually getting quite good at crashing the node-red.
Needed to restore my flow files several times now...

0 Likes

#8

It should be hard to crash Node-RED so if it crashes again it would be useful to look and see if there is anything in the logs and then open an new topic with it in. Hopefully either someone can see what you are doing wrong or someone can see what node needs modifying.

0 Likes

#9

I changed the inject to a manual trigger without inject after start nor interval.
Somehow I get the error message "Lost connection to server, reconnecting..."

If I leave the option deploy/inject 1 sec after start it will go into boot loop without any chance to recover from it.

Email 1. and 2. are received ,but 3. never arrives :frowning:

msg.topic = "3. With both attachments";

msg.payload = "Dear,<br><br>With both attachments<br><br>Kind regards,<br>Your Node-Red flow";

msg.attachments = [{
    filename: msg.filename ,
    path: msg.filename ,
    content: msg.payload
}];
        
return msg;

Maybe its the function which is incorrect as now I am pushing a array instead of a string.

0 Likes

#10

It finally works the last issue was that the array needed to split into array elements for attachments in the function again.

msg.topic = "3. With both attachment";

msg.payload = "Dear,<br><br>With both attachment<br><br>Kind regards,<br>Your Node-Red flow";

msg.attachments = [{
    filename: msg.filename[0] ,
    path: msg.filename[0] },{
    filename: msg.filename[1] ,
    path: msg.filename[1]
}];
return msg;

Herewith the latest flow json

[{"id":"3a945cb8.53d5b4","type":"tab","label":"Public fo forum","disabled":false,"info":""},{"id":"4f338c61.443ef4","type":"file in","z":"3a945cb8.53d5b4","name":"flows_pikea.json","filename":"/home/pi/.node-red/flows_Pikea.json","format":"utf8","sendError":true,"x":500,"y":120,"wires":[["3c598934.1b9876"]]},{"id":"eac1ae94.77aee","type":"file","z":"3a945cb8.53d5b4","name":"","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":930,"y":60,"wires":[[]]},{"id":"3c598934.1b9876","type":"function","z":"3a945cb8.53d5b4","name":"","func":"msg.filename = \"/home/pi/.node-red/backup/backup_\"+(new Date().toISOString().replace(':', '_').replace(':', '_').replace(/\\..+/, ''))+\"_flows_Pikea.json\";\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":120,"wires":[["eac1ae94.77aee","ba1d8648.94c958","c29834bf.7639d8"]]},{"id":"12b08589.3ee24a","type":"file in","z":"3a945cb8.53d5b4","name":"flows_pikea_cred.json","filename":"/home/pi/.node-red/flows_Pikea_cred.json","format":"utf8","sendError":true,"x":520,"y":200,"wires":[["a87f56f4.4ae548"]]},{"id":"c33609db.faee18","type":"file","z":"3a945cb8.53d5b4","name":"","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":930,"y":120,"wires":[[]]},{"id":"a87f56f4.4ae548","type":"function","z":"3a945cb8.53d5b4","name":"","func":"msg.filename = \"/home/pi/.node-red/backup/backup_\"+(new Date().toISOString().replace(':', '_').replace(':', '_').replace(/\\..+/, ''))+\"_flows_Pikea_cred.json\";\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":200,"wires":[["c33609db.faee18","37521ddb.330732","c29834bf.7639d8"]]},{"id":"aa600982.a86638","type":"e-mail","z":"3a945cb8.53d5b4","server":"smtp.gmail.com","port":"465","secure":true,"name":"email@gmail.com","dname":"email@gmail.com","x":1290,"y":240,"wires":[]},{"id":"992b5b36.cedf68","type":"inject","z":"3a945cb8.53d5b4","name":"","topic":"","payload":"Start","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"1","x":170,"y":160,"wires":[["4f338c61.443ef4","12b08589.3ee24a"]]},{"id":"ba1d8648.94c958","type":"function","z":"3a945cb8.53d5b4","name":"1. With Attachment","func":"msg.topic = \"1. With Attachment\";\n\nmsg.payload = \"Dear,<br><br>With attachment<br><br>Kind regards,<br>Your Node-Red flow\";\n\nmsg.attachments = [{\n    filename: msg.filename ,\n    path: msg.filename ,\n    content: msg.payload\n}];\n        \nreturn msg;","outputs":1,"noerr":0,"x":950,"y":200,"wires":[["aa600982.a86638"]]},{"id":"37521ddb.330732","type":"function","z":"3a945cb8.53d5b4","name":"2. With Attachment","func":"msg.topic = \"2. With attachment\";\n\nmsg.payload = \"Dear,<br><br>With attachment<br><br>Kind regards,<br>Your Node-Red flow\";\n\nmsg.attachments = [{\n    filename: msg.filename ,\n    path: msg.filename ,\n    content: msg.payload\n}];\n        \nreturn msg;","outputs":1,"noerr":0,"x":950,"y":260,"wires":[["aa600982.a86638","45c9ff.308896"]]},{"id":"c29834bf.7639d8","type":"join","z":"3a945cb8.53d5b4","name":"","mode":"custom","build":"array","property":"filename","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":360,"wires":[["dabc6db8.12cdf","fc206623.5acaa8"]]},{"id":"dabc6db8.12cdf","type":"function","z":"3a945cb8.53d5b4","name":"3. With both attachments","func":"msg.topic = \"3. With both attachment\";\n\nmsg.payload = \"Dear,<br><br>With both attachment<br><br>Kind regards,<br>Your Node-Red flow\";\n\nmsg.attachments = [{\n    filename: msg.filename[0] ,\n    path: msg.filename[0] },{\n    filename: msg.filename[1] ,\n    path: msg.filename[1]\n}];\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":360,"wires":[["91644a78.119228","aa600982.a86638"]]},{"id":"fc206623.5acaa8","type":"debug","z":"3a945cb8.53d5b4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":890,"y":460,"wires":[]},{"id":"91644a78.119228","type":"debug","z":"3a945cb8.53d5b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1100,"y":460,"wires":[]},{"id":"45c9ff.308896","type":"debug","z":"3a945cb8.53d5b4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":930,"y":300,"wires":[]}]

0 Likes