From d517211425912352ed6161136686722e3db05112 Mon Sep 17 00:00:00 2001 From: Teingi Date: Mon, 9 Dec 2024 19:29:15 +0800 Subject: [PATCH] Backup the files in the ~/.obdiag/ directory --- init.sh | 14 ++++++ obdiag_backup.sh | 70 ++++++++++++++++++++++++++++++ rpm/oceanbase-diagnostic-tool.spec | 3 ++ 3 files changed, 87 insertions(+) create mode 100755 obdiag_backup.sh diff --git a/init.sh b/init.sh index 48c65911..fcf219bf 100755 --- a/init.sh +++ b/init.sh @@ -49,5 +49,19 @@ source ${WORK_DIR}/init_obdiag_cmd.sh if [ -d "${OBDIAG_HOME}/check_package.yaml" ]; then echo "${OBDIAG_HOME}/*check_package.yaml and ${OBDIAG_HOME}/tasks has been discarded. If you have made any changes to these files on your own, please transfer the relevant data to *check_package.yaml in ${OBDIAG_HOME}/check/" fi + +output_file=${OBDIAG_HOME}/version.yaml +version_line=$(obdiag --version 2>&1 | grep -oP 'OceanBase Diagnostic Tool: \K[\d.]+') +if [ -n "$version" ]; then + content="obdiag_version: \"$version\"" + + # Write or update the version information to the file + echo "$content" > "$output_file" + + echo "obdiag version information has been successfully written to $output_file" +else + echo "failed to retrieve obdiag version information." +fi + echo "Init obdiag finished" cd - diff --git a/obdiag_backup.sh b/obdiag_backup.sh new file mode 100755 index 00000000..bc7ff2be --- /dev/null +++ b/obdiag_backup.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# Define source directory and target backup directory +SOURCE_DIR=~/.obdiag/ +BACKUP_DIR=~/.obdiag/backup/ + +# Ensure the backup directory exists, create it if it does not +mkdir -p "$BACKUP_DIR" + +# List of directories to be backed up +DIRS=("display" "check" "gather" "rca") + +# Retrieve version information; if file does not exist or read fails, VERSION remains empty +VERSION="" +if [ -f "$SOURCE_DIR/version.yaml" ]; then + VERSION=$(grep 'obdiag_version:' "$SOURCE_DIR/version.yaml" | awk '{print $2}' | tr -d '"') +fi + +# Define the name of the backup archive (use timestamp for uniqueness, and optionally add version) +TIMESTAMP=$(date +"%Y%m%d_%H%M%S") +TARFILE="$BACKUP_DIR/obdiag_backup_$TIMESTAMP" +TARFILE+="${VERSION:+_v$VERSION}.tar.gz" + +# Temporary directory for staging backup files +TEMP_BACKUP_DIR="$BACKUP_DIR/tmp_obdiag_backup_$TIMESTAMP" +mkdir -p "$TEMP_BACKUP_DIR" + +# Iterate over each directory to be backed up +for dir in "${DIRS[@]}"; do + # Check if the source directory exists + if [ -d "$SOURCE_DIR$dir" ]; then + # Copy the directory into the temporary backup directory + cp -rp "$SOURCE_DIR$dir" "$TEMP_BACKUP_DIR/" + echo "Copied $dir to temporary backup directory." + else + echo "Source directory $SOURCE_DIR$dir does not exist. Skipping." + fi +done + +# Create a tar.gz archive +if tar -czf "$TARFILE" -C "$TEMP_BACKUP_DIR" .; then + echo "Backup archive created successfully at $TARFILE" +else + echo "Failed to create backup archive." + exit 1 +fi + +# Clean up the temporary backup directory +rm -rf "$TEMP_BACKUP_DIR" +echo "Temporary files removed." + +# Cleanup phase: Remove backups older than one year or delete the oldest backups if more than 12 exist +ONE_YEAR_AGO="+365" # find command uses days, so +365 means older than one year + +# Remove backups older than one year +find "$BACKUP_DIR" -maxdepth 1 -name "obdiag_backup_*.tar.gz" -type f -mtime $ONE_YEAR_AGO -exec rm -f {} \; +echo "Removed old backup files older than one year." + +# If there are more than 12 backups, remove the excess oldest ones +BACKUP_FILES=($(find "$BACKUP_DIR" -maxdepth 1 -name "obdiag_backup_*.tar.gz" -type f -printf '%T@ %p\n' | sort -n)) +NUM_BACKUPS=${#BACKUP_FILES[@]} + +if [ $NUM_BACKUPS -gt 12 ]; then + COUNT_TO_DELETE=$((NUM_BACKUPS - 12)) + for ((i = 0; i < COUNT_TO_DELETE; i++)); do + FILE_PATH=${BACKUP_FILES[i]#* } + rm -f "$FILE_PATH" + echo "Removed excess backup file: $FILE_PATH" + done +fi \ No newline at end of file diff --git a/rpm/oceanbase-diagnostic-tool.spec b/rpm/oceanbase-diagnostic-tool.spec index 8f53317d..160ef8b6 100644 --- a/rpm/oceanbase-diagnostic-tool.spec +++ b/rpm/oceanbase-diagnostic-tool.spec @@ -52,6 +52,7 @@ rm -f obdiag.py oceanbase-diagnostic-tool.spec \cp -rf $SRC_DIR/handler/gather/plugins/redact/*.py $BUILD_DIR/SOURCES/gather/redact \cp -rf $SRC_DIR/init.sh $BUILD_DIR/SOURCES/init.sh \cp -rf $SRC_DIR/init_obdiag_cmd.sh $BUILD_DIR/SOURCES/init_obdiag_cmd.sh +\cp -rf $SRC_DIR/obdiag_backup.sh $BUILD_DIR/SOURCES/obdiag_backup.sh \cp -rf $SRC_DIR/conf $BUILD_DIR/SOURCES/conf mkdir -p ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/lib/ mkdir -p ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/dependencies/bin @@ -68,6 +69,7 @@ mkdir -p ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/display \cp -rf $BUILD_DIR/SOURCES/conf ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/ \cp -rf $BUILD_DIR/SOURCES/init.sh ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/ \cp -rf $BUILD_DIR/SOURCES/init_obdiag_cmd.sh ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/ +\cp -rf $BUILD_DIR/SOURCES/obdiag_backup.sh ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/ \cp -rf $BUILD_DIR/SOURCES/check/* ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/check mv ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/check/tasks/*.yaml ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/check/ \cp -rf $BUILD_DIR/SOURCES/gather/tasks ${RPM_BUILD_ROOT}/usr/local/oceanbase-diagnostic-tool/gather @@ -87,6 +89,7 @@ find /usr/local/oceanbase-diagnostic-tool/obdiag -type f -exec chmod 644 {} \; ln -sf /usr/local/oceanbase-diagnostic-tool/obdiag /usr/bin/obdiag chmod +x /usr/local/oceanbase-diagnostic-tool/obdiag cp -rf /usr/local/oceanbase-diagnostic-tool/init_obdiag_cmd.sh /etc/profile.d/obdiag.sh +/usr/local/oceanbase-diagnostic-tool/obdiag_backup.sh /usr/local/oceanbase-diagnostic-tool/init.sh echo -e 'Please execute the following command to init obdiag:\n' echo -e '\033[32m source /usr/local/oceanbase-diagnostic-tool/init.sh \n \033[0m'