Last updated on Wednesday 2nd of November 2022 01:03:35 PM

©XSIBackup-DC chaining jobs and scripts

Create scripts with extended logic using ©XSIBackup-DC, ©XSIBackup-Pro or ©XSIBackup-Free

In previous versions of our software like ©XSIBackup-Classic we would offer two arguments to allow execution of scripts after the ©XSIBackup execution would complete: --on-success and --on-error. This was a way to simplify the very ©ESXi Shell's execution control logic.

Some people have asked us why ©XSIBackup-DC is not offering such arguments in its recent builds.

The answer is that the execution control logic is exposed by the very shell and not from the application layer. In our classic implementation of a local backup service for ©ESXi we oversimplified the matter mainly due to the fact that the main executable holding the backup logic was a script. On top of that, it would depend on the classic Linux Shell's utilities (ls, find, sed, awk, etc...).

In ©ESXi those utilities are offered by Busybox, a well known Open Source project containing them in a single binary file to which the commands above are hard linked. Some of this binaries would contain bugs, thus we passed the flow control logic up to the application layer to work those bugs around.


XSIBackup job chaining

©XSIBackup-DC is a monolithic binary which does not depend so much on outer dependencies, in fact we have placed some of those dependencies in the /<installation root>/bin/ folder to minimize the possibility that you hit any bugs or differences in the implementation that you may encounter when using ©ESXi or Linux backends.

In general the most well known command line utilities tend to have a more or less uniform set of arguments. Still, they are not designed to be cross platform compatible, thus when we recommend that you use RHEL family of distros: RHEL itself, CentOS, Fedora or Rocky Linux (currently our favourite and recommended distro), we do so to prevent problems derived from using an Operating System that has not been thoroughly tested.

How do we chain commands with ©XSIBackup-DC?

Use the regular mechanisms to chain any other logic using command line utilities. A sucessful execution will return zero (0), whereas any other result (error) will return any other value (positive or negative integer).

One of the most used and versatile ways to do one thing or another depending on the outcome of some command or group of commands is the one below:

sed -i 's/fgoo/bar/g' /home/you/somefile.txt && echo "Success" || echo "Failure"

Which literally means: change the word "foo" by "bar" in the /home/you/somefile.txt file and print "Success" if the ouput of the sed command is zero (0) or print "Failure" in case it is some other value.

The above is a short nice way to run some chained command depending on the outcome of the previous, but what about when you are running longer commands like ©XSIBackup jobs?.

In those cases we can inspect the content of the return value in the $? var which is a special system variable that holds the value of the latest execution.

/scratch/XSI/XSIBackup-DC/xsibackup \
--backup \
"VMs(VM1,VM2,VM3)" \
"root@192.168.200.100:22:/home/backup/repo01" \
--mail-to="me@mydomain.com" \
--description="Main daily backup" \
>> /scratch/XSI/XSIBackup-DC/var/log/xsibackup.log 2>&1

We will need to make use of some basic bash logical operators to handle the return value of the above job.

/scratch/XSI/XSIBackup-DC/xsibackup \
--backup \
"VMs(VM1,VM2,VM3)" \
"root@192.168.200.100:22:/home/backup/repo01" \
--mail-to="me@mydomain.com" \
--description="Main daily backup" \
>> /scratch/XSI/XSIBackup-DC/var/log/xsibackup.log 2>&1


# On success
if [ "$?" -eq "0" ]
then
# Do something here
fi


# On error
if [ "$?" -ne "0" ]
then
# Do something here
fi

Prepending commands

In case you want to prepend some command to the backup/replica job and decide whether running it or not based on the outcome of that command.

CheckSSH="$( nc -w1 192.168.200.225 22 2>/dev/null )"
if [ "${CheckSSH//SSH/}" != "${CheckSSH}" ]
then
/scratch/XSI/XSIBackup-DC/xsibackup \
--backup \
"VMs(VM1,VM2,VM3)" \
"root@192.168.200.100:22:/home/backup/repo01" \
--mail-to="me@mydomain.com" \
--description="Main daily backup" \
>> /scratch/XSI/XSIBackup-DC/var/log/xsibackup.log 2>&1
fi

The explanation for the above command is:

- Check whether the SSH service is active at port 22 in host 192.168.200.225 with a 1 second timeout (-w1).
- If the text from the output contains the text "SSH", which means that the service responded in less than one second, then run the job.