
# 开发冻结脚本和解冻脚本
#### 开发冻结脚本
以一个虚构的应用appexample为例，在备份过程中，会先调用appexample_freeze.sh脚本来冻结IO。
appexample_freeze.sh示例如下：
```
#!/bin/sh
AGENT_ROOT_PATH=$1  #Agent程序调用脚本时，传入的的根目录，日志函数等会使用此变量，请不要改名
PID=$2 #Agent程序调用脚本时，传入的PID数字，用于结果的输出，请不要改名
. "${AGENT_ROOT_PATH}/bin/agent_func.sh"#引用脚本框架，提供了日志，加解密等功能
#结果处理函数，用于将结果写入到文件中，供脚本调用者获取返回值。
#入参 $1: 0表示成功，1表示失败
#无返回值
#RESULT_FILE在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."
    #查找appexample是否存在，如果appexample不存在，则返回0，退出
    #在冻结IO步骤中，Agent程序会依次调用每个冻结脚本，如果一个失败，总体就会失败。所以为了防止干扰其他程序的冻结过程，找不到appexample时，应返回0
    which appexample
    if [ $? -ne 0 ]
    then
            Log "[INFO]:appexample is not installed."
            ExitWithResult 0
    fi
    #调用实际的冻结命令
    appexample -freeze
    if [ $? -ne 0 ]
    then
            Log "[INFO]:appexample freeze failed."
            #冻结失败，记录结果并退出
            ExitWithResult 1
    fi
    Log "[INFO]:Freeze appexample success."
    #冻结成功，记录结果并退出
    ExitWithResult 0
}
Main
```
冻结成功后，会进行磁盘的一致性快照激活，保证备份的数据是一致性的，最后再调用appexample_unfreeze.sh脚本解冻IO。
#### 开发解冻脚本
appexample_unfreeze.sh示例如下：
```
#!/bin/sh
AGENT_ROOT_PATH=$1  #Agent程序调用脚本时，传入的的根目录，日志函数等会使用此变量，请不要改名
PID=$2 #Agent程序调用脚本时，传入的PID数字，用于结果的输出，请不要改名
. "${AGENT_ROOT_PATH}/bin/agent_func.sh"#引用脚本框架，提供了日志，加解密等功能
#结果处理函数，用于将结果写入到文件中，供脚本调用者获取返回值。
#入参 $1: 0表示成功，1表示失败
#无返回值
#RESULT_FILE在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."
    #查找appexample是否存在，如果appexample不存在，则返回0，退出
    #在解冻IO步骤中，Agent程序会依次调用每个解冻脚本，如果一个失败，总体就会失败。所以为了防止干扰其他程序的解冻过程，找不到appexample时，应返回0
    which appexample
    if [ $? -ne 0 ]
    then
            Log "[INFO]:appexample is not installed."
            ExitWithResult 0
    fi
    #调用实际的解冻命令
    appexample -unfreeze
    if [ $? -ne 0 ]
    then
         Log "[INFO]:appexample freeze failed."
        #解冻失败，记录结果并退出
         ExitWithResult 1
    fi
    Log "[INFO]:Freeze appexample. success"
    #解冻成功，记录结果并退出
    ExitWithResult 0
}
Main
```
