I have a flow which backs up a file every month.
External nodes required: moment
fs-ops
cronplus
The flow.
[{"id":"9d9dc047.2a46a8","type":"function","z":"1c428643.6f708a","name":"Build stuff","func":"//msg.SourcePath = msg.path + \"reboot\";\nmsg.SourcePath = msg.path;\nmsg.Destination = msg.path + msg.directory + \"/\";\nreturn msg;","outputs":1,"noerr":0,"x":1940,"y":550,"wires":[["f09ccb9.91b22b8"]]},{"id":"f09ccb9.91b22b8","type":"fs-ops-dir","z":"1c428643.6f708a","name":"","path":"SourcePath","pathType":"msg","filter":"SourceFile","filterType":"msg","dir":"payload","dirType":"msg","x":2130,"y":550,"wires":[["8cfdb301.5eb908","41aff8c9.17778"]]},{"id":"416cb465.8b53d4","type":"template","z":"1c428643.6f708a","name":"Fix filename","field":"Month","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{Month}}_{{SourceFile}}","output":"str","x":2490,"y":510,"wires":[["953146ce.6f11a","9d9dc047.2a46a8"]]},{"id":"8cfdb301.5eb908","type":"debug","z":"1c428643.6f708a","name":"Process","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2280,"y":470,"wires":[]},{"id":"41aff8c9.17778","type":"switch","z":"1c428643.6f708a","name":"","property":"payload[0]","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":2270,"y":550,"wires":[["d42f4c01.a38cf","25379d9b.104602","1dd9139a.be159c"]]},{"id":"953146ce.6f11a","type":"debug","z":"1c428643.6f708a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2650,"y":510,"wires":[]},{"id":"14d07e63.e43202","type":"change","z":"1c428643.6f708a","name":"Set Filename here","rules":[{"t":"set","p":"SourceFile","pt":"msg","to":"AC_In.db","tot":"str"},{"t":"move","p":"year","pt":"msg","to":"directory","tot":"msg"},{"t":"set","p":"path","pt":"msg","to":"event_paths","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":2310,"y":510,"wires":[["48cb1b59.4c5bc4","416cb465.8b53d4"]]},{"id":"d42f4c01.a38cf","type":"fs-ops-mkdir","z":"1c428643.6f708a","name":"","path":"SourcePath","pathType":"msg","dirname":"directory","dirnameType":"msg","mode":"777","fullpath":"directory","fullpathType":"msg","x":1960,"y":590,"wires":[["ae9fe60.3953b98"]]},{"id":"25379d9b.104602","type":"delay","z":"1c428643.6f708a","name":"Delay","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1930,"y":630,"wires":[["caae316e.836ff","4e6b9776.81c218"]]},{"id":"1dd9139a.be159c","type":"debug","z":"1c428643.6f708a","name":"OUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2650,"y":550,"wires":[]},{"id":"48cb1b59.4c5bc4","type":"debug","z":"1c428643.6f708a","name":"RAW","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2470,"y":470,"wires":[]},{"id":"865d2f4.74af3d","type":"moment","z":"1c428643.6f708a","name":"Month","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"MMM","locale":"en_AU","output":"Month","outputType":"msg","outTz":"Australia/Sydney","x":2120,"y":510,"wires":[["14d07e63.e43202"]]},{"id":"ae9fe60.3953b98","type":"debug","z":"1c428643.6f708a","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2120,"y":590,"wires":[]},{"id":"caae316e.836ff","type":"debug","z":"1c428643.6f708a","name":"1d","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2120,"y":670,"wires":[]},{"id":"4e6b9776.81c218","type":"fs-ops-move","z":"1c428643.6f708a","name":"","sourcePath":"SourcePath","sourcePathType":"msg","sourceFilename":"SourceFile","sourceFilenameType":"msg","destPath":"Destination","destPathType":"msg","destFilename":"Month","destFilenameType":"msg","link":false,"x":2130,"y":630,"wires":[["2948fb28.41cfb4"]]},{"id":"c958bf6f.7c5d3","type":"moment","z":"1c428643.6f708a","name":"Year","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY","locale":"en_AU","output":"year","outputType":"msg","outTz":"Australia/Sydney","x":1930,"y":510,"wires":[["865d2f4.74af3d"]]},{"id":"2948fb28.41cfb4","type":"debug","z":"1c428643.6f708a","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2270,"y":630,"wires":[]},{"id":"a1873562.affbf","type":"inject","z":"1c428643.6f708a","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1780,"y":550,"wires":[["c958bf6f.7c5d3"]]},{"id":"62315ad4.03cf1c","type":"cronplus","z":"1c428643.6f708a","name":"Monthly","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"Monthly","topic":"Monthly","payloadType":"str","payload":"Backup","expressionType":"cron","expression":"0 55 23 L * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":1770,"y":370,"wires":[["bbb7e036.106ec","c958bf6f.7c5d3"]]},{"id":"86e71dcd.ebff98","type":"comment","z":"1c428643.6f708a","name":"AC in file","info":"","x":2280,"y":430,"wires":[]},{"id":"b98c060f.df7f3","type":"comment","z":"1c428643.6f708a","name":"Read me *","info":"2020 10 05\nName in `set filename` had leading `_`.\nRemoved and shall see what happens now.","x":2460,"y":430,"wires":[]}]
Walk through:
Top left going right.
cronplus
generates a signal at 23:55 on the last day of the month. (Backup
)
mement
nodes to get the year
and month
. (msg.year
and msg.month
respectively)
change
node gets/sets some things: msg.SourceFile
msg.directory
(from msg.year
) msg.path
from global.event_paths
template
node to construct the destination file name.
function
node to build a bit more. I'm not sure why I just don't use msg.path
, but.....
This sets msg.SourcePath
from msg.path
, msg.destination
set from msg.path
+ msg.directory
+ "/"
I see the correct stuff here.
The next bit is where fs-ops
checks if there is a file existing that needs moving.
This is not clear why it is here, but it will become obvious in a moment.
the switch
node only lets the message pass if there is a file. Ok in this case there will be as it is an active database.
Split here.
1 - Make a directory (needed to put/move file into it)
2 - delay
(2 second) Just to be sure the directory is made.
move the file.
Here are screen shots of it happening when I press the test
button.
(when I did it, the link from the delay
to fs-ops
was deleted to stop it actually being moved)
(Above)
you can see the message going through. Blue Pink Yellow.
Yes, I haven't expanded them here. No problem See next.
So, the message arrives.
You have the bits:
msg.month
-- destination file name.
msg.SourceFile
-- the original file name.
msg.path
-- old message
msg.SourcePath
-- path to the file.
msg.destination
-- path to where the file is to be moved.
That would work.
A couple of times on the 1'st of the month, I would do it properly and the file would be moved.
But what is happening is when I look I have:
pi@TelePi:/media/pi/9020-9C27/events $ ls
2020 archive Forced_WiFi_up_commanded.txt IFF.db MQTT.db Uplink.db WAP_Status_Change.txt
_AC_In.db commands_received.txt Forced_WiFi_up.txt Main.db MusicPi.db WAP.db
AC_In.db Flow errors.txt health.db MQTT_Comms_Fail.db TimePi_status.db WAP_Scan_timeout.txt
pi@TelePi:/media/pi/9020-9C27/events $ cd 2020
pi@TelePi:/media/pi/9020-9C27/events/2020 $ ls
Apr_AC_In.db Aug_AC_In.db Jul_AC_In.db Jun_AC_In.db May_AC_In.db Sep_AC_In.db
pi@TelePi:/media/pi/9020-9C27/events/2020 $ cd ..
pi@TelePi:/media/pi/9020-9C27/events $ mv _AC_In.db 2020/Oct_AC_In.db
There is this file _AC_In.db
I don't get why it works if I press the inject
(test) button but fails if left to its own devices.