Temp and diff table

 #!/bin/bash


source /auto/home/oracledb_ci.cfg


log "Starting Day 1: Daily Operations."


# Step 1: Truncate CMDB_DB_CI_TEMP

sqlplus -s $username/$password@$database <<EOF

TRUNCATE TABLE CMDB_DB_CI_TEMP;

exit

EOF


if [ $? -ne 0 ]; then

    log "Failed to truncate CMDB_DB_CI_TEMP."

    exit 1

fi

log "CMDB_DB_CI_TEMP truncated successfully."


# Step 2: Load CMDB_DB_CI_TEMP with current data

control_file="/auto/home/oracledb_ci_temp.ctl"

cat <<EOF > $control_file

LOAD DATA

INFILE '$csv_file'

INTO TABLE CMDB_DB_CI_TEMP

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

(

support_group, install_status, oracle_home, u_cmdb_ci_used_for, u_cmdb_owner_group, name, correlation_id, version

)

EOF


sqlldr $username/$password@$database control=$control_file log="$log_location/temp_loader_oracledb_ci.log"

if [ $? -ne 0 ]; then

    log "Failed to load data into CMDB_DB_CI_TEMP."

    exit 1

fi

log "Data loaded successfully into CMDB_DB_CI_TEMP."


# Step 3: Insert differences into CMDB_DB_CI_DIFF

sqlplus -s $username/$password@$database <<EOF

INSERT INTO CMDB_DB_CI_DIFF

SELECT * FROM CMDB_DB_CI_DAILY MINUS SELECT * FROM CMDB_DB_CI_TEMP;

UPDATE CMDB_DB_CI_DIFF SET modified_date = SYSDATE;

exit

EOF


if [ $? -ne 0 ]; then

    log "Failed to populate CMDB_DB_CI_DIFF."

    exit 1

fi

log "Differences calculated and inserted into CMDB_DB_CI_DIFF."


# Step 4: Validate SQL Loader and Step 5 & 6 if successful

if [ -s "$log_location/temp_loader_oracledb_ci.bad" ]; then

    log "Errors found in SQL Loader. Stopping process."

    exit 1

else

    log "SQL Loader successful. Proceeding with updates."

fi


# Step 5: Truncate CMDB_DB_CI_DAILY

sqlplus -s $username/$password@$database <<EOF

TRUNCATE TABLE CMDB_DB_CI_DAILY;

exit

EOF


if [ $? -ne 0 ]; then

    log "Failed to truncate CMDB_DB_CI_DAILY."

    exit 1

fi

log "CMDB_DB_CI_DAILY truncated successfully."


# Step 6: Insert into CMDB_DB_CI_DAILY from CMDB_DB_CI_TEMP

sqlplus -s $username/$password@$database <<EOF

INSERT INTO CMDB_DB_CI_DAILY

SELECT * FROM CMDB_DB_CI_TEMP;

exit

EOF


if [ $? -ne 0 ]; then

    log "Failed to update CMDB_DB_CI_DAILY."

    exit 1

fi

log "CMDB_DB_CI_DAILY updated successfully."


rm $control_file

log "Day 1 operations completed."

Comments

Popular posts from this blog

Database growth

DBA Day-2 ve

Sql1