Help Center/ Cloud Backup and Recovery/ Best Practices/ Using a Custom Script to Implement Application-Consistent Backup/ Using a Custom Script to Implement Consistent Backup for Other Linux Applications
Updated on 2024-08-28 GMT+08:00

Using a Custom Script to Implement Consistent Backup for Other Linux Applications

Scenario

If other Linux applications need application-consistent backup, you can compile custom scripts to freeze and unfreeze them. To ensure the custom scripts invokable by the Agent, save them in the /home/rdadmin/Agent/bin/thirdparty/ebk_user directory.

The following example uses an application named appexample for demonstration.

appexample is a new database. It provides the appexample -freeze and appexample -unfreeze commands for freezing and unfreezing.

To implement application-consistent backup, you need to compile two scripts named appexample_freeze.sh and appexample_unfreeze.sh. During a backup, the Agent first invokes the appexample_freeze.sh script to freeze I/Os, then activates consistent snapshots on disks to ensure that the backup data is consistent, and finally invokes appexample_unfreeze.sh to unfreeze I/Os.

Figure 1 shows the backup process.

Figure 1 Application-consistent backup flowchart

Compiling a Freezing Script

Example freezing script named appexample_freeze.sh:

#!/bin/sh
AGENT_ROOT_PATH=$1  #The root directory required when the Agent invokes the script. Functions, such as log functions, will use this variable. Do not rename this directory.
PID=$2 #The PID required when the Agent invokes the script. It is used for command output. Do not rename it.
. "${AGENT_ROOT_PATH}/bin/agent_func.sh"#Reference script framework, which provides functions, such as logging, encryption, and decryption
#Result processing function, which writes operation results into given files for invokers to obtain return values.
 # Input parameter. $1: 0 indicates a success; 1 indicates a failure.
# No return value
#RESULT_FILE is defined in agent_func.sh.
function ExitWithResult()
{
    Log "[INFO]:Freeze result is $1."
    echo $1 > ${RESULT_FILE}
    chmod 666 ${RESULT_FILE}
    exit $1
}
function Main()
{
    Log "*********************************************************************"
    Log "[INFO]:Begin to freeze appexample."
    #Check whether appexample exists. If not, 0 is returned and the script exits.
    #In the process of freezing I/Os, the Agent program invokes each freezing script in sequence. If any script fails to be invoked, the whole process fails. To avoid interference from other programs, 0 should be returned when appexample cannot be found.
    which appexample
    if [ $? -ne 0 ]
    then
            Log "[INFO]:appexample is not installed."
            ExitWithResult 0
    fi
    #Invoke the actual freezing command.
    appexample -freeze
    if [ $? -ne 0 ]
    then
            Log "[INFO]:appexample freeze failed."
            #Freezing failed. Record the result and exit.
            ExitWithResult 1
    fi
    Log "[INFO]:Freeze appexample success."
    #Freezing successful. Record the result and exit.
    ExitWithResult 0
}
Main

Compiling an Unfreezing Script

Example unfreezing script named appexample_unfreeze.sh:

#!/bin/sh
AGENT_ROOT_PATH=$1  #The root directory required when the Agent invokes the script. Functions, such as log functions, will use this variable. Do not rename this directory.
PID=$2 #The PID required when the Agent invokes the script. It is used for command output. Do not rename it.
. "${AGENT_ROOT_PATH}/bin/agent_func.sh"#Reference script framework, which provides functions, such as logging, encryption, and decryption
#Result processing function, which writes operation results into given files for invokers to obtain return values.
 # Input parameter. $1: 0 indicates a success; 1 indicates a failure.
# No return value
#RESULT_FILE is defined in agent_func.sh.
function ExitWithResult()
{
    Log "[INFO]:Freeze result is $1."
    echo $1 > ${RESULT_FILE}
    chmod 666 ${RESULT_FILE}
    exit $1
}
function Main()
{
    Log "*********************************************************************"
    Log "[INFO]:Begin to freeze appexample."
    #Check whether appexample exists. If not, 0 is returned and the script exits.
    #In the process of unfreezing I/Os, the Agent program invokes each unfreezing script in sequence. If any script fails to be invoked, the whole process fails. To avoid interference from other programs, 0 should be returned when appexample cannot be found.
    which appexample
    if [ $? -ne 0 ]
    then
            Log "[INFO]:appexample is not installed."
            ExitWithResult 0
    fi
    #Invoke the actual unfreezing command.
    appexample -unfreeze
    if [ $? -ne 0 ]
    then
         Log "[INFO]:appexample freeze failed."
        #Unfreezing failed. Record the result and exit.
         ExitWithResult 1
    fi
    Log "[INFO]:Freeze appexample. success"
    #Unfreezing successful. Record the result and exit.
    ExitWithResult 0
}
Main