重庆思庄Oracle、Redhat认证学习论坛

标题: 配置OGG自启动服务 [打印本页]

作者: mahan    时间: 6 天前
标题: 配置OGG自启动服务
本帖最后由 mahan 于 2024-12-22 15:46 编辑

OGG没有自启动服务,有时候碰到主机重启,没法随主机启动而启动。在 Linux下可以通过创建系统服务来实现自启动,以下是详细步骤:

创建启动脚本
编写一个 Shell 脚本来启动 GoldenGate 进程,例如 ogg_start.sh:

$ cat /ogg/ogg_start.sh
#!/bin/bash

# 设置 GoldenGate 安装路径和日志路径
GG_HOME=/ogg  # 替换为实际的 GoldenGate 安装路径
LOG_FILE=$GG_HOME/ogg_start.log  # 替换为实际日志文件路径
export LD_LIBRARY_PATH=$GG_HOME:$LD_LIBRARY_PATH
source /home/oracle/.bash_profile
# 创建日志文件夹(如果不存在)
mkdir -p $(dirname $LOG_FILE)

# 函数:写日志
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}

log_message "Starting GoldenGate startup process..."

cd $GG_HOME || { log_message "ERROR: Failed to switch to GoldenGate home directory."; exit 1; }

# 启动管理器
log_message "Starting manager process..."
./ggsci <<EOF
start mgr
exit
EOF
sleep 2
# 检查管理器状态
MGR_STATUS=$(./ggsci <<EOF | grep -i "Manager" | awk '{print $2}'
info all
exit
EOF
)

if [ "$MGR_STATUS" != "RUNNING" ]; then
    log_message "ERROR: Failed to start manager process."
else
    log_message "Successfully started manager process."
fi

# 动态启动所有非运行状态的抽取和投递进程
log_message "Checking and starting non-running processes..."
NON_RUNNING_PROCESSES=$(./ggsci <<EOF | awk '/^(EXTRACT|REPLICAT)/ && ($2 == "STOPPED" || $2 == "ABENDED") {print $3}'
info all
exit
EOF
)

if [ -z "$NON_RUNNING_PROCESSES" ]; then
    log_message "All processes are already running."
else
    for process in $NON_RUNNING_PROCESSES; do
        log_message "Starting process: $process"
        ./ggsci <<EOF
start $process
exit
EOF
        sleep 2
        # 检查启动是否成功
        STATUS=$(./ggsci <<EOF | grep -i "$process" | awk '{print $2}'
info all
exit
EOF
)
        if [ "$STATUS" != "RUNNING" ]; then
            log_message "ERROR: Failed to start process: $process"
        else
            log_message "Successfully started process: $process"
        fi
    done
fi

log_message "GoldenGate startup process completed."

关键点:

info all 命令会列出所有进程及其状态。
使用 awk 筛选出状态为 STOPPED 或 ABENDED 的进程名称。
依次启动这些进程。
给脚本赋予执行权限:

$ chmod +x /ogg/ogg_start.sh
创建关闭脚本
编写一个 ogg_stop.sh 脚本来停止所有正在运行的进程:

$ cat ogg/ogg_stop.sh
#!/bin/bash

# 设置 GoldenGate 安装路径和日志路径
GG_HOME=/ogg/  # 替换为实际的 GoldenGate 安装路径
LOG_FILE=$GG_HOME/ogg_stop.log  # 替换为实际日志文件路径
export LD_LIBRARY_PATH=$GG_HOME:$LD_LIBRARY_PATH
source /home/oracle/.bash_profile

# 创建日志文件夹(如果不存在)
mkdir -p $(dirname $LOG_FILE)

# 函数:写日志
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}

log_message "Starting GoldenGate shutdown process..."

cd $GG_HOME || { log_message "ERROR: Failed to switch to GoldenGate home directory."; exit 1; }

# 动态停止所有运行的抽取和投递进程
log_message "Stopping all running processes..."
RUNNING_PROCESSES=$(./ggsci <<EOF | grep "RUNNING" | awk '{print $3}'
info all
exit
EOF
)

if [ -z "$RUNNING_PROCESSES" ]; then
    log_message "No running processes found."
else
    for process in $RUNNING_PROCESSES; do
        log_message "Stopping process: $process"
        ./ggsci <<EOF
stop $process
exit
EOF
        sleep 2
        # 检查停止是否成功
        STATUS=$(./ggsci <<EOF | grep "$process" | awk '{print $2}'
info all
exit
EOF
)
        if [ "$STATUS" != "STOPPED" ]; then
            log_message "ERROR: Failed to stop process: $process"
        else
            log_message "Successfully stopped process: $process"
        fi
    done
fi

# 停止管理器进程
log_message "Stopping manager process..."
./ggsci <<EOF
stop mgr!
exit
EOF
sleep 2
# 检查管理器状态
MGR_STATUS=$(./ggsci <<EOF | grep -i "manager" | awk '{print $2}'
info all
exit
EOF
)

if [ "$MGR_STATUS" != "STOPPED" ]; then
    log_message "ERROR: Failed to stop manager process."
else
    log_message "Successfully stopped manager process."
fi

log_message "GoldenGate shutdown process completed."
关键点:

(1)动态检测运行状态的进程

利用 ggsci 的 info all 命令获取所有进程的状态。
使用 grep "RUNNING" 筛选出运行中的进程。
(2)逐一停止运行的进程

循环处理筛选出的进程名,逐一通过 STOP 命令停止它们。
(3)最后停止管理器进程

确保所有抽取和投递进程停止后,再停止 mgr 进程。
给脚本赋予执行权限:

$ chmod +x /ogg/ogg_stop.sh
创建 systemd 服务文件
在 /etc/systemd/system/ 目录下创建一个服务文件,例如 ogg.service:

# cat /etc/systemd/system/ogg.service
[Unit]
Description=Oracle GoldenGate Service
After=network.target

[Service]
User=oracle
Group=oinstall
Type=forking
RemainAfterExit=yes
KillMode=none
TimeoutStopSec=60
TimeoutSec=300
ExecStart=/ogg/ogg_start.sh
ExecStop=/ogg/ogg_stop.sh
Restart=on

[Install]
WantedBy=multi-user.target
启动服务
# 重新加载 systemd 配置
systemctl daemon-reload

# 启动服务
systemctl start ogg

# 设置开机启动
systemctl enable ogg
验证服务状态

systemctl status ogg
如果不设置成系统服务,可以在rc.local文件添加启动内容:

# cat /etc/rc.local
sh /ogg/ogg_start.sh






欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/) Powered by Discuz! X3.2