Generate shell files directly in servers via Workflow

  • 1 March 2022
  • 1 reply
  • 183 views

Userlevel 2
Badge +3

This is simple but working trick to maintain Commvault.

Via Workflow built-in activity "ExecuteScript", you can call arbitrary shells (both on Windows/Linux) remotely.
If you have full access to the remote server and able to place scripts, or if the script can be called via Workflow, no issues.
But if you'd like to modify the script remotely for OS-side schedule jobs (Task Scheduler or crontab), it's slightly difficult to control this process remotely, since Commvault can restore the script but not so easy to modify the content itself.

If the script contains only text data, you can utilize echo command to put contents remotely, one trick required though since arbitrary

To achieve this,

First, prepare any scripts you want to put remotely (this is modified version of .bat file generated via Save as Script):

Next, pass the generated script to the following logic, which "escapes" all strings per OS type:

String text = <original script>;
String osType = <Windows or Linux>;

// Generate echo command
StringBuilder sb = new StringBuilder("");

String[] lines = text.split("\\r?\\n");
for (String line : lines) {
if (osType.equals("Windows")) {
if (line.trim().equals("")) {
sb.append("echo." + "\r\n");
} else {
// Escape some characters e.g. closing parenthesis ')' since all echo will be enclosed with parenthesis.
sb.append("echo !\"!" + line.replaceAll("[)]", "^)").replaceAll("[%]", "%%").replaceAll("[|]", "^|") + "\r\n");
}
} else {
if (line.trim().equals("")) {
sb.append("echo" + "\r\n");
} else {
// Escape parenthesis '()' since all echo will be enclosed with parenthesis.
sb.append("echo "
+ line.replaceAll("\\\\", "\\\\\\\\")
.replaceAll("[(]", "\\\\(")
.replaceAll("[)]", "\\\\)")
.replaceAll("#", "\\\\#")
.replaceAll(";", "\\\\;")
.replaceAll("<", "\\\\<")
.replaceAll(">", "\\\\>")
.replaceAll("\\$", "\\\\\\$")
.replaceAll("\"", "\\\\\"")
.replaceAll("`", "\\\\`")
.replaceAll("[|]", "\\\\|")
.replaceAll("[&]", "\\\\&")
.replaceAll("[']", "\\\\'")
+ "\r\n");
}
}
}

// Store to variable
workflow.setVariable(<any Workflow variable>, sb.toString());

Lastly, access to remote server via ExecuteScript and put the generated escaped strings via simple redirect:

echo <escaped variable> > output_text

This generates expected script files remotely, so later you can change permissions via ExecuteScript again.

Hope this helps,


If you have a question or comment, please create a topic

1 reply

Userlevel 7
Badge +23

Super helpful, thanks!

I’ll convert this to an article.