通過自定義腳本實現其它Linux應用的一致性備份
更新時間 2022-12-08 17:17:54
最近更新時間: 2022-12-08 17:17:54
分享文章
本章節介紹如何通過自定義腳本實現其它Linux應用的一致性備份。
場景介紹
在Linux下,如果有其它應用需要一致性備份,可以編寫自己的凍結、解凍腳本,來實現應用的保護。自定義腳本需放置在 /home/rdadmin/Agent/bin/thirdparty/ebk_user 目錄中,供Agent在備份過程中調用。
下面以一個虛構的應用appexample為例,來進行說明。
appexample是一款新的數據庫,它對外提供了appexample -freeze與appexample -unfreeze兩個命令來實現凍結與解凍。
用戶需要開發自己的appexample_freeze.sh與appexample_unfreeze.sh腳本,供備份Agent調用以實現一致性備份。在備份過程中,會先調用appexample_freeze.sh腳本來凍結IO,凍結成功后,會進行磁盤的一致性快照激活,保證備份的數據是一致性的,最后再調用appexample_unfreeze.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示例如下:
#!/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