SAP 에서 Audit Log 는 설정된 이벤트에 따라 시스템의 행위가 기록되는 중요한 로깅 파일이다.
이러한 Audit Log 는 기본적으로 로컬 파일 시스템 (DIR_AUDIT, DIR_LOGGING) 에 파일 형태로 보관되며,
SAP_BASIS 7.50 SP03 버전부터는 Audit Log 를 DB 에 저장할 수 있지만, 이 경우에도 중요한 시스템 이벤트는 파일 시스템에 저장된다.
이번 글에서는 이렇듯 중요한 Audit Log 에 대해서, 파일 시스템에 저장되는 경우, Audit Log 파일을 백업해주는 간단한 쉘 스크립트를 작성한다.
Audit Log 백업 용도 외에도, 약간의 수정을 통해서 다른 OS 파일들을 손쉽게 백업하도록 설정 할 수 있으니, 필요에 따라 수정하여 사용해도 좋다.
◾ 스크립트 상단의 MODIFICATION 부분은 운영환경에 맞게 수정 필요.
◾ crontab 을 통해 자동 백업 수행 필요.
#!/bin/bash
### MODIFICATION ##########################################
# Original/Backup directory path
ORIG=/usr/sap/<SID>/D<INSTANCE_NUMBER>/log
BKUP=<BACKUP Path>
###########################################################
# BACKUP Logging file path and name
LOG_FILE=${BKUP}/SAP_SAL_BACKUP_LOG.txt
# Target year for consistency check
TARGET_YEAR=$(date +\%Y)
# Backup Audit Log files for the previous day (D-1)
TARGET_DAY=$(date "+%Y%m%d" -d '-1days')
# Logging - Start
echo "= SAP Audit Log Daily Backup - " $(date +\%Y\%m\%d) " ==================" >> ${LOG_FILE}
echo "= TARGET DAY = " ${TARGET_DAY} " ==================================" >> ${LOG_FILE}
echo "============================================================" >> ${LOG_FILE}
# Check if D-1 Audit Log exists
if [ -e $(find "${ORIG}" -type f -name "${TARGET_DAY}*.AUD" -print -quit) ]; then
# If D-1 Audit Log exists, perform backup
# Backup target Audit Log file list
BACKUP_LIST=($(find ${ORIG} -type f -name "${TARGET_DAY}*.AUD"))
# Logging - Check the Count of the Audit Log files to be backed up
echo " - Backup Target File Exists : " ${#BACKUP_LIST[@]} " Files." >> ${LOG_FILE}
# Logging - Check the names of the Audit Log files to be backed up
for FILE_NAME in ${BACKUP_LIST} ; do
echo " =>" ${FILE_NAME} >> ${LOG_FILE}
done
# Copy the original Audit Log files to the backup directory
cp -Rp ${ORIG}/${TARGET_DAY}*.AUD ${BKUP}
# Logging - Backup complete
echo " - Backup Complete" >> ${LOG_FILE}
# Logging - Check if original and backup files match
echo " - Backup File Check" >> ${LOG_FILE}
# After backup the Audit Log, check if individual files match
for CHK in ${BACKUP_LIST} ; do
BACKUP_CHK=${BKUP}/$(basename ${CHK})
if diff ${CHK} ${BACKUP_CHK} 1>/dev/null; then
# Original and backup files match
# Logging - File match confirmation
echo " => Confirm : BACKUP FILE -" ${BACKUP_CHK} >> ${LOG_FILE}
else
# Original and backup files do not match
# Logging - File mismatch confirmation
echo " => Error : BACKUP FILE -" ${BACKUP_CHK} >> ${LOG_FILE}
fi
done
# Logging - End of backup
echo " - Backup End." >> ${LOG_FILE}
else
# No Audit Log file exists for the target date
# Logging - No Backup Target File found, Backup ends
echo " - Backup Target File Not Exists." >> ${LOG_FILE}
echo " - Backup End." >> ${LOG_FILE}
fi
# Logging
echo " " >> ${LOG_FILE}
# Perform consistency check between the original and backup Audit Log files
# (consistency check based on file size)
# Calculate the FULL size of the Original Audit Log files (TARGET_YEAR)
#ORIG_SIZE=$((find ${ORIG}/${TARGET_YEAR}*.AUD -exec stat --format=%s {} \; | awk '{total+=$1} END {print total}'))
ORIG_SIZE_FULL=$(du -sb ${ORIG}/${TARGET_YEAR}*.AUD | awk '{total+=$1} END {print total}')
# Correction amount for excluding Audit Log size generated on the same day
ORIG_SIZE_ADJ=$(du -sb ${ORIG}/$(date "+%Y%m%d")*.AUD | awk '{total+=$1} END {print total}')
# D-1 Original Audit Log File Size
ORIG_SIZE=`expr ${ORIG_SIZE_FULL} - ${ORIG_SIZE_ADJ}`
# Calculate the size of the backup Audit Log files (TARGET_YEAR)
#BKUP_SIZE=$((find ${BKUP}/${TARGET_YEAR}*.AUD -exec stat --format=%s {} \; | awk '{total+=$1} END {print total}'))
BKUP_SIZE=$(du -sb ${BKUP}/${TARGET_YEAR}*.AUD | awk '{total+=$1} END {print total}')
# Logging - Backup consistency check
echo " - " ${TARGET_YEAR} "ALL Backup File Consistency Check" >> ${LOG_FILE}
# Logging - Original/Backup Audit Log File Size
echo " =>" ${TARGET_YEAR} "Original Audit Log File Size : " ${ORIG_SIZE} " Byte" >> ${LOG_FILE}
echo " =>" ${TARGET_YEAR} "Backup Audit Log File Size : " ${BKUP_SIZE} " Byte" >> ${LOG_FILE}
# Compare the total size of the original and backup Audit Log files for the target year
if [ -n "${ORIG_SIZE}" ] && [ -n "${BKUP_SIZE}" ]; then
if [ "${ORIG_SIZE}" == "${BKUP_SIZE}" ]; then
# If the size of the original and backup Audit Log files match, Backup file consistency is confirmed
echo " => GOOD : ALL Backup File Consistency." >> ${LOG_FILE}
else
# If the sizes of the original and backup Audit Log files differ, backup verification is required
echo " => ISSUE : Backup File Verification Required." >> ${LOG_FILE}
fi
else
echo " => SKIP : There is No Comparison." >> ${LOG_FILE}
fi
# Logging - Empty Line
echo " " >> ${LOG_FILE}
# Logging - End of script
echo " - SAP Audit Log Backup Finish." >> ${LOG_FILE}
echo "============================================================" >> ${LOG_FILE}