Skip to content

Commit

Permalink
Backup the files in the ~/.obdiag/ directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Teingi committed Dec 9, 2024
1 parent c0ca6ae commit d517211
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
14 changes: 14 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
70 changes: 70 additions & 0 deletions obdiag_backup.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions rpm/oceanbase-diagnostic-tool.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'

0 comments on commit d517211

Please sign in to comment.