Backup Script Not Working

Hi Jbudd,

This is how I would change your flow for my situation, I hope I didn't upset you by trying chatgpt as I am just trying to learn how it works

Thanks

Hi Jbudd,

I changed the flow to this and it sends me an email telling me how many files in the backup but the backup does not appear in the backup directory

Thanks

Looking at your screen capture, I think you have misunderstood the {{{ ... }}} bits of my template.

The template in the flow I posted contains a Bash script, but it also uses Node-red's "Mustache" syntax to insert msg.nodereddirectory and msg.backupfilename into the script.

# To override defaults pass these in as msg properties 
NODEREDDIR={{{nodereddirectory}}}   
BACKUPTO={{{backupfilename}}}
  • This is a Bash script, so comment lines begin with # not //
  • The {{{ ... }}} is replaced by Node-red. Bash only sees the result of the replacement.
  • If msg.nodereddirectory contains .node-red then the first line of the script becomes NODEREDDIR=.node-red. If it does not exist, or is blank, the line becomes NODEREDDIR=, also valid Bash syntax (to set the variable $NODEREDDIR to an empty string).

That's it for Node-red jiggery-pokery. Everything else in the template is pure Bash syntax.

# Default location
if [ -z $NODEREDDIR ]
then
   NODEREDDIR=.node-red
fi
  • If $NODEREDDIR is zero length (an empty string) then set it's value to the default, .node-red

Taking a look at my inject node:

  • msg.nodereddir is simple enough - it's .node-red, which happens also to be the default in the script.
  • msg.backupfilename is more complex but it uses Jsonata "node-red-" & $moment().format("YYYY-MM-DD") & ".tar.gz" to obtain the current date and wrap it between node-red- and .tar.gz

You could connect a debug node (set to show the full message) to the outputs of the inject and template nodes to see more clearly what's going on.

I'm happy to help you further to adapt my flow to your needs but I need to see any changes you have made as code I can import, not as a screen capture.

Hi Jbudd,

This is how I changed the template:

# To override defaults pass these in as msg properties NB Bash does not see these mustaches
NODEREDDIR={{{/home/pi/.node-red}}}   
BACKUPTO={{{/dev/sda1/node-red-backups}}}

# Default location
if [ -z $NODEREDDIR ]
then
   NODEREDDIR=.node-red
fi

# Default backup file
if [ -z $BACKUPTO ]
then
   BACKUPTO=nodered.tar.gz
fi

# Don't overwrite backupfile
#if [ -s "$BACKUPTO" ]
#then
#  echo "Error: $BACKUPTO already exists" >&2
#  exit 1
#fi

ARCHIVER="tar -czf $BACKUPTO --numeric-owner --exclude=node_modules*"
$($ARCHIVER $NODEREDDIR)   # Do the backup

COUNTFILES="$(tar -tvf $BACKUPTO | wc -l)"
FILESIZE="$(du -h $BACKUPTO | sed -e 's/\s.*//')"
printf "%s files %s in %s" $COUNTFILES $FILESIZE $BACKUPTO

Thanks

I refer you to this line of my last post

This is making an assumption about Node-RED's current working directory. I would not recommend that.

TO find out what the cwd is for your instance of node-red, run an exec node with one of the following:

Linux : Use the pwd command in the terminal to display the full path of the current working directory. This command shows the absolute path relative to the root directory.

Windows (CMD) : Use the cd command without any arguments to print the full path of the current directory. Alternatively, use the echo %CD% command to display the current directory path stored in the CD environment variable.

Windows (PowerShell) : Use the Get-Location command to retrieve the current directory path. You can also use (Get-Location).Path for the same result.

Hi Totally,

is this what you meant ?

Thanks

1 Like

Well that's true.
It's almost certainly possible to run Node-red on Linux in such a way that the Node-red directory is not called .node-red, nor indeed need it be in the current directory.

The script could check the location of the flows file and strip off the actual filename
NODEREDDIR=$(journalctl --unit=nodered --grep="Flows file" --merge --lines=1 --output=cat | sed 's/.: //; s/[^/]$//')

This does make the assumption that Node-red is being run by a systemd service called nodered.service and that the directory containing the flows file is also the directory where Node-red is installed.
It might result in the tar archive having absolute pathnames too, which is generally a bad thing.

And bearing in mind that in 90% of installations on Linux, Node-red is installed in the directory ./.node-red relative to the working directory, I think mine is a reasonable default.

I did allow for different setups though by passing msg.nodereddir from the inject node.

  • I don't advise using my flow on any OS other than Linux. Does tar even exist on that OS? Does the $(command) syntax?
  • My flow writes a Bash script within Node-red and runs it. To add more exec nodes for additional commands is perverse. IMHO :grin:

Hi Jbudd,

I think what you are saying is that your flow is for a default location for node red which I am sure mine is, if I can get it working it would be great, I am going to delete the first one and import your flow again as i messed it up and hopefully go from there

Thanks

The complexity of my latest post was really addressed to @TotallyInformation who is not a beginner.
You can safely ignore that post (or digest it, entirely up to you) :grinning_face: